From b4dbd34c3b9c927dd02d0ef10863e08a32ea27a8 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Tue, 15 Sep 2026 16:28:01 +0800 Subject: [PATCH] refactor: use langmap and langid instead of specified winlang --- doc/rc-render-context.md | 8 +++-- src/metaglot/cmds/rc/render.py | 20 +++++------ src/metaglot/langmap/__init__.py | 1 + src/metaglot/winlang.py | 59 -------------------------------- 4 files changed, 16 insertions(+), 72 deletions(-) delete mode 100644 src/metaglot/winlang.py diff --git a/doc/rc-render-context.md b/doc/rc-render-context.md index 8e83e62..d82a54b 100644 --- a/doc/rc-render-context.md +++ b/doc/rc-render-context.md @@ -27,14 +27,16 @@ back to the manifest source string, which is typically English). The language of a PO file is resolved from its `Language:` header, falling back to the file name without extension; only values valid as Gettext `Language` fields are accepted. Languages without a Windows language mapping, two PO files describing the -same language, and PO entries unknown to the manifest are rejected. +same language, and PO entries unknown to the manifest are rejected. Languages +without a country resolve to a neutral language identifier (sublanguage `0x00`), +e.g. `en` resolves to `0x0009`. ## Language object | Property | Type | Description | |---------------|--------|-------------| -| `name` | string | Human-readable language name, e.g. `English (United States)`. | -| `langid` | string | Windows language identifier in hexadecimal, e.g. `0x409`. | +| `name` | string | The language tag of the PO file, e.g. `en_US`, `zh_CN`. | +| `langid` | string | Windows language identifier in hexadecimal, e.g. `0x0409`. | | `primary` | string | Windows primary language identifier in hexadecimal, e.g. `0x09`. | | `sublanguage` | string | Windows sublanguage identifier in hexadecimal, e.g. `0x01`. | | `block_key` | string | Eight hex digits combining `langid` and the Unicode code page, suitable as a `StringFileInfo` block key, e.g. `040904B0`. | diff --git a/src/metaglot/cmds/rc/render.py b/src/metaglot/cmds/rc/render.py index a0a4e9a..b4db38a 100644 --- a/src/metaglot/cmds/rc/render.py +++ b/src/metaglot/cmds/rc/render.py @@ -1,9 +1,9 @@ import logging -from ... import winlang from ...cli import RcRenderOpts from ...filters import register_general_filters, register_rc_filters -from ...langid import PoLang +from ...langid import PoLang, WinLcid +from ...langmap import win_lcid_map from ...manifest import Manifest, load_manifest from ...pofile import LanguagePack, resolve_glob_files, resolve_translations from ...render import create_environment, render @@ -16,21 +16,21 @@ constraint of the pipeline, not a configuration point. """ -def _lookup_windows_language(lang: PoLang) -> winlang.WindowsLanguage: +def _lookup_langid(lang: PoLang) -> WinLcid: try: - return winlang.lookup(lang.value) + return win_lcid_map.lookup(lang) except KeyError: raise ValueError(f"language '{lang}' has no Windows language mapping") from None def _language_context(pack: LanguagePack, manifest: Manifest) -> dict: - language = _lookup_windows_language(pack.lang) + lcid = _lookup_langid(pack.lang) return { - "name": language.name, - "langid": f"0x{language.langid:X}", - "primary": f"0x{language.primary:02X}", - "sublanguage": f"0x{language.sublanguage:02X}", - "block_key": f"{language.langid:04X}{_CODE_PAGE:04X}", + "name": pack.lang.value, + "langid": f"0x{lcid.value:04X}", + "primary": f"0x{lcid.primary:02X}", + "sublanguage": f"0x{lcid.sublanguage:02X}", + "block_key": f"{lcid.value:04X}{_CODE_PAGE:04X}", "code_page": f"{_CODE_PAGE}", "strings": pack.translations, } diff --git a/src/metaglot/langmap/__init__.py b/src/metaglot/langmap/__init__.py index e69de29..8b13789 100644 --- a/src/metaglot/langmap/__init__.py +++ b/src/metaglot/langmap/__init__.py @@ -0,0 +1 @@ + diff --git a/src/metaglot/winlang.py b/src/metaglot/winlang.py deleted file mode 100644 index ce8e6b5..0000000 --- a/src/metaglot/winlang.py +++ /dev/null @@ -1,59 +0,0 @@ -from dataclasses import dataclass - - -@dataclass(frozen=True) -class WindowsLanguage: - langid: int - name: str - - @property - def primary(self) -> int: - return self.langid & 0x3FF - - @property - def sublanguage(self) -> int: - return self.langid >> 10 - - -_LANGUAGES: dict[str, WindowsLanguage] = { - "en": WindowsLanguage(0x0409, "English (United States)"), - "ca": WindowsLanguage(0x0403, "Catalan (Catalan)"), - "de": WindowsLanguage(0x0407, "German (Germany)"), - "es": WindowsLanguage(0x0C0A, "Spanish (Spain)"), - "fr": WindowsLanguage(0x040C, "French (France)"), - "id": WindowsLanguage(0x0421, "Indonesian (Indonesia)"), - "it": WindowsLanguage(0x0410, "Italian (Italy)"), - "ja": WindowsLanguage(0x0411, "Japanese (Japan)"), - "ko": WindowsLanguage(0x0412, "Korean (Korea)"), - "nb_NO": WindowsLanguage(0x0414, "Norwegian Bokmal (Norway)"), - "nl": WindowsLanguage(0x0413, "Dutch (Netherlands)"), - "pa_PK": WindowsLanguage(0x0846, "Punjabi (Pakistan)"), - "ru": WindowsLanguage(0x0419, "Russian (Russia)"), - "si": WindowsLanguage(0x045B, "Sinhala (Sri Lanka)"), - "sl": WindowsLanguage(0x0424, "Slovenian (Slovenia)"), - "ta": WindowsLanguage(0x0449, "Tamil (India)"), - "tr": WindowsLanguage(0x041F, "Turkish (Turkey)"), - "uk": WindowsLanguage(0x0422, "Ukrainian (Ukraine)"), - "vi": WindowsLanguage(0x042A, "Vietnamese (Vietnam)"), - "zh_CN": WindowsLanguage(0x0804, "Chinese (Simplified, China)"), -} - -_ALIASES: dict[str, str] = { - "en_US": "en", - "en_GB": "en", - "zh": "zh_CN", - "zh_Hans": "zh_CN", - "zh_Hans_CN": "zh_CN", - "nb": "nb_NO", - "no": "nb_NO", - "pa": "pa_PK", -} - - -def lookup(locale: str) -> WindowsLanguage: - normalized = locale.replace("-", "_") - if normalized in _LANGUAGES: - return _LANGUAGES[normalized] - if normalized in _ALIASES: - return _LANGUAGES[_ALIASES[normalized]] - raise KeyError(locale)