# gander languages: a game's text in the player's language A game's text in several languages: one JSON file per language in assets/lang/, `gander::tr(key)` where the text is drawn, plural forms, numbers written each language's way, a Language tab in Settings, and the fonts each language needs (gander build makes them). The engine's own menus, settings, save screens and score tables come translated into 18 languages already. A game adds its own text. ## The files One file per language, named by its code, in `assets/lang/`: ``` assets/lang/en.json English (the source language unless lang_source says otherwise) assets/lang/fr.json French assets/lang/ja.json Japanese assets/lang/pt-BR.json Portuguese as spoken in Brazil assets/lang/zh-Hans.json Chinese, simplified characters ``` Codes gander knows (the file name must be one of these, or the engine never finds it): `en en-GB en-US fr fr-CA de es es-419 es-MX it pt pt-BR pt-PT nl sv da nb no fi is pl cs sk hu ro bg el ru uk be kk tr et lv lt hr sr sl ca eu gl id ms vi fil sw af th ja ko zh-Hans zh-Hant zh-CN zh-TW` (and `ar he fa hi`, which draw unshaped: see Limits). A file is keys to text. Nested objects join their names with dots, so these two are the same key, `menu.play`: ```json { "menu": { "play": "Jouer", "quit": "Quitter" } } { "menu.play": "Jouer", "menu.quit": "Quitter" } ``` A whole game's text, English (`assets/lang/en.json`): ```json { "title": "Lantern Keeper", "menu": { "tap_to_start": "Tap to start" }, "hud": { "score": "Score: {0}", "lanterns": { "one": "{n} lantern lit", "other": "{n} lanterns lit" } }, "dialogue": { "keeper_1": "The ships come home at dawn.", "keeper_2": "Light the harbour lantern first, {0}." }, "over": { "win": "Every lantern burns", "lose": "The fog took the harbour" } } ``` and French (`assets/lang/fr.json`), same keys: ```json { "title": "Le Gardien des lanternes", "menu": { "tap_to_start": "Touchez pour commencer" }, "hud": { "score": "Score : {0}", "lanterns": { "one": "{n} lanterne allumée", "other": "{n} lanternes allumées" } }, "dialogue": { "keeper_1": "Les navires rentrent à l'aube.", "keeper_2": "Allume d'abord la lanterne du port, {0}." }, "over": { "win": "Toutes les lanternes brûlent", "lose": "Le brouillard a pris le port" } } ``` ## Drawing text ```goose // in frame(): call tr where the text is drawn, every frame gander::text(gander::DEFAULT_FONT, gander::tr("title"), 40.0, 40.0, 48.0, gander::WHITE, gander::ALIGN_LEFT); let s = gander::text(gander::DEFAULT_FONT, gander::tr("hud.score", gander::number(score as f64)), 20.0, 20.0, 24.0, gander::WHITE, gander::ALIGN_LEFT); let l = gander::text(gander::DEFAULT_FONT, gander::tr_n("hud.lanterns", lit), 20.0, 50.0, 24.0, gander::YELLOW, gander::ALIGN_LEFT); if gander::flow_update(flow, gander::tr("title"), gander::tr("menu.tap_to_start")) { ... } gander::flow_game_over(flow, gander::tr("over.win"), gander::tr("hud.score", str(score))); let k = gander::dialogue(gander::tr("names.keeper"), gander::tr("dialogue.keeper_2", player_name)); ``` | Call | Does | | --- | --- | | `tr(key) -> u8[>..]` | the key's text in the current language | | `tr(key, a, b, c, d)` | the same with `{0}` `{1}` `{2}` `{3}` filled in (text: `str(x)` for a value) | | `tr_n(key, n: i64, a, b)` | the plural form for count `n` (below); `{n}` is `n` written the language's way, `{0}` `{1}` are `a`, `b` | | `tr_list("menu.play,menu.quit")` | each key's text, comma joined: menu items for `main_menu`, `ui_segmented` | | `tr_has(key) -> bool` | some file has the key | | `number(v: f64, decimals = 0)` | `12,345.5` (en), `12.345,5` (de), `12 345,5` (fr) | | `lang() -> u8[>..]` | the current language's code | | `lang_set(code) -> bool` | switch now and keep the choice (next sessions too); `""` goes back to the system's language | | `lang_count()`, `lang_code(i)`, `lang_name(i)`, `lang_index()` | the game's languages (the source first), each one's name in its own words ("Deutsch", "日本語"), the current one | | `lang_version() -> i32` | changes every time the language does | | `lang_source(code)` | in `main()`: the language the source text is in (default `"en"`) | | `font_fallback(path) -> bool` | a font every font draws missing characters from (symbols; see Fonts) | `tr` makes a fresh string on each call, so text drawn with it follows a language switch at once. Text a game keeps (a level's dialogue read into an array at load) is read again when `lang_version()` changes. When a key has no text in the current language, the source language's text shows (and the log says which key is missing); when no file has it, the key itself shows. `{{` and `}}` are literal braces. **Never build sentences from pieces** (`tr("you_have") + n + tr("coins")`): word order differs between languages. One key with placeholders, the whole sentence: `"Allume d'abord la lanterne du port, {0}."` ## Plural forms `tr_n("hud.lanterns", n)` reads `hud.lanterns.