From c83586cfc0d37afe1e6ce2d6ce7990b354a90065 Mon Sep 17 00:00:00 2001 From: fanfan <135687942+fanfan1233@users.noreply.github.com> Date: Wed, 23 Sep 2026 17:05:30 +0800 Subject: [PATCH] fix(i18n): match browser locale tags precisely --- frontend/src/i18n.tsx | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/frontend/src/i18n.tsx b/frontend/src/i18n.tsx index 4062191..d0b1aa6 100644 --- a/frontend/src/i18n.tsx +++ b/frontend/src/i18n.tsx @@ -285,10 +285,19 @@ const storageKey = "laya-server-locale" export function resolveLocale(languages: readonly string[]): Locale { for (const language of languages) { - const value = language.toLowerCase() - if (value.startsWith("zh-hant") || /^zh-(tw|hk|mo)(-|$)/.test(value)) return "zh-TW" - if (value.startsWith("zh")) return "zh-CN" - if (value.startsWith("en")) return "en" + let locale: Intl.Locale + try { + locale = new Intl.Locale(language) + } catch { + // Ignore malformed preferences and continue to the next supported language. + continue + } + if (locale.language === "zh") { + // An explicit script takes precedence over the region's usual script. + if (locale.script) return locale.script === "Hant" ? "zh-TW" : "zh-CN" + return ["TW", "HK", "MO"].includes(locale.region ?? "") ? "zh-TW" : "zh-CN" + } + if (locale.language === "en") return "en" } return "en" }