ludic/packages/ludic.i18n/use.ludic

61 lines
2.1 KiB
Text

# use.ludic - choosing a language: its file loaded into the lookups, and the font atlas it names
export function i18n_use(i18n_st: mut I18nState, code: string) -> void {
if i18n_st.codes == null { i18n_init(i18n_st) }
i18n_st.on = false
i18n_st.cur = "en"
kc_clear(i18n_st)
var k = i18n_index(i18n_st, code)
let forced = I18nWorld.forced()
if len(forced) > 0 { k = i18n_index(i18n_st, forced) }
var font = ""
if k > 0 and tables_have(i18n_st, k) {
tables_put(i18n_st, i18n_st.tabs[k])
i18n_st.on = true
i18n_st.cur = i18n_st.codes[k]
font = i18n_st.fonts[k]
} else if k > 0 {
let text = Fs.read_text(i18n_st.paths[k])
if text != null {
i18n_load_text(i18n_st, text)
tables_file(i18n_st, k)
i18n_st.on = true
i18n_st.cur = i18n_st.codes[k]
font = i18n_st.fonts[k]
}
}
# the atlas follows the language, and is only reloaded when it actually changes
if len(font) == 0 { font = i18n_st.font_default }
if I18nWorld.font_ready() and not (font == i18n_st.font_now) {
if I18nWorld.set_font(font) { i18n_st.font_now = font } else if not (i18n_st.font_now == i18n_st.font_default) {
I18nWorld.set_font(i18n_st.font_default)
i18n_st.font_now = i18n_st.font_default
}
}
}
# a .po's text as the lookups, whatever the language is called (a test, a file from elsewhere)
@alloc_ok("on a language change: the language's messages read in")
export function i18n_load_text(i18n_st: mut I18nState, text: string) -> void {
let es = i18n_parse(text)
kc_clear(i18n_st)
i18n_st.exact = Dict.new()
i18n_st.out = new []string
i18n_st.plural = Dict.new()
i18n_st.forms = new []string
i18n_st.plural_rule = plural_rule_of(i18n_header(text, "Plural-Forms"))
for i in 0 .. len(es) {
let e = es[i]
if len(e.plural) > 0 { plural_add(i18n_st, e) } else if not (blank(e.id) or blank(e.str)) {
Dict.set(i18n_st.exact, e.id, len(i18n_st.out))
push(i18n_st.out, e.str)
}
}
i18n_st.on = true
}
# English again: nothing loaded, nothing translated
export function i18n_off(i18n_st: mut I18nState) -> void {
kc_clear(i18n_st)
i18n_st.on = false
i18n_st.cur = "en"
}