Skip to content

feat(i18n): English UI layer with reactive language switching - #7

Merged
feiyang-dev merged 3 commits into
feiyang-dev:mainfrom
ayleen:feat/i18n-english
Aug 27, 2026
Merged

feiyang-dev merged 3 commits into
feiyang-dev:mainfrom
ayleen:feat/i18n-english

Conversation

@ayleen

@ayleen ayleen commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

feat(i18n): English UI layer with reactive language switching

English

What this does

Adds a lightweight i18n layer so the whole web panel can be displayed in English while keeping Chinese fully intact — no strings are removed, Chinese remains the fallback and the dictionary keys.

  • Auto-detection: browser language decides the initial locale (zh → Chinese, anything else → English).
  • Manual toggle: a button next to Refresh switches instantly; the choice persists via localStorage (dsh-usage-lang).
  • Reactive switching: language is React state (a small useLang() subscription). Toggling re-renders panels immediately and does not depend on any network request. SUBTABS / transient status messages / client-side validation errors are translated at render time, so even already-visible text follows the current locale.
  • Coverage: overview tables, usage calendar (+ hover tooltips), cache-hit list, price table, balance panel, CSV/JSON/PNG export dialogs, PNG canvas report.

Implementation notes

Piece Detail
Core zh strings double as dictionary keys; t(key) resolves via a 260-entry EN map (408 wrapped sites)
Balance panel host now emits semantic keys (labelKey/hintKey/meaningKey/sourceNoteKey/balanceLabelKey, credentialHintKey); both locales live client-side (tb())
Dates locale-aware dayLabel/fmtMonthLabel/dailyStatsTitle/weekdayLabels — zh keeps 2026年8月22日, en renders Aug 22, 2026, August 2026, Mo–Su headers
Missing credential error is built from technical credential names (未找到 DEEPSEEK_API_KEY,请配置后重试, errorCode: missing-credential) — internal keys can no longer leak into user-facing messages
Known limitation (intentional) external Conversation/Settings slot labels are registered once at boot, so they update after a page reload; noted in the toggle tooltip, both READMEs and CHANGELOG. Host-side log/error messages remain Chinese for now — proposed follow-up

Testing

node --test test/balance.test.js      # 12 passing
node --test test/client-i18n.test.js  # 7 passing

The new suite loads the real bundle through a stubbed ModuleLoader and covers: instant switch + storage persistence across mounts, date/weekday rendering, transient-message locales, balance-key coverage in both locales, and the missing-credential error shape.

Compatibility

  • No configuration changes; no breaking changes to stored data.
  • balanceProviders API field credentialHint was renamed to credentialHintKey (it now carries a semantic key by design).

中文

为 Web 面板新增轻量 i18n 层:完整保留中文,同时支持英文界面。

  • 按浏览器语言自动选择;「用量与消耗」头部切换按钮即时生效,选择保存在 localStorage
  • 语言切换完全响应式(React 状态 + 订阅),不依赖任何网络请求;SUBTABS、瞬态状态消息、客户端校验错误均在渲染时翻译
  • 余额查询改为语义化字段(host 不再返回中文展示文案),双语文案集中在客户端
  • 日期 / 星期本地化:中文保持 2026年8月22日,英文输出 Aug 22, 2026、August 2026 与 Mo–Su 表头
  • 缺失凭据错误改由技术凭据名生成,不再可能泄漏内部键
  • 已知限制(有意为之):外部标签页名在启动时注册,刷新页面后更新新语言;宿主端日志 / 错误消息本期保持中文,拟后续跟进

测试:test/balance.test.js(12 通过)+ 新增 test/client-i18n.test.js(7 通过)。


Commits:

  1. d2eb6e4 feat(i18n): English UI layer with auto-detection and manual toggle
  2. 028aa4a fix(i18n): address review — reactive language, semantic balance keys, locale dates
  3. this commit — round-two review fixes (credential error leak, reactive transient messages, docs, tests)

ayleen added 3 commits August 22, 2026 04:13
Add a lightweight i18n layer to lib/client.js so the whole web panel can be
displayed in English while keeping Chinese fully intact:

- zh strings stay as-is and double as dictionary keys; a 271-entry EN map
  covers all 408 wrapped string sites
- language is auto-detected from navigator.language (zh -> Chinese,
  otherwise English); a toggle button next to Refresh switches instantly
  and persists the choice in localStorage (dsh-usage-lang)
- PNG report, calendar hover tooltips and canvas tables are translated too;
  data-facing comparisons (e.g. the total-row key) intentionally stay raw
  so host-side values keep matching
- host-side (lib/index.js) log/error messages are unchanged in this pass;
  proposed as follow-up work

Verified: node --check passes; bundle loads through a stubbed ModuleLoader;
every t() call site resolves in the dictionary (0 missing).
… locale dates

Review follow-up on the initial i18n commit:

1. Language toggle is now fully reactive and network-independent:
   - module-level listener registry + useLang() hook (React state)
   - setLang() persists and notifies subscribers; panels re-render
     instantly even if /usage/api never resolves
   - SUBTABS hold raw zh keys and are translated at render time
     (also removes the t-shadowing inside the map callback)
   - BalancePanel subscribes too; external slots labels are frozen at
     registration by design and refresh on page reload (noted in the
     button tooltip and README)

2. Balance host responses carry semantic keys instead of Chinese
   presentation strings:
   - balance.js emits labelKey/hintKey/meaningKey/sourceNoteKey/
     balanceLabelKey (+ credentialHint keys); numeric values and API
     field names stay raw
   - both locales for those keys live in the client (__BAL_ZH/__BAL_EN
     rendered through tb())
   - successful payloads no longer contain CJK presentation text;
     covered by new unit tests (12 total, all passing)

3. Locale-aware dates and weekdays:
   - dayLabel/fmtMonthLabel/dailyStatsTitle/weekdayLabels helpers
   - zh keeps 2026年8月22日 style; en renders "Aug 22, 2026",
     "August 2026" and Mo..Su headers (Sunday no longer collides with
     the date-suffix key)

Verified: node --check on lib/*.js; node --test test/balance.test.js
(12 passing); every t()/tb() call site resolves in its dictionary;
bundle smoke-loads through a stubbed ModuleLoader.
…nt messages, docs

1. Missing-credential errors no longer leak internal hint keys:
   - PROVIDERS.credentialHint renamed to credentialHintKey (semantic)
   - index.js builds the message from technical credential names via a
     new missingCredentialError() helper in balance.js
     ("未找到 DEEPSEEK_API_KEY,请配置后重试", errorCode: missing-credential)

2. Transient UI messages are now reactive:
   - export/import/picker status messages and client-side validation
     errors are stored as semantic descriptors ({k}, {k,tail}, {seq},
     errorKey) and translated at render time through msgText()
   - switching language instantly re-renders existing status lines

3. READMEs state the slots-label reload caveat (synced with CHANGELOG
   and the toggle tooltip).

4. Tests & debug hook:
   - exports.__i18n (t/tb/msgText/setLang/date formatters)
   - new test/client-i18n.test.js (7 tests): instant switch + storage
     persistence across mounts, date/weekday rendering, transient
     message locales, balance key coverage in both locales,
     missing-credential error shape

All green: node --test test/balance.test.js (12), test/client-i18n.test.js (7);
node --check on lib/client.js, lib/balance.js, lib/index.js.
@feiyang-dev

Copy link
Copy Markdown
Owner

Thank you, my dear friend! Our team has wanted to add an English interface for a long time now. Thank you for your edits and additions. We'll keep creating more in the future

@feiyang-dev
feiyang-dev merged commit cdc2c14 into feiyang-dev:main Aug 27, 2026
feiyang-dev pushed a commit that referenced this pull request Aug 27, 2026
- PR #7 (@ayleen): English UI layer with reactive language switching; balance semantic keys
- PR #8 (@wuhuqif176): Bailian/Qwen Token Plan quota query in balance panel
- bump to v1.13.0; credit both contributors in README/README.zh/CHANGELOG
feiyang-dev pushed a commit that referenced this pull request Aug 27, 2026
feiyang-dev pushed a commit that referenced this pull request Aug 27, 2026
- PR #7 (@ayleen): English UI layer with reactive language switching; balance semantic keys
- PR #8 (@wuhuqif176): Bailian/Qwen Token Plan quota query in balance panel
- bump to v1.13.0; credit both contributors in README/README.zh/CHANGELOG
feiyang-dev pushed a commit that referenced this pull request Aug 27, 2026
feiyang-dev pushed a commit that referenced this pull request Aug 27, 2026
…een)

The login-only noreply form ([email protected]) is not resolved
to the ayleen account by GitHub's contributors graph. Use the id+login form so
both the repo Contributors graph and git blame attribute the PR #7 i18n commits
to @ayleen consistently.
feiyang-dev pushed a commit that referenced this pull request Aug 27, 2026
- PR #7 (@ayleen): English UI layer with reactive language switching; balance semantic keys
- PR #8 (@wuhuqif176): Bailian/Qwen Token Plan quota query in balance panel
- bump to v1.13.0; credit both contributors in README/README.zh/CHANGELOG
feiyang-dev pushed a commit that referenced this pull request Aug 27, 2026
feiyang-dev pushed a commit that referenced this pull request Aug 27, 2026
…een)

The login-only noreply form ([email protected]) is not resolved
to the ayleen account by GitHub's contributors graph. Use the id+login form so
both the repo Contributors graph and git blame attribute the PR #7 i18n commits
to @ayleen consistently.
@ayleen
ayleen deleted the feat/i18n-english branch August 27, 2026 15:02
@ayleen

ayleen commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

@feiyang-dev
Hi! Thanks for the tip and for merging the PR.
I’ve just added and verified the email on my GitHub account.
Best of luck with the continued development of your projects! 🚀

@feiyang-dev

Copy link
Copy Markdown
Owner

@feiyang-dev 嗨!谢谢你的提示和合并公关。 我刚刚在我的GitHub帐户上添加并验证了这封邮件。 祝你们的项目能继续发展!🚀

Thank you, my dear friend. If you find any issues here in the future, please let me know immediately, and I will address them when I have time. Thank you for your experience and support!

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