Reference · Translations

locale

signature
locale() -> str | null

What it does

The locale the code runs in, as named in translations: "en", "pt-BR". In a request it is what locale_from gives, else the locale cookie, else the best match of Accept-Language (fr-CA is served by fr, pt by pt-BR; q weights count, written q= or Q=, * does not), else the default locale; inside with_locale it is that locale. Outside a request it is the default. Without translations it is null.

Examples

print(locale())translations({ en: { a: "b" }, fr: { a: "c" } })print(locale())print(with_locale("fr", () => locale()))
output
nullenfr

Edge cases

translations({ fr: { a: "x" }, de: { a: "y" } })print(locale())translations({ en: { a: "b" }, fr: { a: "c" } })print(with_locale("fr-CA", () => locale()), with_locale("de", () => locale()))print(locale() == "en")
output
frfr entrue

Errors

None: locale takes no arguments and never fails. What it gives is null or a string, so a program that may run without translations gives it a default.

print(locale() ?? "en")
output
en

Chaining

It is a value to pass on: to format_number, date.format or money.format, which use the program's locale when none is given, or to pick a file.

translations({ "en-IN": { a: "b" } })print(format_number(1234567.5, locale()))print(`/help/${locale()}.html`)
output
12,34,567.5/help/en-IN.html
in a program
route GET / = <html lang={locale()}><body>{t("welcome")}</body></html>

See also

translations, t, locale_from, with_locale, format_number

Thread
8Useful? Like it, or add to its thread below.

Thread 0 posts

On locale(): 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