From 9aa2d2c92f64aeded2bed3042031d5f615a71219 Mon Sep 17 00:00:00 2001 From: tomasz1986 Date: Sat, 31 Aug 2024 22:47:10 +0200 Subject: [PATCH] gui: Fix incorrect UI language auto detection (fixes #9668) (#9669) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- gui/default/syncthing/core/localeService.js | 26 ++++++++++++--------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/gui/default/syncthing/core/localeService.js b/gui/default/syncthing/core/localeService.js index 8486df768..ed4ae8d02 100644 --- a/gui/default/syncthing/core/localeService.js +++ b/gui/default/syncthing/core/localeService.js @@ -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); }); }