fix(i18n): match browser locale by language subtag instead of exact code - #174
Open
iAstro wants to merge 1 commit into
Open
fix(i18n): match browser locale by language subtag instead of exact code#174iAstro wants to merge 1 commit into
iAstro wants to merge 1 commit into
Conversation
The extension ships en-US and zh-CN resources, but only en-US was ever
selected: every other browser language fell through to fallbackLng
("zh-CN"), so users on en, en-GB, en-CN, en-AU, ja-JP, fr-FR saw Chinese.
Match on the BCP 47 language subtag and normalise it to a resource key:
en* -> en-US
zh-Hans* / zh-CN / zh-SG / zh -> zh-CN
zh-Hant* / zh-TW / zh-HK / zh-MO -> zh-TW
anything else -> unchanged (fallbackLng default: en-US)
Simplified vs Traditional is decided by the script subtag (Hans/Hant) when
present, then by the region subtag, since Chrome reports zh-CN / zh-TW
rather than script subtags. The zh-TW branch is written now so adding a
Traditional translation later needs no code change.
chrome.i18n.getUILanguage() is now preferred over navigator.language, which
is unreliable on the chrome-extension:// origin the popup runs on.
Adds a locale-resolution regression matrix (28 cases).
Closes Tencent#168
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The extension ships
en-USandzh-CN, but onlyen-USwas ever selected - every other browser language fell through tofallbackLng: "zh-CN", so users onen,en-GB,en-CN,en-AU,ja-JP,fr-FRsaw a Chinese UI.Fix
Match on the BCP 47 language subtag instead of the full locale code:
en*->en-US(every English variant)zh-Hans*/zh-CN/zh-SG/zh->zh-CNzh-Hant*/zh-TW/zh-HK/zh-MO->zh-TWfallbackLngThree layers, all in
packages/i18n:chrome.i18n.getUILanguage()overnavigator.language, which is unreliable on thechrome-extension://origin the popup runs on.convertDetectedLanguageapplying the table above.fallbackLngis now an object:{ en: ["en-US"], zh: ["zh-CN"], default: ["en-US"] }- unmatched languages land on English, not Chinese.Simplified vs Traditional
Decided by the script subtag (
Hans/Hant) when present, then by the region subtag, since Chrome reportszh-CN/zh-TWrather than script subtags.The
zh-TWbranch is written now, so adding a Traditional translation later needs no code change - only a newzh-TWresource. Until then i18next falls back tozh-CN(graceful degradation, not a misroute).Tests
Adds
packages/i18n/tests/locale-resolution.test.ts(28 cases) locking the table in as regression tests; the package previously had no tests.28/28 tests pass;
tsc --noEmit,biome check, andpnpm ext:buildare all clean.Closes #168