diff options
author | Karsten Heimrich <karsten.heimrich@qt.io> | 2020-12-02 13:15:14 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2020-12-03 09:57:39 +0000 |
commit | 2537b5c6c34a360f943fd79327eee7a7ba6c3fc8 (patch) | |
tree | a75bd0f5a57d500ae85a2b8c7660a1528f2c909a /src/corelib/kernel/qtranslator.cpp | |
parent | bf888576230ef0f506f2d167702eedbafcde7c25 (diff) |
Fix QTranslator::load() search order not following uiLanguages order
Merge the exact and and fuzzy search loop. This way we prioritize
translations which match a more preferred language loosely over ones
which match a less preferred language exactly. This is likely what
most users want.
Fixes: QTBUG-86179
Change-Id: Id1610c82a2e77edf9a5d4417968dcc4c307810d5
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
(cherry picked from commit 8df2bf04917e742470bbd1ff69e003811c5975ff)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src/corelib/kernel/qtranslator.cpp')
-rw-r--r-- | src/corelib/kernel/qtranslator.cpp | 34 |
1 files changed, 9 insertions, 25 deletions
diff --git a/src/corelib/kernel/qtranslator.cpp b/src/corelib/kernel/qtranslator.cpp index 837b6712f8..2e853dde5f 100644 --- a/src/corelib/kernel/qtranslator.cpp +++ b/src/corelib/kernel/qtranslator.cpp @@ -648,7 +648,6 @@ static QString find_translation(const QLocale & locale, QString realname; realname += path + filename + prefix; // using += in the hope for some reserve capacity const int realNameBaseSize = realname.size(); - QStringList fuzzyLocales; // see http://www.unicode.org/reports/tr35/#LanguageMatching for inspiration @@ -662,32 +661,12 @@ static QString find_translation(const QLocale & locale, } #endif - // try explicit locales names first for (QString localeName : qAsConst(languages)) { localeName.replace(QLatin1Char('-'), QLatin1Char('_')); - realname += localeName + suffixOrDotQM; - if (is_readable_file(realname)) - return realname; - - realname.truncate(realNameBaseSize + localeName.size()); - if (is_readable_file(realname)) - return realname; - - realname.truncate(realNameBaseSize); - fuzzyLocales.append(localeName); - } - - // start guessing - for (const QString &fuzzyLocale : qAsConst(fuzzyLocales)) { - QStringView localeName(fuzzyLocale); + // try the complete locale name first and progressively truncate from + // the end until a matching language tag is found (with or without suffix) for (;;) { - int rightmost = localeName.lastIndexOf(QLatin1Char('_')); - // no truncations? fail - if (rightmost <= 0) - break; - localeName.truncate(rightmost); - realname += localeName + suffixOrDotQM; if (is_readable_file(realname)) return realname; @@ -697,6 +676,11 @@ static QString find_translation(const QLocale & locale, return realname; realname.truncate(realNameBaseSize); + + int rightmost = localeName.lastIndexOf(QLatin1Char('_')); + if (rightmost <= 0) + break; // no truncations anymore, break + localeName.truncate(rightmost); } } @@ -755,10 +739,10 @@ static QString find_translation(const QLocale & locale, \li \c /opt/foolib/foo.es \li \c /opt/foolib/foo.fr_CA.qm \li \c /opt/foolib/foo.fr_CA - \li \c /opt/foolib/foo.de.qm - \li \c /opt/foolib/foo.de \li \c /opt/foolib/foo.fr.qm \li \c /opt/foolib/foo.fr + \li \c /opt/foolib/foo.de.qm + \li \c /opt/foolib/foo.de \li \c /opt/foolib/foo.qm \li \c /opt/foolib/foo. \li \c /opt/foolib/foo |