Skip to content

fix: set i18next default namespace so the desktop app can render - #159

Merged
jamliaoo merged 2 commits into
mainfrom
fix/i18n-default-namespace
Oct 4, 2026
Merged

jamliaoo merged 2 commits into
mainfrom
fix/i18n-default-namespace

Conversation

@jamliaoo

@jamliaoo jamliaoo commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator

Summary

打包後的桌面版(Electron)啟動後視窗一片空白。原因是 i18next 初始化時沒有設定 ns / defaultNS,除了各元件指定的 namespace 之外,還會去載入內建預設的 translation,而 public/locales/*/translation.json 從未存在。

  • 網頁版:缺檔回 404,i18next 警告一次後放棄,畫面正常。
  • 桌面版:electron-serve 的 app:// 協定把缺檔回報成網路錯誤(ERR_FILE_NOT_FOUND),i18next-http-backend 視為可重試而不斷重試,namespace 永遠載不完,react-i18next 的 Suspense 不會 resolve,#root 一直是空的。

Changes

src/lib/i18n.js:init 加上 ns: ['common'] 與 defaultNS: 'common',把預設 namespace 指到兩個語系都有的 common.json。

所有 useTranslation(...)(12 處)與 i18n.t(..., { ns })(2 處)都已明確指定 namespace,沒有任何呼叫依賴預設值,所以不會改變任何翻譯查找結果。

Verification

以同一支 CDP 探針對打包後的 app(macOS arm64,全新 user data 目錄)輪詢 20 秒:

畫面 translation.json 請求
修正前(main, v2.1.1) 始終空白,#root 無子元素 22 次,全部失敗
修正後 正常渲染編輯器與標題 0 次
  • 網頁版(vite preview):zh-TW 與 ?locale=en 的標題、按鈕、aria-label 皆正確翻譯,不再請求 translation.json,console 無警告。
  • npm run lint、npm run format:check、npm test(8/8)通過。
  • npm run build、npx electron-forge package 成功。

Notes

  • 這是既有問題,i18n.js 自 2024-01 起未變動,與 Vite 遷移無關;v2.1.1 的 Windows 安裝檔是同一份設定。Windows(Squirrel)安裝版未實機驗證,建議合併後發版時在 Windows 上確認一次。
  • 未包含在本 PR:debug: true 在正式版仍開啟,console 會輸出大量 i18next log,可另外改成只在開發模式啟用。

i18next was initialised without ns/defaultNS, so besides the namespaces each
component asks for it also requested its built-in default, translation, which
has no file under public/locales.

On the web the miss is a 404 and i18next gives up after one warning. In the
packaged Electron app, electron-serve's app:// protocol reports the missing
file as a network failure, so i18next-http-backend keeps retrying, the
namespace never settles, and react-i18next's Suspense never resolves: the
window stays blank.

Point the default namespace at common, which exists for both locales. Every
useTranslation / i18n.t call already passes an explicit namespace, so no
lookup changes.
@jamliaoo
jamliaoo merged commit 2c746af into main Oct 4, 2026
1 check passed
@jamliaoo
jamliaoo deleted the fix/i18n-default-namespace branch October 4, 2026 09:47
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