Reference · Translations
locale
locale() -> str | nullWhat 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()))nullenfrEdge cases
nullbeforetranslations, never an empty string.- The default is what
{ default: ... }names, elseenwhen there is one, else the first locale: the first written in a map, the first by name in a folder. - Inside
with_localethe name is the locale's own:with_locale("fr-CA", ...)served byfrreports"fr". - A request's locale is settled on its first use and stays for the request.
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")frfr entrueErrors
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")enChaining
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`)12,34,567.5/help/en-IN.htmlroute GET / = <html lang={locale()}><body>{t("welcome")}</body></html>See also
translations, t, locale_from, with_locale, format_number
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