gui: Fix incorrect UI language auto detection (fixes #9668) (#9669)

gui: Fix incorrect UI language auto detection (fixes #9668)

Currently, the code only checks whether the detected language partially
matches one of the available languages. This means that if the detected
language is "fi" but among the available languages there is only "fil"
and no "fi", then it will match "fi" with "fil", even though the two are
completely different languages.

With this change, the matching is only done when there is a hyphen in
the language code, e.g. "en" will match with "en-US".

Signed-off-by: Tomasz Wilczyński <twilczynski@naver.com>
This commit is contained in:
tomasz1986 2024-08-31 22:47:10 +02:00 committed by GitHub
parent d1c5100c98
commit 9aa2d2c92f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -59,30 +59,35 @@ angular.module('syncthing.core')
// Find the first language in the list provided by the user's browser
// that is a prefix of a language we have available. That is, "en"
// sent by the browser will match "en" or "en-US", while "zh-TW" will
// match only "zh-TW" and not "zh-CN".
// match only "zh-TW" and not "zh" or "zh-CN".
var i,
lang,
browserLang,
matching,
locale = _defaultLocale;
locale = _defaultLocale; // Fallback if nothing matched
for (i = 0; i < langs.length; i++) {
lang = langs[i];
browserLang = langs[i];
if (lang.length < 2) {
if (browserLang.length < 2) {
continue;
}
matching = _availableLocales.filter(function (possibleLang) {
// The langs returned by the /rest/langs call will be in lower
// The langs returned by the /svc/langs call will be in lower
// case. We compare to the lowercase version of the language
// code we have as well.
possibleLang = possibleLang.toLowerCase();
if (possibleLang.length > lang.length) {
return possibleLang.indexOf(lang) === 0;
} else {
return lang.indexOf(possibleLang) === 0;
if (possibleLang.indexOf(browserLang) !== 0) {
// Prefix does not match
return false;
}
if (possibleLang.length > browserLang.length) {
// Must match up to the next hyphen separator
return possibleLang[browserLang.length] === '-';
}
// Same length, exact match
return true;
});
if (matching.length >= 1) {
@ -90,7 +95,6 @@ angular.module('syncthing.core')
break;
}
}
// Fallback if nothing matched
useLocale(locale);
});
}