Skip to main content

Module lang

Module lang 

Source
Expand description

Translations: play the same game in several languages. Available as ctx.lang.

Put one text file per language into the lang folder next to the game, named by the language code (en.txt, de.txt, fr.txt, …). Each line is key = text:

# Lines starting with # are comments.
language = Deutsch
intro = Willkommen! Drücke Leertaste zum Springen.
score = Du hast {points} Punkte.
coins.one = 1 Münze
coins.other = {n} Münzen

Then speak keys instead of text:

ctx.speak(ctx.tr("intro"));
ctx.speak(ctx.tr_with("score", &[("points", &points)]));
ctx.speak(ctx.lang.plural("coins", 3)); // "3 Münzen"
  • The game starts in the language the player chose last time (Context::set_language saves it), else in Engine::language, else in the system language if there is a file for it, else in the fallback language (English).
  • A key missing in the current language comes from the fallback language. A key missing everywhere is spoken as the key itself and reported once on the console, so a forgotten translation never crashes the game.
  • {name} is replaced by the argument with that name. \n in a text is a line break.
  • The special key language is the language’s own name, for a language menu (Lang::name).
  • Texts can also be compiled into the game: ctx.lang.add_text("de", include_str!("de.txt")).

The engine’s own announcements (menus, dialogs, Context::describe, vehicle speed) are translated as well. English and German are built in; other languages, or different wording, go into your files under the keys listed in docs/translations.md.

Structs§

Lang
The translation tables and the current language. Reach it through ctx.lang.