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" }