Reference · Translations

t

signature
t(str, map | null=) -> str

What 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" })))
output
HomeHello, Ada!1 item | 0 items | 2 itemsBonjour, Ada !

Edge cases

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 }))))
output
missing.key navHello, {name}! Hello, null! Hello, {name}!English only["1 день", "2 дня", "5 дней", "21 день", "1.5 дня"]Welcome, <b>&lt;i&gt;Ada&lt;/i&gt;</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).

this one fails
print(t("hello"))
output
line 1: `t` needs `translations` first
this one fails
translations({ en: { a: "b" } })print(t("a", "Ada"))
output
line 2: `t` expects its values as a map, got str

Chaining

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))
output
Hi, Ada["Yes", "No"]
in a program
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
6Useful? Like it, or add to its thread below.

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