90 lines
2.9 KiB
Text
90 lines
2.9 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.cache = Dict.new()
|
|
i18n_st.cache_out = new []string
|
|
i18n_st.caps = new []string
|
|
for c in 0 .. CAPS { push(i18n_st.caps, "") }
|
|
i18n_st.plural = Dict.new()
|
|
i18n_st.forms = new []string
|
|
i18n_st.plural_rule = plural_rule_of(i18n_header(text, "Plural-Forms"))
|
|
var np = 0
|
|
var nlit = 0
|
|
for i in 0 .. len(es) {
|
|
if blank(es[i].id) or blank(es[i].str) { continue }
|
|
let h = holes(es[i].id)
|
|
if h > 0 {
|
|
np += 1
|
|
nlit += h + 1
|
|
}
|
|
}
|
|
i18n_st.pat_out = new []string
|
|
i18n_st.lits = new []string
|
|
i18n_st.pat_l0 = words(np + 1)
|
|
i18n_st.pat_nl = words(np + 1)
|
|
i18n_st.pat_sc = words(np + 1)
|
|
i18n_st.pat_ph = words(nlit + 1)
|
|
i18n_st.nx_p = words(np + 1)
|
|
i18n_st.nx_s = words(np + 1)
|
|
i18n_st.nx_g = words(np + 1)
|
|
i18n_st.pfx = Dict.new()
|
|
i18n_st.sfx = Dict.new()
|
|
i18n_st.gen_head = 0
|
|
i18n_st.pat_n = 0
|
|
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)) {
|
|
if holes(e.id) > 0 { add_pattern(i18n_st, e.id, e.str) } else {
|
|
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"
|
|
}
|