Skip to content

Pull Request: Add i18n Infrastructure and Chinese (zh-CN) Localization#554

Open
smryyyyy wants to merge 1 commit into
tmustier:mainfrom
smryyyyy:main
Open

Pull Request: Add i18n Infrastructure and Chinese (zh-CN) Localization#554
smryyyyy wants to merge 1 commit into
tmustier:mainfrom
smryyyyy:main

Conversation

@smryyyyy
Copy link
Copy Markdown

Pull Request: Add i18n Infrastructure and Chinese (zh-CN) Localization

English | 中文


Summary

This PR adds a lightweight internationalization (i18n) system and complete Simplified Chinese (zh-CN) translation to Pi for Excel.

Motivation: Pi for Excel has a growing user base in China. Previously all UI text was hardcoded in English, making it inaccessible to non-English speakers. This PR introduces a translation framework with zero third-party dependencies and provides a complete Chinese localization out of the box.


Architecture

Translation System (src/language/)

A minimal, zero-dependency translation function that avoids adding any bundle overhead:

src/language/
├── index.ts           ← t() + initLanguage() + getLanguage()
└── locales/
    ├── en.json        ← English (source of truth, ~900 keys)
    └── zh-CN.json     ← Chinese translations (~900 keys)

Design decisions:

  • No i18n library — keeps bundle size zero for Office WebView
  • English as fallback — untranslated keys silently display the English string
  • t("key", {vars}) — variable interpolation via {placeholder} syntax
  • Language stored in SettingsStore — persisted across sessions
  • Page reload on switch — simple, no runtime reactivity needed

API

import { t, initLanguage, getLanguage } from "../language/index.js";

// Basic usage
element.textContent = t("welcome.subtitle");

// Variable interpolation  (JSON: "{label} connected")
element.textContent = t("welcome.connected", { label: "DeepSeek" });

// Initialize at startup
initLanguage("zh-CN");

// Get current language
const lang = getLanguage(); // "en" | "zh-CN"

Language Switching

Two entry points for switching language:

  1. Welcome screen — Language toggle (English / 中文) at top-right corner
  2. Settings → Advanced → Language — Dropdown selector

Both save the preference to settings.language and reload the page.


Changes by Category

Core Infrastructure (4 new files)

  • src/language/index.ts — Translation function
  • src/language/locales/en.json — English strings
  • src/language/locales/zh-CN.json — Chinese strings
  • cn_README.md — Chinese documentation

Modified Files (74 files)

Area Files Description
UI Components 15 Sidebar, input, working indicator, whimsical messages, dialogs, loading, toast, disclosure bar, proxy banner, bridge/web-search cards, message renderers, file dialogs
Taskpane 8 Welcome/login, status bar, session title, queue display, bootstrap, init, status context, status popovers
Commands 13 Model, settings, session, export, help, files, skills, addons, tools, debug, experimental, clipboard
Dialogs & Overlays 12 Settings, rules, shortcuts, resume, recovery, experimental, extensions hub (overlay + connections + plugins + skills), custom gateway
Backend 6 Execution mode, runtime manager, permissions, flags, thinking duration, relative date
Files 3 Backend, workspace, file dialog status
HTML 5 taskpane.html, ui-gallery.html, oauth-callback.html, index.html, architecture/index.html
Tools 2 Tool renderers, get-workbook-overview
Other 2 Workbook context, README.md

Key Fixes

  1. Module-level t() timing bug — Several files had t() calls in module-level const declarations that executed before initLanguage() was called. Fixed by converting to lazy getter functions:

    • src/experiments/flags.tsEXPERIMENTAL_FEATURESgetFeatures()
    • src/commands/builtins/settings-overlay.tsSETTINGS_TABSgetSettingsTabs()
    • src/commands/builtins/shortcuts-overlay.tsSHORTCUT_GROUPSgetShortcutGroups()
    • src/ui/confirm-dialog.ts, src/ui/text-input-dialog.ts — Error constants → getter functions
    • src/taskpane/status-context.ts — Context constants → getter functions
    • src/taskpane/status-popovers.ts — Level labels/hints → getter functions
    • src/extensions/permissions.ts — Capability descriptors → getter function
  2. Missing imports — Added import { t } where it was missing:

    • src/ui/files-dialog-filtering.ts
    • src/ui/files-dialog.ts
    • src/ui/files-dialog-status.ts
    • src/compat/thinking-duration.ts
    • src/workbook/context.ts
    • src/commands/builtins/rules-overlay.ts
  3. Hardcoded relative datesrc/commands/builtins/overlay-relative-date.ts and src/ui/files-dialog.ts had hardcoded "just now", "m ago", "h ago" strings → made translatable

  4. Execution mode toastsrc/execution/controller.ts had `${label} mode.` hardcoded → made translatable


Testing

  • npm run dev — Dev server starts successfully (HTTP 200)
  • git diff --stat — 78 files changed, 3365 insertions, 1144 deletions
  • Pre-existing lint errors (22 in files-dialog-actions.ts and render-csv-table.ts) are upstream issues, not introduced here

Manual Verification

  1. Load the taskpane → welcome screen shows in English (default)
  2. Click "中文" button → page reloads → UI switches to Chinese
  3. Settings → Advanced → Change language → works both ways
  4. All major UI sections verified: sidebar, settings, dialogs, shortcuts, extensions hub, file dialog
  5. Error toasts show Chinese prefix where applicable

Adding a New Language

Adding another language requires only:

  1. Create src/language/locales/{lang}.json with all keys translated
  2. Import it in src/language/index.ts
  3. Add the option to the language selectors

Screenshots

(Add screenshots showing: English welcome page, Chinese welcome page, language selector in Settings)


中文版本

摘要

此 PR 为 Pi for Excel 添加了轻量级国际化(i18n)系统和完整的简体中文(zh-CN)翻译。

动机: Pi for Excel 在中国拥有日益增长的用户群。此前所有 UI 文本均硬编码为英文,非英语用户无法直接使用。此 PR 引入了一个零第三方依赖的翻译框架,并提供开箱即用的完整中文本地化。

架构

翻译系统位于 src/language/,包含:

  • index.tst() 翻译函数、initLanguage() 初始化、getLanguage() 获取当前语言
  • locales/en.json — 英文原文(约 900 条,作为兜底)
  • locales/zh-CN.json — 中文翻译(约 900 条)

设计原则:不引入任何 i18n 第三方库,对 Office WebView 保持零包体积增长。英文为兜底语言,未翻译的 key 自动显示英文原文。

语言切换

两种方式:

  1. 欢迎页右上角的 English / 中文切换按钮
  2. 设置 → 高级 → 语言 下拉选择框

切换后保存到 settings.language 并自动刷新页面。

文件变更

  • 4 个新文件:翻译系统 + 中文文档
  • 74 个修改文件:覆盖全部 UI 组件、对话框、命令、后端逻辑

关键修复

  • 修复了模块级 t() 调用在 initLanguage() 之前执行的时序问题(7 处)
  • 修复了多处缺少 import { t } 导致的运行时错误
  • 将硬编码的日期格式化字符串("just now"、"5m ago" 等)改为可翻译

- Add lightweight translation system (src/language/) with t() function,
  zero external dependencies
- Add en.json and zh-CN.json covering ~900 UI strings
- Convert 60+ source files from hardcoded English to t() calls
- Add language selector on welcome page and in Settings
- Fix module-level t() timing issues with lazy initialization
- Translate welcome page, sidebar, settings, dialogs, commands, toasts,
  shortcuts, extensions hub, file dialogs, and status bar
- Add cn_README.md with full Chinese documentation
- Update HTML lang attributes to zh-CN
- Fix pre-commit lint errors in settings-overlay.ts
- Default language is English, switchable to Chinese
@vercel
Copy link
Copy Markdown

vercel Bot commented May 28, 2026

@smryyyyy is attempting to deploy a commit to the tmustier's projects Team on Vercel.

A member of the Team first needs to authorize it.

@smryyyyy
Copy link
Copy Markdown
Author

Simply put
I have used a Chinese version for myself
Maybe you can add them together?
ovo

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.

1 participant