-
Notifications
You must be signed in to change notification settings - Fork 4.4k
feat(desktop): add zh locale support and localize navigation/menu labels #8639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
aecd7db
dfb964f
1acf963
739b333
26b8f2c
1ffccb1
612b5b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ | |
| export { defineMessages, useIntl } from 'react-intl'; | ||
|
|
||
| /** The set of locales that have translation catalogs. */ | ||
| const SUPPORTED_LOCALES = new Set(['en']); | ||
| const SUPPORTED_LOCALES = new Set(['en', 'zh', 'zh-CN']); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Adding Useful? React with 👍 / 👎. |
||
|
|
||
| /** | ||
| * Detect the user's preferred locale. | ||
|
|
@@ -36,23 +36,30 @@ export function getLocale(): { locale: string; messageLocale: string } { | |
| candidates.push(navigator.language); | ||
| } | ||
|
|
||
| for (const tag of candidates) { | ||
| for (const candidate of candidates) { | ||
| const normalized = candidate.replace(/_/g, '-'); | ||
|
|
||
| let canonical: string | undefined; | ||
| try { | ||
| [canonical] = Intl.getCanonicalLocales(normalized); | ||
| } catch { | ||
| canonical = undefined; | ||
| } | ||
|
|
||
| const resolved = canonical ?? normalized; | ||
|
|
||
| // Exact match first | ||
| if (SUPPORTED_LOCALES.has(tag)) return { locale: tag, messageLocale: tag }; | ||
| if (SUPPORTED_LOCALES.has(resolved)) { | ||
| return { locale: resolved, messageLocale: resolved }; | ||
| } | ||
|
|
||
| // Try base language (e.g. "pt-BR" → "pt") for the catalog, but keep the | ||
| // full regional tag for formatting so date/number output respects the region. | ||
| const base = tag.split('-')[0]; | ||
| const base = resolved.split('-')[0]; | ||
| if (SUPPORTED_LOCALES.has(base)) { | ||
| // Validate the full tag is a well-formed BCP 47 locale before using it | ||
| // for formatting. Invalid tags (e.g. "en-") would cause RangeError in | ||
| // Intl APIs, so fall back to the base language in that case. | ||
| let locale = base; | ||
| try { | ||
| [locale] = Intl.getCanonicalLocales(tag); | ||
| } catch { | ||
| // tag is not valid BCP 47 — use the base language instead | ||
| } | ||
| return { locale, messageLocale: base }; | ||
| // If canonicalization fails, return the base locale so downstream Intl APIs | ||
| // never receive malformed tags like "en-". | ||
| return { locale: canonical ?? base, messageLocale: base }; | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With
zh/zh-CNnow listed as supported,GOOSE_LOCALE=zh_CNbecomes a practical input, butgetLocale()still compares the raw tag and only derives a base viasplit('-'), sozh_CNmisses both checks and falls back to English. MeanwhileisZhLocale()inmain.tsalready normalizes_to-, so this produces a mixed-language app (Chinese native menu, English renderer). Normalize candidate tags ingetLocale()before matching/canonicalizing to keep renderer and menu localization consistent.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Addressed in 1acf963.
I updated i18n locale resolution to normalize underscore tags (e.g. zh_CN -> zh-CN) before canonicalization/matching, so renderer locale selection stays consistent with native menu locale handling.