Reference · Translations
t
t(str, map | null=) -> strWhat it does
The key's text in the current locale, from the program's translations. {name} in
the text is replaced by that value from the map; an entry of plural forms picks the
form for count by the locale's rules. A key missing in the locale comes from the
default locale, and a key missing there shows as itself. The text is escaped in
markup like any other; raw(t(...)) keeps markup written in the translation while
still escaping the values put in.
Examples
translations({ en: { greeting: "Hello, {name}!", nav: { home: "Home" }, items: { one: "{count} item", other: "{count} items" } }, fr: { greeting: "Bonjour, {name} !", nav: { home: "Accueil" }, items: { one: "{count} article", other: "{count} articles" } },})print(t("nav.home"))print(t("greeting", { name: "Ada" }))print(t("items", { count: 1 }), "|", t("items", { count: 0 }), "|", t("items", { count: 2 }))print(with_locale("fr", () => t("greeting", { name: "Ada" })))HomeHello, Ada!1 item | 0 items | 2 itemsBonjour, Ada !Edge cases
- A missing key is the key itself, with no error; a key of a nested map, not a leaf, is the same.
- A
{name}with no value in the map stays as written; a value that is null shows asnull. Other braces are left alone. - The form is CLDR 47's for the number as
{count}shows it, read from its digits (CLDR's operands: the whole part, and the fraction's digits with and without their trailing zeros), the same on the server and in the browser, for about 110 languages (old codesiw,jw,tltoo); the sign is ignored. So a fraction is not one rule for all:1.5and0.5areonein French,"1.0"isonein Spanish andotherin English ("1.0 items"), and2.0istwoin Arabic.frhasonefor 0 and 1,ruhasone,fewandmany,jahasotheralone. - A whole float shows as a route writes it,
1.0, so it takesotherin English there, and as a component writes it,1, so it takesonethere, on both sides alike. A decimal keeps its digits:1.0mis "1.0 items". - A
zeroform, where one is written, is used for a 0 shown with no point (0, not0.0), in every language. countmay be numeric text ("1", as a query or a form gives it):"1e3"is 1000, and"0x1","Infinity"and full-width digits are not numbers, on either side, and takeother.- A key missing in the locale falls back to the default locale's text.
raw(t(...))escapes the values but not the translation.
translations({ en: { greeting: "Hello, {name}!", only_en: "English only", welcome: "Welcome, <b>{name}</b>", nav: { home: "Home" } }, fr: { greeting: "Bonjour, {name} !" }, ru: { n: { one: "{count} день", few: "{count} дня", many: "{count} дней", other: "{count} дня" } },})print(t("missing.key"), t("nav"))print(t("greeting"), t("greeting", { name: null }), t("greeting", { other: 1 }))print(with_locale("fr", () => t("only_en")))print(with_locale("ru", () => [1, 2, 5, 21, 1.5] | map((c) => t("n", { count: c }))))print(raw(t("welcome", { name: "<i>Ada</i>" })))translations({ en: { items: { zero: "no items", one: "{count} item", other: "{count} items" } }, fr: { items: { one: "{count} article", other: "{count} articles" } },})print([1, 1.0, "1.0", 0, 0.0, "1e3", "0x1"] | map((c) => t("items", { count: c })))print(with_locale("fr", () => [1.5, 0.5, 2] | map((c) => t("items", { count: c }))))missing.key navHello, {name}! Hello, null! Hello, {name}!English only["1 день", "2 дня", "5 дней", "21 день", "1.5 дня"]Welcome, <b><i>Ada</i></b>["1 item", "1.0 items", "1.0 items", "no items", "0.0 items", "1e3 items", "0x1 items"]["1.5 article", "0.5 article", "2 articles"]Errors
Calling t before translations is a runtime error, not an error value, and so is a
key that is not a string or values that are not a map (null is fine).
print(t("hello"))line 1: `t` needs `translations` firsttranslations({ en: { a: "b" } })print(t("a", "Ada"))line 2: `t` expects its values as a map, got strChaining
The key comes first in a pipeline, so the values are the only argument written. In
markup t reads well on its own; a list of keys maps over it.
translations({ en: { yes: "Yes", no: "No", hi: "Hi, {name}" } })print("hi" | t({ name: "Ada" }))print(["yes", "no"] | map(t))Hi, Ada["Yes", "No"]view Header(user) = <header> <a href="/">{t("nav.home")}</a> <span>{t("greeting", { name: user.name })}</span></header>See also
translations, locale, with_locale, raw, format_number
Thread 0 posts
On t(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo