Use Qt UI language if system was not determined.

Also rename platform/mac/specific_mac.cpp to .mm
This commit is contained in:
John Preston 2017-05-30 12:31:32 +03:00
parent 6fb980ca79
commit f5353080e7
13 changed files with 119 additions and 49 deletions

View File

@ -49,7 +49,7 @@ void CloudManager::requestLangPackDifference() {
_langPackRequestId = 0;
}).send();
} else {
_langPackRequestId = request(MTPlangpack_GetLangPack()).done([this](const MTPLangPackDifference &result) {
_langPackRequestId = request(MTPlangpack_GetLangPack(MTP_string(_langpack.cloudLangCode()))).done([this](const MTPLangPackDifference &result) {
_langPackRequestId = 0;
applyLangPackDifference(result);
}).fail([this](const RPCError &error) {
@ -58,6 +58,26 @@ void CloudManager::requestLangPackDifference() {
}
}
void CloudManager::setSuggestedLanguage(const QString &langCode) {
if (_langpack.id().isEmpty()
&& !langCode.isEmpty()
&& langCode != Lang::DefaultLanguageId()) {
_suggestedLanguage = langCode;
} else {
_suggestedLanguage = QString();
}
if (!_languageWasSuggested) {
_languageWasSuggested = true;
_firstLanguageSuggestion.notify();
if (AuthSession::Exists() && !_suggestedLanguage.isEmpty()) {
_offerSwitchToId = _suggestedLanguage;
offerSwitchLangPack();
}
}
}
void CloudManager::applyLangPackDifference(const MTPLangPackDifference &difference) {
Expects(difference.type() == mtpc_langPackDifference);
if (_langpack.isCustom()) {
@ -68,14 +88,6 @@ void CloudManager::applyLangPackDifference(const MTPLangPackDifference &differen
auto langpackId = qs(langpack.vlang_code);
if (needToApplyLangPack(langpackId)) {
applyLangPackData(langpack);
} else if (_langpack.id().isEmpty()) {
_offerSwitchToId = langpackId;
if (langpack.vfrom_version.v == 0) {
_offerSwitchToData = std::make_unique<MTPLangPackDifference>(difference);
} else {
_offerSwitchToData.reset();
}
offerSwitchLangPack();
} else {
LOG(("Lang Warning: Ignoring update for '%1' because our language is '%2'").arg(langpackId).arg(_langpack.id()));
}

View File

@ -51,6 +51,13 @@ public:
void applyLangPackDifference(const MTPLangPackDifference &difference);
void switchToLanguage(const QString &id);
void setSuggestedLanguage(const QString &langCode);
QString suggestedLanguage() const {
return _suggestedLanguage;
}
base::Observable<void> &firstLanguageSuggestion() {
return _firstLanguageSuggestion;
}
private:
void offerSwitchLangPack();
@ -71,6 +78,10 @@ private:
QString _offerSwitchToId;
std::unique_ptr<MTPLangPackDifference> _offerSwitchToData;
QString _suggestedLanguage;
bool _languageWasSuggested = false;
base::Observable<void> _firstLanguageSuggestion;
};
inline bool operator==(const CloudManager::Language &a, const CloudManager::Language &b) {

View File

@ -286,15 +286,26 @@ void Instance::fillDefaults() {
}
}
QString Instance::cloudLangCode() const {
if (isCustom() || id().isEmpty()) {
QString Instance::systemLangCode() const {
if (_systemLanguage.isEmpty()) {
_systemLanguage = Platform::SystemLanguage();
if (_systemLanguage.isEmpty()) {
_systemLanguage = Platform::SystemLanguage();
auto uiLanguages = QLocale::system().uiLanguages();
if (!uiLanguages.isEmpty()) {
_systemLanguage = uiLanguages.front();
}
if (_systemLanguage.isEmpty()) {
_systemLanguage = DefaultLanguageId();
}
}
return _systemLanguage;
// _systemLanguage = "de"; // TESTING
}
return _systemLanguage;
}
QString Instance::cloudLangCode() const {
if (isCustom() || id().isEmpty()) {
return DefaultLanguageId();
}
return id();
}

View File

@ -48,6 +48,7 @@ public:
Instance(Instance &&other) = default;
Instance &operator=(Instance &&other) = default;
QString systemLangCode() const;
QString cloudLangCode() const;
QString id() const {

View File

@ -780,11 +780,12 @@ void ConnectionPrivate::tryToSend() {
MTPInitConnection<mtpRequest> initWrapper;
int32 initSize = 0, initSizeInInts = 0;
if (needsLayer) {
auto langCode = sessionData->langCode();
auto systemLangCode = sessionData->systemLangCode();
auto cloudLangCode = sessionData->cloudLangCode();
auto langPack = "tdesktop";
auto deviceModel = (_dcType == DcType::Cdn) ? "n/a" : cApiDeviceModel();
auto systemVersion = (_dcType == DcType::Cdn) ? "n/a" : cApiSystemVersion();
initWrapper = MTPInitConnection<mtpRequest>(MTP_int(ApiId), MTP_string(deviceModel), MTP_string(systemVersion), MTP_string(cApiAppVersion()), MTP_string(langCode), MTP_string(langPack), MTP_string(langCode), mtpRequest());
initWrapper = MTPInitConnection<mtpRequest>(MTP_int(ApiId), MTP_string(deviceModel), MTP_string(systemVersion), MTP_string(cApiAppVersion()), MTP_string(systemLangCode), MTP_string(langPack), MTP_string(cloudLangCode), mtpRequest());
initSizeInInts = (initWrapper.innerLength() >> 2) + 2;
initSize = initSizeInInts * sizeof(mtpPrime);
}

View File

@ -29,6 +29,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "mtproto/sender.h"
#include "mtproto/rsa_public_key.h"
#include "lang/lang_instance.h"
#include "lang/lang_cloud_manager.h"
#include "base/timer.h"
namespace MTP {
@ -600,6 +601,7 @@ void Instance::Private::configLoadDone(const MTPConfig &result) {
Global::SetPhoneCallsEnabled(data.is_phonecalls_enabled());
Global::RefPhoneCallsEnabledChanged().notify();
}
Lang::CurrentCloudManager().setSuggestedLanguage(data.has_suggested_lang_code() ? qs(data.vsuggested_lang_code) : QString());
Local::writeSettings();
@ -1082,6 +1084,8 @@ bool Instance::Private::onErrorDefault(mtpRequestId requestId, const RPCError &e
session->sendPrepared(request);
}
return true;
} else if (err == qstr("CONNECTION_LANG_CODE_INVALID")) {
Lang::CurrentCloudManager().switchToLanguage(qsl("en"));
} else if (err == qstr("MSG_WAIT_FAILED")) {
mtpRequest request;
{
@ -1270,6 +1274,10 @@ DcId Instance::mainDcId() const {
return _private->mainDcId();
}
QString Instance::systemLangCode() const {
return Lang::Current().systemLangCode();
}
QString Instance::cloudLangCode() const {
return Lang::Current().cloudLangCode();
}

View File

@ -53,6 +53,7 @@ public:
void suggestMainDcId(DcId mainDcId);
void setMainDcId(DcId mainDcId);
DcId mainDcId() const;
QString systemLangCode() const;
QString cloudLangCode() const;
void setKeyForWrite(DcId dcId, const AuthKeyPtr &key);

View File

@ -76,7 +76,8 @@ Session::Session(gsl::not_null<Instance*> instance, ShiftedDcId shiftedDcId) : Q
connect(&timeouter, SIGNAL(timeout()), this, SLOT(checkRequestsByTimer()));
timeouter.start(1000);
data.setLangCode(instance->cloudLangCode());
data.setSystemLangCode(instance->systemLangCode());
data.setCloudLangCode(instance->cloudLangCode());
connect(&sender, SIGNAL(timeout()), this, SLOT(needToResumeAndSend()));
}
@ -126,7 +127,8 @@ void Session::restart() {
DEBUG_LOG(("Session Error: can't restart a killed session"));
return;
}
data.setLangCode(_instance->cloudLangCode());
data.setSystemLangCode(_instance->systemLangCode());
data.setCloudLangCode(_instance->cloudLangCode());
emit needToRestart();
}

View File

@ -123,13 +123,21 @@ public:
_layerInited = was;
}
QString langCode() const {
QString systemLangCode() const {
QReadLocker locker(&_lock);
return _langCode;
return _systemLangCode;
}
void setLangCode(const QString &code) {
void setSystemLangCode(const QString &code) {
QWriteLocker locker(&_lock);
_langCode = code;
_systemLangCode = code;
}
QString cloudLangCode() const {
QReadLocker locker(&_lock);
return _cloudLangCode;
}
void setCloudLangCode(const QString &code) {
QWriteLocker locker(&_lock);
_cloudLangCode = code;
}
void setSalt(uint64 salt) {
@ -268,7 +276,8 @@ private:
AuthKeyPtr _authKey;
bool _keyChecked = false;
bool _layerInited = false;
QString _langCode;
QString _systemLangCode;
QString _cloudLangCode;
mtpPreRequestMap _toSend; // map of request_id -> request, that is waiting to be sent
mtpRequestMap _haveSent; // map of msg_id -> request, that was sent, msDate = 0 for msgs_state_req (no resend / state req), msDate = 0, seqNo = 0 for containers

View File

@ -549,9 +549,18 @@ QString objc_currentCountry() {
}
QString objc_currentLang() {
NSLocale *currentLocale = [NSLocale currentLocale]; // get the current locale.
NSString *currentLang = [currentLocale objectForKey:NSLocaleLanguageCode];
return currentLang ? NS2QString(currentLang) : QString();
if (auto currentLocale = [NSLocale currentLocale]) { // get the current locale.
if (NSString *collator = [currentLocale objectForKey:NSLocaleCollatorIdentifier]) {
return NS2QString(collator);
}
if (NSString *identifier = [currentLocale objectForKey:NSLocaleIdentifier]) {
return NS2QString(identifier);
}
if (NSString *language = [currentLocale objectForKey:NSLocaleLanguageCode]) {
return NS2QString(language);
}
}
return QString();
}
QByteArray objc_downloadPathBookmark(const QString &path) {

View File

@ -376,8 +376,8 @@ QString SystemCountry() {
namespace {
QString GetLangCodeById(int lngId) {
int primary = lngId & 0xFF;
QString GetLangCodeById(unsigned lngId) {
auto primary = (lngId & 0xFFU);
switch (primary) {
case 0x36: return qsl("af");
case 0x1C: return qsl("sq");
@ -498,37 +498,42 @@ QString GetLangCodeById(int lngId) {
} // namespace
QString SystemLanguage() {
int chCount = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SNAME, 0, 0);
if (chCount && chCount < 128) {
WCHAR wstrLocale[128];
int len = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SNAME, wstrLocale, chCount);
if (!len) {
constexpr auto kMaxLanguageLength = 128;
auto uiLanguageId = GetUserDefaultUILanguage();
auto uiLanguageLength = GetLocaleInfo(uiLanguageId, LOCALE_SNAME, nullptr, 0);
if (uiLanguageLength > 0 && uiLanguageLength < kMaxLanguageLength) {
WCHAR uiLanguageWideString[kMaxLanguageLength] = { 0 };
uiLanguageLength = GetLocaleInfo(uiLanguageId, LOCALE_SNAME, uiLanguageWideString, uiLanguageLength);
if (uiLanguageLength <= 0) {
return QString();
}
QString locale = QString::fromStdWString(std::wstring(wstrLocale));
QRegularExpressionMatch m = QRegularExpression("(^|[^a-z])([a-z]{2})-").match(locale);
if (m.hasMatch()) {
return m.captured(2);
}
return QString::fromWCharArray(uiLanguageWideString);
}
chCount = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ILANGUAGE, 0, 0);
if (chCount && chCount < 128) {
WCHAR wstrLocale[128];
int len = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ILANGUAGE, wstrLocale, chCount), lngId = 0;
if (len < 5) return QString();
auto uiLanguageCodeLength = GetLocaleInfo(uiLanguageId, LOCALE_ILANGUAGE, nullptr, 0);
if (uiLanguageCodeLength > 0 && uiLanguageCodeLength < kMaxLanguageLength) {
WCHAR uiLanguageCodeWideString[kMaxLanguageLength] = { 0 };
uiLanguageCodeLength = GetLocaleInfo(uiLanguageId, LOCALE_ILANGUAGE, uiLanguageCodeWideString, uiLanguageCodeLength);
if (uiLanguageCodeLength <= 0) {
return QString();
}
for (int i = 0; i < 4; ++i) {
WCHAR ch = wstrLocale[i];
lngId *= 16;
auto languageCode = 0U;
for (auto i = 0; i != uiLanguageCodeLength; ++i) {
auto ch = uiLanguageCodeWideString[i];
if (!ch) {
break;
}
languageCode *= 0x10U;
if (ch >= WCHAR('0') && ch <= WCHAR('9')) {
lngId += (ch - WCHAR('0'));
languageCode += static_cast<unsigned>(int(ch) - int(WCHAR('0')));
} else if (ch >= WCHAR('A') && ch <= WCHAR('F')) {
lngId += (10 + ch - WCHAR('A'));
languageCode += static_cast<unsigned>(0x0A + int(ch) - int(WCHAR('A')));
} else {
return QString();
}
}
return GetLangCodeById(lngId);
return GetLangCodeById(languageCode);
}
return QString();
}

View File

@ -289,7 +289,7 @@
<(src_loc)/platform/mac/main_window_mac.h
<(src_loc)/platform/mac/notifications_manager_mac.mm
<(src_loc)/platform/mac/notifications_manager_mac.h
<(src_loc)/platform/mac/specific_mac.cpp
<(src_loc)/platform/mac/specific_mac.mm
<(src_loc)/platform/mac/specific_mac.h
<(src_loc)/platform/mac/specific_mac_p.mm
<(src_loc)/platform/mac/specific_mac_p.h