ludic/packages/ludic.i18n/tests/keys_test.ludic

99 lines
6.1 KiB
Text

# keys_test.ludic - keys where English was: en.po as the base under every language, a key's text from the
# language in use else English else the key ([[key]] in a developer's build), holes filled by arguments
# that may be keys themselves, plural forms by each language's rule, and the old English form beside them
import "ludic.i18n"
program I18nKeysTest {
numbers float
function q(s: string) -> string { return "\"" + s + "\"" }
function po_line(id: string, tr: string) -> string { return "msgid " + q(id) + "\nmsgstr " + q(tr) + "\n\n" }
function plural(id: string, one: string, many: string, three: string) -> string {
var s = "msgid " + q(id) + "\nmsgid_plural " + q(id) + "\nmsgstr[0] " + q(one) + "\nmsgstr[1] " + q(many) + "\n"
if len(three) > 0 { s = s + "msgstr[2] " + q(three) + "\n" }
return s + "\n"
}
function en() -> string {
var s = "msgid \"\"\nmsgstr \"\"\n\"Plural-Forms: nplurals=2; plural=(n != 1);\\n\"\n\n"
s = s + po_line("pause.resume", "Resume") + po_line("pause.quit", "Quit") + po_line("hud.day", "Day {1}")
s = s + po_line("species.elk.a_name", "an elk") + po_line("catch.saw", "You saw {1} at {2}.")
return s + plural("catch.count", "{1} trout", "{1} trout", "") + plural("pack.items", "{1} item", "{1} items", "")
}
# Turkish-shaped, with a Russian-shaped plural so the third form is chosen by the rule
function tt() -> string {
var s = "msgid \"\"\nmsgstr \"\"\n\"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : 2);\\n\"\n\n"
s = s + po_line("pause.resume", "Devam") + po_line("hud.day", "{1}. Gun") + po_line("species.elk.a_name", "bir geyik")
s = s + po_line("catch.saw", "{2} saatinde {1} gordun.") + po_line("Settings", "Ayarlar") + po_line("Day {1}", "{1}. gun")
return s + plural("pack.items", "{1} esya", "{1} esyaa", "{1} esyalar")
}
function start (i18n_st: mut I18nState) -> void {
i18n_off(i18n_st)
i18n_loud(i18n_st, false)
i18n_base_text(i18n_st, en())
}
test "a key is its English under en.po, its own language's text when there is one, else English, else itself" (i18n_st: mut I18nState) {
start (i18n_st)
expect(L(i18n_st, tr(k"pause.resume")) == "Resume")
expect(L(i18n_st, tr(k"pause.quit")) == "Quit")
i18n_load_text(i18n_st, tt())
expect(L(i18n_st, tr(k"pause.resume")) == "Devam")
expect(L(i18n_st, i18n_key("pause.quit")) == "Quit") # the language lacks it: English
expect(L(i18n_st, i18n_key("nowhere.at_all")) == "nowhere.at_all") # en.po lacks it: the key
i18n_loud(i18n_st, true)
expect(L(i18n_st, i18n_key("nowhere.else")) == "[[nowhere.else]]") # a developer's build says so
i18n_off(i18n_st)
expect(L(i18n_st, tr(k"pause.resume")) == "Resume") # back to English: nothing stale kept
}
test "holes take their arguments in the language's order, and an argument that is a key is made first" (i18n_st: mut I18nState) {
start (i18n_st)
expect(L(i18n_st, trf(k"hud.day", "12", "", "", "")) == "Day 12")
let saw = trf(k"catch.saw", tr(k"species.elk.a_name"), "5:30", "", "")
expect(L(i18n_st, saw) == "You saw an elk at 5:30.")
i18n_load_text(i18n_st, tt())
expect(L(i18n_st, trf(k"hud.day", "12", "", "", "")) == "12. Gun")
expect(L(i18n_st, saw) == "5:30 saatinde bir geyik gordun.")
expect(L(i18n_st, trf(k"hud.day", "", "", "", "")) == ". Gun") # a missing argument is empty
let nested = trf(k"catch.saw", trf(k"hud.day", "3", "", "", ""), "5:30", "", "") # a key with holes inside a hole
expect(L(i18n_st, nested) == "5:30 saatinde 3. Gun gordun.")
expect(key_text(saw) == "catch.saw" and key_text("Settings") == "Settings")
}
test "a plural key takes its form by the count, by each language's own rule, falling back to English's" (i18n_st: mut I18nState) {
start (i18n_st)
expect(L(i18n_st, trn(kn"pack.items", 1, "", "", "")) == "1 item")
expect(L(i18n_st, trn(kn"pack.items", 3, "", "", "")) == "3 items")
i18n_load_text(i18n_st, tt())
expect(L(i18n_st, trn(kn"pack.items", 21, "", "", "")) == "21 esya") # n%10 == 1
expect(L(i18n_st, trn(kn"pack.items", 5, "", "", "")) == "5 esyalar")
expect(L(i18n_st, trn(kn"catch.count", 2, "", "", "")) == "2 trout") # the language lacks it
}
test "the old English form still works beside keys, so a game can move over a file at a time" (i18n_st: mut I18nState) {
start (i18n_st)
expect(L(i18n_st, "Settings") == "Settings")
i18n_load_text(i18n_st, tt())
expect(L(i18n_st, "Settings") == "Ayarlar")
expect(L(i18n_st, tr(k"pause.resume")) == "Devam")
expect(not i18n_is_key("Settings") and i18n_is_key(i18n_key("x")) and i18n_is_key(i18n_key_plural("x")))
}
test "an English value handed into a key's hole still gets its old English line, until it is a key itself" (i18n_st: mut I18nState) {
start (i18n_st)
expect(L(i18n_st, trf(k"catch.saw", "Settings", "5:30", "", "")) == "You saw Settings at 5:30.")
i18n_load_text(i18n_st, tt())
expect(L(i18n_st, trf(k"catch.saw", "Settings", "5:30", "", "")) == "5:30 saatinde Ayarlar gordun.") # exact
expect(L(i18n_st, trf(k"catch.saw", "Day 3", "5:30", "", "")) == "5:30 saatinde 3. gun gordun.") # a pattern
expect(L(i18n_st, trf(k"catch.saw", "Nowhere known", "7", "", "")) == "7 saatinde Nowhere known gordun.") # no line: as it is
}
test "a key's text glued into an English line is made where it sits, until the line is a key of its own" (i18n_st: mut I18nState) {
start (i18n_st)
let elk = tr(k"species.elk.a_name")
expect(L(i18n_st, "Saw " + elk + ", 30 cm.") == "Saw an elk, 30 cm." and L(i18n_st, "Saw " + elk + ".") == "Saw an elk.")
expect(L(i18n_st, elk + ": 30 cm") == "an elk: 30 cm" and L(i18n_st, elk + "\n" + tr(k"pause.quit")) == "an elk\nQuit")
expect(L(i18n_st, trf(k"catch.saw", "Seen: " + elk, "5:30", "", "")) == "You saw Seen: an elk at 5:30.")
i18n_load_text(i18n_st, tt())
expect(L(i18n_st, "Day " + tr(k"pause.resume")) == "Devam. gun") # the name made, then the line's own pattern
expect(L(i18n_st, trf(k"catch.saw", elk, "5:30", "", "")) == "5:30 saatinde bir geyik gordun.") # a key whole is untouched
}
}