Translation
Two-way lookup between keys and translations.
Every field on a chart already carries both a translation and a *_key, so manual translation is
usually unnecessary. These functions exist for the cases where you have only a key (or only a
translation in some language) and need to convert.
from x_iztro import i18nSix languages are supported: zh-CN, zh-TW, en-US, ja-JP, ko-KR, vi-VN.
translate
Purpose Translate any key into a given language.
Signature
def translate(key: str, language: LanguageType = "zh-CN") -> str | NoneParameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
key | str | Yes | — | A language-independent key |
language | str | No | "zh-CN" | Target language |
Covering 260 keys across twelve categories:
| Category | Count | Examples |
|---|---|---|
| Stars | 162 | ziweiMaj, changsheng, yunlu |
| Palaces (including the body palace and the palace of origin) | 14 | soulPalace, wealthPalace, bodyPalace, originalPalace |
| Heavenly stems | 10 | jiaHeavenly |
| Earthly branches | 12 | ziEarthly |
| Brightness | 7 | miao, wang |
| Mutagens | 4 | sihuaLu |
| Five elements class | 5 | water2nd |
| Gender | 2 | male, female |
| Chinese zodiac | 12 | rat, ox |
| Hours | 13 | earlyRatHour |
| Zodiac signs | 12 | aries |
| Horoscope scopes | 7 | decadal, turn |
Return value The translation; None for an unknown key.
Example
print(i18n.translate("ziweiMaj", "en-US"))
print(i18n.translate("soulPalace", "ja-JP"))
print(i18n.translate("ziweiMaj", "vi-VN"))
print(i18n.translate("bodyPalace", "en-US"))
print(i18n.translate("nosuch"))Output
emperor
命宮
Tử Vi
body
Nonekey_of
Purpose Reverse-look-up a key from a translation in any language.
Signature
def key_of(text: str, key_filter: str | None = None) -> str | NoneParameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
text | str | Yes | — | A translation in any supported language |
key_filter | str | None | No | None | A substring the key name must contain, for disambiguating homographic translations |
Return value The key; None when nothing matches.
Example
print(i18n.key_of("紫微"))
print(i18n.key_of("emperor"))
print(i18n.key_of("자미"))
print(i18n.key_of("no such name"))Output
ziweiMaj
ziweiMaj
ziweiMaj
NoneTranslations in all three languages resolve to the same key.
Edge cases and pitfalls
all_keys
Purpose Get all 260 translatable keys.
Signature
def all_keys() -> list[str]Return value A list of keys, in the order key_of scans them: horoscope scopes, Chinese zodiac,
hours, zodiac signs, five elements classes, heavenly stems, earthly branches, brightness, mutagens,
stars, palaces, gender — matching the merge order of iztro's per-language translation files.
Example
keys = i18n.all_keys()
print(len(keys))
print(keys[:4])
print(i18n.translate(keys[0], "en-US"))Output
260
['decadal', 'childhood', 'yearly', 'monthly']
decadalTo iterate the keys of one category, the enums in enums or data.constants() are simpler.
There is no global language switch
x-iztro keeps no global "current language" state: the language is passed as a parameter when charting, and translation functions name their target language explicitly on every call.
Why
A global language switch makes the same code produce different results depending on call order, which is especially dangerous with multiple threads. Passing it explicitly means a call's result depends only on its arguments.
To emit several languages within one process, just chart several times; they do not interfere:
zh = Astro().by_solar("2000-8-16", 2, "female")
en = Astro().by_solar("2000-8-16", 2, "female", language="en-US")
print(zh.palace("soulPalace").major_stars[0].name,
en.palace("soulPalace").major_stars[0].name)Output
紫微 emperorThe *_key fields of the two charts are identical, so any key-based predicate gives the same answer
on both.