Skip to content

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). context is usually the page class name, which keeps entries grouped in the .ts file.
  • Translation sources: i18n/zh_CN.ts and i18n/en_US.ts are XML translation sources, editable with Qt Linguist or updatable via scripts.
  • Compiling to .qm: use lrelease to compile .ts into binary .qm files. At runtime core.settings.apply_language() loads the matching .qm and calls installTranslator.
  • Chinese as the source language: when the language is zh_CN the 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) and docs/en/ with identical structure.
  • mkdocs.yml uses the i18n plugin (docs_structure: folder) and builds a site per language based on the zh / en subfolder name.
  • Chinese navigation labels are mapped to English through nav_translations (declared under the locale: en entry of mkdocs.yml), so the navigation does not need to be duplicated under en/.
  • When adding a doc page, always create it in both zh/ and en/ 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.