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 i18n

Six 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 | None

Parameters

ParameterTypeRequiredDefaultDescription
keystrYesA language-independent key
languagestrNo"zh-CN"Target language

Covering 260 keys across twelve categories:

CategoryCountExamples
Stars162ziweiMaj, changsheng, yunlu
Palaces (including the body palace and the palace of origin)14soulPalace, wealthPalace, bodyPalace, originalPalace
Heavenly stems10jiaHeavenly
Earthly branches12ziEarthly
Brightness7miao, wang
Mutagens4sihuaLu
Five elements class5water2nd
Gender2male, female
Chinese zodiac12rat, ox
Hours13earlyRatHour
Zodiac signs12aries
Horoscope scopes7decadal, 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
None

key_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 | None

Parameters

ParameterTypeRequiredDefaultDescription
textstrYesA translation in any supported language
key_filterstr | NoneNoNoneA 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
None

Translations 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']
decadal

To 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

紫微 emperor

The *_key fields of the two charts are identical, so any key-based predicate gives the same answer on both.

On this page