Z-Sans ships with built-in support for English and Chinese (zh_CN / en), implemented on top of standard gettext (.po / .mo).
Directory Structure¶
i18n/
├── en/LC_MESSAGES/
│ ├── messages.po # Source strings
│ └── messages.mo # Compiled binary
└── zh_CN/LC_MESSAGES/
├── messages.po
└── messages.mo
Configuration¶
language:
default_language: en # zh_CN or en
supported_languages:
- zh_CN
- en
locale_dir: i18n # Language files directory
How It Works¶
main()first parses-c/--configwith a pre-parser to obtain the config file pathsetup_i18n(config)loads the language:- Reads the default language and supported list from the
languagesection of the config - Default language not in the supported list → warning and fallback to
zh_CN locale_diris resolved relative to the project root; directory does not exist → error and fallback toi18ngettext.translation('messages', ...)loads the language pack and callsinstall()- Falls back to English on failure
argparse._ = _: injects the translation function into argparse so built-in texts like-h/--helpgo through the same translation- Logs and prompts are all wrapped with
_()
Developer Usage¶
Use it in code:
After adding new strings, update the .po file and recompile it to .mo (tools such as Babel / msgfmt).
Querying the Current Language¶
The HTML report and the Web console UI language also follow this config:
- HTML report: zh prefix →
lang="zh-CN" - Web console:
GET /api/i18nreturns{lang, dict}(80+ frontend text keys), the frontend looks them up viat(key)