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>
This commit is contained in:
Karsten Heimrich 2020-12-02 13:15:14 +01:00 committed by Qt Cherry-pick Bot
parent bf88857623
commit 2537b5c6c3

View File

@ -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