locale
import locale from "locale";
locale.set("fi"); // all @t("...") strings in Finnish from now on
io.println(@t("Good morning!")); // "Hyvää huomenta!"locale chooses which language the program’s translatable strings are
shown in. The strings and their translations are written with @t("...")
and the #localization region; the compiler
builds the translations into the program, and @t looks them up in the
active locale. You only import locale to choose or inspect the locale.
It is a capability module: it reads the environment.
The active locale
| Function | Does |
|---|---|
set(code) | Use code from now on (normalized); "" shows the source strings |
get() -> string | The active code, normalized; "" when none |
use_env() | Go back to the locale the environment names |
env_code() -> string | The code the environment names, else the OS display language (Windows, macOS); "" when none |
normalize(name) -> string | "fi-FI.UTF-8" → "fi_FI"; "C", "POSIX" and invalid names → "" |
pick(langs) -> ?i64 | Which of langs the active locale picks, as @t does |
available() -> []const []const u8 | Every language of the program’s #localization regions, sorted |
A program starts in the locale its environment names: the first of
LC_ALL, LC_MESSAGES and LANG that is set and not empty. The charset
and modifier are dropped and - becomes _, so LANG=fi_FI.UTF-8 is
fi_FI. C and POSIX mean no locale: the source strings.
LANG=de_DE.UTF-8 ./app # German, if the program has it
LC_ALL=C ./app # the source stringsWhen none of the three is set, the program starts in the user’s display language from the OS, normalized the same way:
- Windows normally sets none of the variables. The program uses the
Windows display language (Settings, Time & language, Windows display
language), so
en-USisen_US. It is the UI language, not the regional format: a user with Finnish date and number formats and an English Windows gets English. - macOS gives an app started from Finder or the Dock no
LANG. The program uses the first of the user’s preferred languages (System Settings, Language & Region):fi-FIisfi_FI,zh-Hant-TWiszh_Hant_TW. - Linux uses the variables only.
A variable that is set still wins, on every platform, and LC_ALL=C
still means the source strings. A program can always override the choice
with locale.set (for example from a setting or a --lang option).
Which translation is shown
For the active code, say fi_FI:
- the
fi_FItranslation, - else the translation of its language part,
fi, - else the source string.
Codes compare without regard to case (fi_fi is fi_FI). set never
fails: a code the program has no translations for shows the source strings.
locale.set("de_AT");
io.println(@t("Good morning!")); // the "de" translation
locale.set("pt");
io.println(@t("Good morning!")); // "Good morning!" — no pt translationOffering a choice
available() lists the languages the program’s #localization regions
translate into, for a language menu:
for (code in locale.available()) {
io.println(code); // "de", "fi", "sv", ...
}Plural forms
Languages differ in how many forms a count takes (English two, Russian
and Polish four, Arabic six). A keyed translation can have plural forms
(#localization, Keyed translations): @t
takes the count first and shows the form the active language’s CLDR
plural rules pick, other when a language has no form of that category.
The compiler builds in the rules of the region’s languages only; locale
evaluates them and needs no intl:
fn files(n: i64) -> string { return @t(files.count, n); }
#localization
source en;
languages fi, ru;
files.count {
en: { one: "{1} file"; other: "{1} files"; };
fi: { one: "{1} tiedosto"; other: "{1} tiedostoa"; };
ru: { one: "{1} файл"; few: "{1} файла"; many: "{1} файлов"; other: "{1} файла"; };
}files(1), files(3), files(5), files(21) are “1 файл”, “3 файла”,
“5 файлов”, “21 файл” in Russian.
With text-keyed entries (@t("{n} files")), a translation can only
reword around {n}. Then ask intl for the count’s plural
category in the active locale and choose among strings, one per category
your languages need:
import intl from "intl";
fn files(n: i64) -> string {
var code: string = locale.get();
if (code.len == 0) { code = "en"; } // the source strings' language
let loc: intl.Locale = intl.locale(code) or { return @t("{n} files"); };
let p: intl.Plural = intl.plural(loc, intl.dec_from_i64(n));
if (p == intl.Plural.One) { return @t("{n} file"); }
if (p == intl.Plural.Few) { return @t("{n} files (few)"); }
if (p == intl.Plural.Many) { return @t("{n} files (many)"); }
return @t("{n} files");
}
#localization
"{n} file" { fi: "{n} tiedosto"; ru: "{n} файл"; }
"{n} files (few)" { ru: "{n} файла"; }
"{n} files (many)" { ru: "{n} файлов"; }
"{n} files" { fi: "{n} tiedostoa"; ru: "{n} файла"; }files(1), files(3), files(5), files(21) are “1 tiedosto”, “3
tiedostoa”, … in Finnish and “1 файл”, “3 файла”, “5 файлов”, “21 файл”
in Russian. intl.locale accepts locale’s codes (fi_FI).
Notes
- Codes are identifiers: letters, digits and
_, at most 32 bytes. - Translations can’t be loaded from a file at run time yet; they are part of the program.
- Not available in t0.
Last modified: