Skip to content

feat(i18n): add Brazilian Portuguese locale and fall back to English - #780

Merged
yacuo merged 2 commits into
dshplugin:mainfrom
chr-z:feat/pt-br-locale
Nov 14, 2026
Merged

yacuo merged 2 commits into
dshplugin:mainfrom
chr-z:feat/pt-br-locale

Conversation

@chr-z

@chr-z chr-z commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds a Brazilian Portuguese (pt) dictionary to the hub panel and fixes the language fallback that made every non-English host locale render Chinese.

Why

useLanguage resolved the dictionary with lang === 'en' ? 'en' : 'zh', so a host running in Portuguese (or Japanese, German, French, Russian, Spanish) received the zh dictionary — the hub was either English or Chinese, never anything else. The hub is used from a pt-BR host, which is how this showed up.

Changes

  • locales: new pt dictionary, one to one with the en keys (452 entries), keeping every {placeholder} and the dynamic failKind_* / failReason_* pairs.
  • locale-key (src/client/logic/locale-key.ts): dictKeyOf() maps the host locale to a shipped dictionary — unknown locales fall back to English, not Chinese — and nextLocaleOf() cycles en → zh → pt for the header toggle.
  • category labels: CATEGORY_LABELS / CATEGORY_SHORT_LABELS carry pt now, and their type is Record<LocaleId, string> instead of { zh, en }.
  • dates: new intlLocaleOf() in logic/format.ts; absolute dates use pt-BR instead of falling back to zh-CN.
  • remote content: the about and changelog payloads only ship { zh, en }, so non-zh locales now prefer en instead of zh.
  • server: catalog (which catalog file to fetch) and install (error messages) now treat every locale other than zh as English. Server-side messages are still zh/en only — a pt version of those two strings would be a separate change if you want it.
  • tests: key parity and the failKind_*/failReason_* pairing are enforced across all three dictionaries, pt is asserted free of leftover CJK, and locale-key has its own unit tests for the fallback and the toggle order.

Verification

npm run check (typecheck client+server+test, node --test, build) passes locally: 268 tests, 0 failures. Built client/client.js and lib/ are included in the commit, per the repo's practice of committing build output.

Notes / open questions

  • Locale id is pt: that is what the host reports, so pt-BR is not registered separately.
  • The header toggle now cycles three languages instead of two. Happy to keep it as an en/zh toggle and reach pt only from the host settings if you prefer — say the word and I'll adjust.

Follow-up in this branch

  • The header toggle label was derived from the zh URL prefix, so with three languages it always read 中文, even for pt users whose next stop is English. It now comes from the same cycle the toggle uses (logic/locale-key.ts).

Add a full pt-BR dictionary to the hub panel and stop resolving unsupported
host locales to Chinese.

- locales: new `pt` dictionary, one-to-one with the `en` keys (452 entries),
  including the dynamic failKind_/failReason_ pairs and every {placeholder}.
- useLanguage: language selection moves to `dictKeyOf` / `nextLocaleOf`.
  Previously every host locale other than `en` resolved to `zh`, so pt, ja,
  de, fr and ru users got the Chinese interface; unknown locales now fall
  back to English and the header toggle cycles en -> zh -> pt.
- category labels, Intl date formatting and the remote about / changelog
  content prefer English (not Chinese) when the UI language is not zh.
- server catalog and install error messages: non-zh locales receive English
  text instead of Chinese.
- tests: key parity and failKind_/failReason_ pairing are enforced for all
  three dictionaries, pt is checked for leftover CJK text, and the locale
  fallback rules have their own unit tests.
The toggle label was derived from the zh URL prefix, so with three
languages it always read 中文 — including for pt users, whose next stop is
English. The label now comes from the same cycle the toggle actually uses.
@yacuo
yacuo merged commit e50a477 into dshplugin:main Nov 14, 2026
dshplugin pushed a commit that referenced this pull request Nov 14, 2026
The pt dictionary added by #780 could never become active: the host only ships zh and en, so setLocale('pt') threw "locale is not registered" on the second toggle press. Register the language definition first, ignore the duplicate-registration error when another pack already owns pt, and map region-qualified ids (pt-BR, zh-CN) back to their dictionary.
@yacuo

yacuo commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

Merged — thanks, the fallback direction was genuinely wrong and this fixes a bug we had not caught.

One follow-up was needed after merge, in 9adb0fb:

  • the host ships only zh and en as built-in locales; a third language must be declared through ctx.locale.addLanguage({ id, label, fallback }) before setLocale('pt') accepts it. Without that call the header toggle threw locale "pt" is not registered on the second press, so the pt dictionary could never become active. src/client/index.tsx now registers it, and tolerates the duplicate-id error when another language pack already owns pt.
  • dictKeyOf now normalizes the primary subtag, so an active id like pt-BR or zh-CN still resolves to its dictionary instead of falling back to English.
  • comments were switched to Chinese to match the repository convention, and tests/locale-key.test.ts / docs/architecture.md were updated accordingly.

npm run typecheck, npm test (272 tests) and npm run build all pass on main.

@yacuo

yacuo commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

Merged — thanks, the fallback direction was genuinely wrong and this fixes a bug we had not caught.

One follow-up was needed after merge, in 9adb0fb:

  • the host ships only zh and en as built-in locales; a third language must be declared through ctx.locale.addLanguage({ id, label, fallback }) before setLocale('pt') accepts it. Without that call the header toggle threw locale "pt" is not registered on the second press, so the pt dictionary could never become active. src/client/index.tsx now registers it, and tolerates the duplicate-id error when another language pack already owns pt.
    • dictKeyOf now normalizes the primary subtag, so an active id like pt-BR or zh-CN still resolves to its dictionary instead of falling back to English.
    • comments were switched to Chinese to match the repository convention, and tests/locale-key.test.ts / docs/architecture.md were updated accordingly.
      npm run typecheck, npm test (272 tests) and npm run build all pass on main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants