Improve language code matching logic for locale fallback (#1534)

## Description:

This pull request improves the logic for matching language codes to
supported languages.
Now, the function tries to find the most specific language by splitting
the input code with “-” or “_”, and searching from the longest
combination to the shortest.
If no direct match is found, it searches for all supported language
codes that start with the same base (first two letters) and chooses the
most specific one.
If nothing matches, it defaults to English.

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced
- [x] I have read and accepted the CLA aggreement (only required once).

## Please put your Discord username so you can be contacted if a bug or
regression is found:

aorumuri
This commit is contained in:
Aotumuri
2025-07-26 03:08:35 +09:00
committed by GitHub
parent d010fdbda0
commit 1c6aa2bfc3
4 changed files with 19 additions and 11 deletions
@@ -167,7 +167,7 @@
"en": "Brazilian Portuguese",
"native": "Português brasileiro",
"svg": "br",
"lang_code": "pt_BR"
"lang_code": "pt-BR"
},
"game_mode": {
"ffa": "Free for All",
@@ -3,7 +3,7 @@
"en": "Swedish",
"native": "Svenska",
"svg": "se",
"lang_code": "sv_SE"
"lang_code": "sv-SE"
},
"common": {
"close": "Stäng"
@@ -3,7 +3,7 @@
"en": "Chinese Simplified",
"native": "简体中文",
"svg": "cn",
"lang_code": "zh_CN"
"lang_code": "zh-CN"
},
"common": {
"close": "关闭"
+16 -8
View File
@@ -21,14 +21,14 @@ import ja from "../../resources/lang/ja.json";
import ko from "../../resources/lang/ko.json";
import nl from "../../resources/lang/nl.json";
import pl from "../../resources/lang/pl.json";
import pt_BR from "../../resources/lang/pt_BR.json";
import pt_BR from "../../resources/lang/pt-BR.json";
import ru from "../../resources/lang/ru.json";
import sh from "../../resources/lang/sh.json";
import sv_SE from "../../resources/lang/sv_SE.json";
import sv_SE from "../../resources/lang/sv-SE.json";
import tp from "../../resources/lang/tp.json";
import tr from "../../resources/lang/tr.json";
import uk from "../../resources/lang/uk.json";
import zh_CN from "../../resources/lang/zh_CN.json";
import zh_CN from "../../resources/lang/zh-CN.json";
@customElement("lang-selector")
export class LangSelector extends LitElement {
@@ -55,7 +55,7 @@ export class LangSelector extends LitElement {
ja,
nl,
pl,
pt_BR,
"pt-BR": pt_BR,
ru,
sh,
tr,
@@ -65,8 +65,8 @@ export class LangSelector extends LitElement {
he,
da,
fi,
sv_SE,
zh_CN,
"sv-SE": sv_SE,
"zh-CN": zh_CN,
ko,
gl,
};
@@ -93,8 +93,16 @@ export class LangSelector extends LitElement {
private getClosestSupportedLang(lang: string): string {
if (!lang) return "en";
if (lang in this.languageMap) return lang;
const base = lang.split("-")[0];
if (base in this.languageMap) return base;
const base = lang.slice(0, 2);
const candidates = Object.keys(this.languageMap).filter((key) =>
key.startsWith(base),
);
if (candidates.length > 0) {
candidates.sort((a, b) => b.length - a.length); // More specific first
return candidates[0];
}
return "en";
}