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.

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

func Translate(key string, language Language) (string, error)

Parameters

ParameterTypeRequiredDefaultDescription
keystringYesA language-independent key
languageLanguageYesTarget 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; an unknown key returns an empty string, not an error.

Example

a, _ := iztro.Translate(iztro.StarZiweiMaj, iztro.LanguageEnUS)
b, _ := iztro.Translate(iztro.PalaceSoul, iztro.LanguageJaJP)
c, _ := iztro.Translate("nosuch", iztro.LanguageZhCN)

fmt.Printf("%q %q %q\n", a, b, c)

Output

"emperor" "命宮" ""

Edge cases and pitfalls

An unknown key returns an empty string, not an error

A key that cannot be found is not an exception; it returns an empty string. To tell "the translation happens to be empty" from "the key does not exist", confirm the key belongs to one of the categories in the table above.


KeyOf

Purpose Reverse-look-up a key from a translation in any language.

Signature

func KeyOf(text string) (string, error)
func KeyOfIn(text, keyFilter string) (string, error)

Parameters

ParameterTypeRequiredDefaultDescription
textstringYesA translation in any supported language
keyFilterstringYesA substring the key name must contain, for disambiguating homographic translations

Return value The key; an empty string when nothing matches.

Example

a, _ := iztro.KeyOf("紫微")
b, _ := iztro.KeyOf("emperor")
c, _ := iztro.KeyOf("자미")
d, _ := iztro.KeyOf("no such name")

fmt.Printf("%q %q %q %q\n", a, b, c, d)

Output

"ziweiMaj" "ziweiMaj" "ziweiMaj" ""

Translations in all three languages resolve to the same key.

Edge cases and pitfalls


AllKeys

Purpose Get all 260 translatable keys.

Signature

func AllKeys() ([]string, error)

Return value A slice of keys, in the order KeyOf 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, _ := iztro.AllKeys()
first, _ := iztro.Translate(keys[0], iztro.LanguageEnUS)

fmt.Println(len(keys), keys[:4], first)

Output

260 [decadal childhood yearly monthly] decadal

To iterate the keys of one category, the constants in keys.go or GetConstants() 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 under concurrency. 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, _ := iztro.BySolar("2000-8-16", 2, iztro.GenderFemale, true, iztro.LanguageZhCN, nil)
en, _ := iztro.BySolar("2000-8-16", 2, iztro.GenderFemale, true, iztro.LanguageEnUS, nil)

fmt.Println(zh.Palace(iztro.PalaceSoul).MajorStars[0].Name,
    en.Palace(iztro.PalaceSoul).MajorStars[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