Skip to content

Commit 165a956

Browse files
findlayfengmgallien
authored andcommitted
fix(application): Correcting the loading of translation files
Now, it attempts to load all values ​​returned by the function QLocale::system().uiLanguages() sequentially until success or all values ​​fail, instead of only trying the first value. Signed-off-by: Findlay Feng <[email protected]>
1 parent 3d9b37a commit 165a956

File tree

1 file changed

+26
-31
lines changed

1 file changed

+26
-31
lines changed

src/gui/application.cpp

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ bool Application::configVersionMigration()
133133
AccountManager::backwardMigrationSettingsKeys(&deleteKeys, &ignoreKeys);
134134
FolderMan::backwardMigrationSettingsKeys(&deleteKeys, &ignoreKeys);
135135
configFile.setClientPreviousVersionString(configFile.clientVersionString());
136-
136+
137137
qCDebug(lcApplication) << "Migration is in progress:" << configFile.isMigrationInProgress();
138138
const auto versionChanged = configFile.isUpgrade() || configFile.isDowngrade();
139139
if (versionChanged) {
@@ -997,23 +997,6 @@ void Application::handleEditLocallyFromOptions()
997997
_editFileLocallyUrl.clear();
998998
}
999999

1000-
QString substLang(const QString &lang)
1001-
{
1002-
// Map the more appropriate script codes
1003-
// to country codes as used by Qt and
1004-
// transifex translation conventions.
1005-
1006-
// Simplified Chinese
1007-
if (lang == QLatin1String("zh_Hans")) {
1008-
return QLatin1String("zh_CN");
1009-
}
1010-
// Traditional Chinese
1011-
if (lang == QLatin1String("zh_Hant")) {
1012-
return QLatin1String("zh_TW");
1013-
}
1014-
return lang;
1015-
}
1016-
10171000
QString enforcedLanguage()
10181001
{
10191002
const ConfigFile cfg;
@@ -1028,11 +1011,6 @@ QString enforcedLanguage()
10281011

10291012
void Application::setupTranslations()
10301013
{
1031-
qCInfo(lcApplication) << "System UI languages are:" << QLocale::system().uiLanguages();
1032-
const auto enforcedLocale = enforcedLanguage();
1033-
const auto lang = substLang(!enforcedLocale.isEmpty() ? enforcedLocale : QLocale::system().uiLanguages(QLocale::TagSeparator::Underscore).first());
1034-
qCInfo(lcApplication) << "selected application language:" << lang;
1035-
10361014
auto *translator = new QTranslator(this);
10371015
auto *qtTranslator = new QTranslator(this);
10381016
auto *qtkeychainTranslator = new QTranslator(this);
@@ -1043,19 +1021,36 @@ void Application::setupTranslations()
10431021
qCWarning(lcApplication()) << trPath << "folder containing translations is missing. Impossible to load translations";
10441022
return;
10451023
}
1046-
const QString trFile = QLatin1String("client_") + lang;
1047-
qCDebug(lcApplication()) << "trying to load" << lang << "in" << trFile << "from" << trPath;
1048-
if (translator->load(trFile, trPath) || lang.startsWith(QLatin1String("en"))) {
1024+
1025+
qCInfo(lcApplication) << "System UI languages are:" << QLocale::system().uiLanguages();
1026+
auto choosenLanguage = enforcedLanguage();
1027+
if (choosenLanguage.isEmpty()) {
1028+
for(const auto &localeToTest : QLocale::system().uiLanguages(QLocale::TagSeparator::Underscore)){
1029+
const auto trFile = QString{QLatin1String{"client_"} + localeToTest};
1030+
qCDebug(lcApplication()) << "trying to load" << localeToTest << "in" << trFile << "from" << trPath;
1031+
if (translator->load(trFile, trPath)) {
1032+
choosenLanguage = localeToTest;
1033+
break;
1034+
}
1035+
}
1036+
} else {
1037+
const QString trFile = QLatin1String("client_") + choosenLanguage;
1038+
qCDebug(lcApplication()) << "trying to load" << choosenLanguage << "in" << trFile << "from" << trPath;
1039+
translator->load(trFile, trPath);
1040+
}
1041+
1042+
qCInfo(lcApplication) << "selected application language:" << choosenLanguage;
1043+
if (!translator->isEmpty() || choosenLanguage.startsWith(QLatin1String("en"))) {
10491044
// Permissive approach: Qt and keychain translations
10501045
// may be missing, but Qt translations must be there in order
10511046
// for us to accept the language. Otherwise, we try with the next.
10521047
// "en" is an exception as it is the default language and may not
10531048
// have a translation file provided.
1054-
qCInfo(lcApplication) << "Using" << lang << "translation";
1055-
setProperty("ui_lang", lang);
1049+
qCInfo(lcApplication) << "Using" << choosenLanguage << "translation";
1050+
setProperty("ui_lang", choosenLanguage);
10561051
const QString qtTrPath = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
1057-
const QString qtTrFile = QLatin1String("qt_") + lang;
1058-
const QString qtBaseTrFile = QLatin1String("qtbase_") + lang;
1052+
const QString qtTrFile = QLatin1String("qt_") + choosenLanguage;
1053+
const QString qtBaseTrFile = QLatin1String("qtbase_") + choosenLanguage;
10591054
if (!qtTranslator->load(qtTrFile, qtTrPath)) {
10601055
if (!qtTranslator->load(qtTrFile, trPath)) {
10611056
if (!qtTranslator->load(qtBaseTrFile, qtTrPath)) {
@@ -1065,7 +1060,7 @@ void Application::setupTranslations()
10651060
}
10661061
}
10671062
}
1068-
const QString qtkeychainTrFile = QLatin1String("qtkeychain_") + lang;
1063+
const QString qtkeychainTrFile = QLatin1String("qtkeychain_") + choosenLanguage;
10691064
if (!qtkeychainTranslator->load(qtkeychainTrFile, qtTrPath)) {
10701065
if (!qtkeychainTranslator->load(qtkeychainTrFile, trPath)) {
10711066
qCDebug(lcApplication()) << "impossible to load QtKeychain translation catalog" << qtkeychainTrFile;

0 commit comments

Comments
 (0)