Grav v1.14.0

locale

import "std/text" (included in import "std") · namespace std.text

Numbers, money, dates and plurals the way people in a country write them: group separators and decimal marks, currency amounts such as KES 1,234.50, month and day names, date patterns, and CLDR plural categories for choosing "1 file" or "3 files".

grav
import "std"

fn main() {
    let ke = Locale.of("en-KE")
    print(ke.currency(1234.5, "KES"))
    print(ke.currency(1234.5, "KES", true))
    print(Locale.of("de-DE").currency(1234.5, "EUR", true))
    print(Locale.of("en-IN").number(12345678.9))
    print(Locale.of("fr").percent(0.256, 1))
    let d = DateTime.of(2025, 1, 15, 15, 4, 0)
    print(Locale.of("en-US").dateTime(d))
    print(ke.date(d, "full"))
    print(Locale.of("sw").date(d, "full"))
    print(Locale.of("ru").date(d, "long"))
    print(Locale.of("en").choose(1, "one:# file|other:# files"))
    print(Locale.of("en").choose(1200, "one:# file|other:# files"))
    print(Locale.of("ru").plural(3))
    print(Locale.of("en").bytes(1536000))
}
Output
KES 1,234.50
KSh1,234.50
1.234,50 €
1,23,45,678.9
25,6 %
Jan 15, 2025, 3:04 PM
Wednesday, 15 January 2025
Jumatano, 15 Januari 2025
15 января 2025 г.
1 file
1,200 files
few
1.5 MB
  • Locales: English (en, with US, GB, KE, IN, NG, ZA, AU, CA and other regions), Swahili (sw), French, German, Spanish, Portuguese (BR and PT), Italian, Dutch, Russian, Japanese and Chinese. A tag is written en-KE, en_KE or en; an unknown region falls back to its language, and an unknown language to en-US. Locale.of makes each locale once and shares it.
  • Numbers: number(x) (up to 3 decimals, trailing zeros dropped), number(x, 2) (exactly 2), integer(n), percent(ratio), bytes(n) (1.5 MB; bytes(n, true) for KiB units), and parseNumber("1.234,5") to read one back. Rounding is half away from zero, with binary fuzz taken out, so 1.005 becomes 1.01.
  • Money: currency(amount, code) uses the currency's own decimals (2 for KES and USD, 0 for JPY and UGX, 3 for KWD) and writes the code, or the symbol with currency(amount, code, true) when one is well known ($, €, £, KSh, ₹, ₦, R$, …). Whether it comes before or after the number follows the locale.
  • Dates: date(dt, style) with "short", "medium", "long" or "full"; time(dt, "short" | "medium"); dateTime(dt); format(dt, pattern) with CLDR letters (yyyy MMMM d EEEE HH mm a, and 'quoted text'). Russian months are declined inside dates.
  • Plurals: plural(n) gives "one", "few", "many" or "other" by the CLDR rules of the language (Russian distinguishes 1, 2–4 and 5+; French counts 0 as one). choose(n, "one:# file|other:# files") picks the form and puts the number, written in the locale, in place of #.
  • The data follows CLDR, trimmed to what applications use. Currency spacing is a plain space.

API

class Locale

How one locale writes numbers, money, dates and plurals.

Constructors

  • new Locale(tag: string)

Static members

  • static fn of(tag: string) -> Locale — The locale for a BCP 47 tag ("en-KE", "fr", "pt_BR"); made once and shared.
  • static fn languages() -> Array<string> — The languages with data of their own.
  • static fn currencyDigits(code: string) -> int — The decimals a currency is written with (ISO 4217).
  • static fn symbolOf(code: string, region: string = "") -> string — A currency's symbol ($, €, KSh), or the code when there is no well-known one.

Fields

  • tag: string — The tag it was made for ("en-KE").
  • language: string — The language ("en") and region ("KE", or "").
  • region: string
  • decimal: string = "." — The decimal mark and the group separator.
  • group: string = ","
  • groupFirst: int = 3 — Digits in the first group and in the ones before it (3 and 3; 3 and 2 in India), and the fewest integer digits that get grouped at all (2 in Spanish: 1234, 12.345).
  • groupRest: int = 3
  • minGroup: int = 1
  • currencyFirst: bool = true — Whether money is written "KES 12.50" (true) or "12,50 KES".
  • symbolSpaced: bool = false — Whether a currency symbol touches the number ("$12.50") or is spaced ("R$ 12,50").
  • monthsShort: Array<string> — January first, short and long; Sunday first, short and long. "|"-separated in the data.
  • months: Array<string>
  • monthsInDate: Array<string> — Month names as used inside a date (Russian declines them: "15 января").
  • daysShort: Array<string>
  • days: Array<string>
  • datePatterns: Array<string> — Date patterns: short, medium, long, full; time patterns: short, medium.
  • timePatterns: Array<string>
  • dateTimeJoin: string = " — How the date and the time are joined ("{date}, {time}").
  • am: string = "AM" — AM and PM.
  • pm: string = "PM"

Methods

  • fn number(x: double, decimals: int = -1) -> string — x with this locale's separators: up to 3 decimals without trailing zeros, or exactly decimals of them.
  • fn integer(n: long) -> string — A whole number with group separators.
  • fn percent(ratio: double, decimals: int = 0) -> string — ratio as a percentage (0.256 -> "26%"; "25,6 %" in French and German).
  • fn currency(amount: double, code: string, symbol: bool = false) -> string — An amount of money: "KES 1,234.50" with the currency code, or with its symbol ("$1,234.50", "1.234,50 €") when symbol is true and one is known. The decimals are the currency's (2 for KES, 0 for JPY, 3 for KWD).
  • fn bytes(n: long, binary: bool = false) -> string — A size in bytes: "1.5 MB" (powers of 1000; "MiB" units with binary).
  • fn parseNumber(text: string) -> double — A number written in this locale read back ("1.234,5" in German is 1234.5); ValueError when it is not one.
  • fn plural(n: double) -> string — The CLDR plural category of n: "zero", "one", "two", "few", "many" or "other".
  • fn choose(n: double, forms: string) -> string — The form of forms ("one:# file|other:# files") that fits n, with # replaced by n written in this locale. "other" is used when the category has no form.
  • fn date(dt: DateTime, style: string = "medium") -> string — The date in a style: "short" (15/01/2025), "medium" (15 Jan 2025), "long" (15 January 2025) or "full" (Wednesday, 15 January 2025).
  • fn time(dt: DateTime, style: string = "short") -> string — The time in a style: "short" (15:04 or 3:04 PM) or "medium" (with seconds).
  • fn dateTime(dt: DateTime, dateStyle: string = "medium", timeStyle: string = "short") -> string — The date and the time ("15 Jan 2025, 15:04").
  • fn monthName(month: int, short: bool = false) -> string — The month's name (1 is January), long or short.
  • fn dayName(weekday: int, short: bool = false) -> string — The weekday's name (0 is Sunday, as DateTime.weekday counts), long or short.
  • fn format(dt: DateTime, pattern: string) -> string — The date written with a CLDR-style pattern: yyyy yy MMMM MMM MM M dd d EEEE EEE HH H hh h mm ss a, and 'quoted text' as is.

Edit this page on GitHub