refactor: use langmap and langid instead of specified winlang

This commit is contained in:
2026-09-15 16:28:01 +08:00
parent 08d47c3ea8
commit b4dbd34c3b
4 changed files with 16 additions and 72 deletions
+5 -3
View File
@@ -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`. |
+10 -10
View File
@@ -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,
}
+1
View File
@@ -0,0 +1 @@
-59
View File
@@ -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)