Internationalization
UI text is translated between Chinese and English through Qt's translation system (QTranslator + .ts / .qm); the documentation itself is bilingual via the mkdocs i18n plugin using the docs/zh and docs/en folders.
Internationalization in code
- Marking strings: wrap all user-visible text with
QCoreApplication.translate(context, text).contextis usually the page class name, which keeps entries grouped in the.tsfile. - Translation sources:
i18n/zh_CN.tsandi18n/en_US.tsare XML translation sources, editable with Qt Linguist or updatable via scripts. - Compiling to
.qm: uselreleaseto compile.tsinto binary.qmfiles. At runtimecore.settings.apply_language()loads the matching.qmand callsinstallTranslator. - Chinese as the source language: when the language is
zh_CNthe translator is uninstalled and the Chinese strings in source code are shown directly.
How the Expression Calculator is handled
The interface of the Expression Calculator (expr_calculator/) has three parts, each translated differently:
| Part | Approach |
|---|---|
| Qt widgets (toolbar, dialogs, status bar) | the module-local _t(text) (i.e. QCoreApplication.translate("expr_calculator", text)); the translation context is always expr_calculator |
| Text in constant tables (common equations, conditions, geometric quantities, transforms) | constant values are marked with QT_TRANSLATE_NOOP("expr_calculator", text) for extraction, and translated with _t() only when displayed, so switching to English never changes the Chinese keys used internally by the logic |
| HTML/JS page (canvas buttons, tooltips, empty-canvas hint) | text in the page template becomes @@TOKEN@@ placeholders plus a JSON blob injected as window.__I18N__; page_html() fills in the translations when the page is generated |
On a language switch, ui/main.py calls the tab's retranslateUi(widget); ui/expr.py forwards it to MainWindow.retranslate(), which refreshes the Qt widgets and reloads the page, after which _on_page_loaded() rebuilds the existing cards with their positions, titles, notes and folded state.
Note
The page templates (PAGE / DIALOG_PAGE) stay module-level string constants, but they must not be passed to setHtml directly — always run them through page_html(), otherwise the @@…@@ placeholders would be displayed verbatim.
Example
When adding a page, in the constructor:
python
btn.setText(QCoreApplication.translate("XinGongneng", "Compute"))
Then fill in the English translation in the corresponding <message> of i18n/en_US.ts.
Internationalization in docs
- Docs are split into two independent folders
docs/zh/(default) anddocs/en/with identical structure. mkdocs.ymluses thei18nplugin (docs_structure: folder) and builds a site per language based on thezh/ensubfolder name.- Chinese navigation labels are mapped to English through
nav_translations(declared under thelocale: enentry ofmkdocs.yml), so the navigation does not need to be duplicated underen/. - When adding a doc page, always create it in both
zh/anden/with the same filename and relative path.
Warning
After deleting or renaming a page under zh/, update en/ and the nav in mkdocs.yml as well, otherwise mkdocs build --strict will report broken links.