QTranslator: generalize a UNIX-only path

QTranslator builds a list of "candidate" names in order to find out
the translation file to load. Part of the name comes from the UI
languages, as determined by the QLocale object passed to load()
(via QLocale::uiLanguages()).

On UNIX this list of languages is processed by adding a lowercase
version of each language. In other words, if the list is something
like:

{"en-Latn-US", "en-US", "en"}

then it is changed to contain:

{"en-Latn-US", "en-latn-us", "en-US", "en-us", "en"}

(The last element is not needlessly duplicated, since it's already
fully lowercase.)

I am not sure why this is done only on UNIX: the commit introducing
this behavior predates public history. If I have to take a guess, it's
done because the language is then used to assemble a file name that
QTranslator tries to open. On UNIX file names are case sensitive, so
if the translation file is called "translations_en_us.qm" it would
fail to load under a "en-US" locale (the "-" -> "_" processing is done
later).

But what about Windows? In principle, turning the names in lowercase
is not necessary as Windows' filesystem is case insensitive; a
translation file called "translations_en_us.qm" will still be loaded
if opened as "translations_en_US.qm"...

... *except* if the file is in the resource system! In that case,
filesystem access is still case sensitive, and will fail to load the
translation file (which instead would load just fine on UNIX).

Plug this silly cross-platform difference by also lowercasing on
Windows.

Change-Id: I2573721e33d9da08f60a5bb56e35e4553cbe4efe
Pick-to: 5.15 6.2 6.3 6.4
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Kai Koehne <kai.koehne@qt.io>
This commit is contained in:
Giuseppe D'Angelo 2022-07-29 11:38:56 +02:00
parent c4fabe37b9
commit b779b8062c

View File

@ -620,15 +620,20 @@ static QString find_translation(const QLocale & locale,
// see http://www.unicode.org/reports/tr35/#LanguageMatching for inspiration
// For each language_country returned by locale.uiLanguages(), add
// also a lowercase version to the list. Since these languages are
// used to create file names, this is important on case-sensitive
// file systems, where otherwise a file called something like
// "prefix_en_us.qm" won't be found under the "en_US" locale. Note
// that the Qt resource system is always case-sensitive, even on
// Windows (in other words: this codepath is *not* UNIX-only).
QStringList languages = locale.uiLanguages();
#if defined(Q_OS_UNIX)
for (int i = languages.size()-1; i >= 0; --i) {
QString lang = languages.at(i);
QString lowerLang = lang.toLower();
if (lang != lowerLang)
languages.insert(i + 1, lowerLang);
}
#endif
for (QString localeName : qAsConst(languages)) {
localeName.replace(u'-', u'_');