module std::localesource
Locales: a language and region and how they write dates, times, numbers and prices, from CLDR 48's data (alloy-ui's decisions/F12.md, F12e, questions 11A to 14A), the same on every target: the tables of locale_data.alloy, which cldr/generate.alloy writes from the pinned release cldr/fetch.sh fetches. Every locale CLDR gives modern coverage is there, regional ones as their differences.
Dates are written from components, as Intl.DateTimeFormat's options
and CLDR's skeletons are, the locale choosing the order, punctuation
and forms (ICU's pattern generator, followed step by step), or from a
pattern of CLDR's letters, fixed for every locale.
import std::time;
const fr = Locale::of(&"fr-FR");
var line = String::empty();
const show = DateFormat { .weekday = Width::Short, .month = Width::Short, .time = true, .year = false };
const moment: i64 = 1795199400000000000; // 2026-11-20T18:30Z
fr.format_moment(moment, &Zone::utc(), &show, &var line);
expect_equal(&line, &String::from(&"ven. 20 nov., 18:30"));
line.clear();
fr.format_money(2450, &"EUR", &var line);
expect_equal(&line, &String::from(&"24,50\u{a0}€"));
expect(fr.first_weekday() == Weekday::Monday && Locale::of(&"en-US").first_weekday() == Weekday::Sunday);
expect(Locale::of(&"ar-EG").right_to_left() && Locale::of(&"he").right_to_left() && !fr.right_to_left());
LocaleLocale::ofLocale::systemtagfirst_weekdaytwelve_hourright_to_leftWidthmonth_nameweekday_nameZoneNameDateFormatformat_dateformat_momentformat_patternformat_numberformat_moneyparse_numberparse_dateparse_time
type Localesource
exp type Locale = struct
A locale of the tables, with the region its week and clock follow and the user's choices of them.
Functions
exp fn Locale::of(tag: &[u8]) -> Localeexp fn Locale::system() -> Localeexp fn tag(self locale: &Locale) -> &[u8]exp fn first_weekday(self locale: &Locale) -> Weekdayexp fn twelve_hour(self locale: &Locale) -> boolexp fn right_to_left(self locale: &Locale) -> boolexp fn month_name(self locale: &Locale, month: Month, width: Width) -> &[u8]exp fn weekday_name(self locale: &Locale, day: Weekday, width: Width) -> &[u8]exp fn format_date(self locale: &Locale, date: &Date, format: &DateFormat, out: &var String)exp fn format_moment( self locale: &Locale, moment: i64, zone: &Zone, format: &DateFormat, out: &var String )exp fn format_pattern( self locale: &Locale, moment: i64, zone: &Zone, pattern: &[u8], out: &var String )exp fn format_number(self locale: &Locale, value: f64, fraction: u8, out: &var String)exp fn format_money(self locale: &Locale, amount: i64, currency: &[u8], out: &var String)exp fn parse_number(self locale: &Locale, text: &[u8]) -> Option<f64>exp fn parse_date(self locale: &Locale, text: &[u8]) -> Option<Date>exp fn parse_time(self locale: &Locale, text: &[u8]) -> Option<TimeOfDay>
fn Locale::ofsource
exp fn Locale::of(tag: &[u8]) -> Locale
The locale the tables hold nearest the BCP 47 tag ("fr-CA", "zh-TW",
"sr-Latn-ME"), "en" at worst; its week and clock follow the tag's
region, else the language's likely one, and the extension's hc and
fw keys (en-GB-u-hc-h12).
fn Locale::systemsource
exp fn Locale::system() -> Locale
The user's locale, with their choices of the week's first day and a
12- or 24-hour clock: LC_ALL, LC_TIME or LANG on Linux and under
WASI (fr_FR.UTF-8 is fr-FR, C and POSIX are en), the user's
locale, first day and time format on Windows, CFLocale's and
AppleICUForce24HourTime on macOS, the page's first language and
Intl.Locale's week on the web.
fn tagsource
exp fn tag(self locale: &Locale) -> &[u8]
The tables' tag for the locale: "fr-CA", "en" for one they lack.
fn first_weekdaysource
exp fn first_weekday(self locale: &Locale) -> Weekday
The week's first day: Monday in France, Sunday in the United States.
fn twelve_hoursource
exp fn twelve_hour(self locale: &Locale) -> bool
Whether the clock has 12 hours and AM and PM.
fn right_to_leftsource
exp fn right_to_left(self locale: &Locale) -> bool
Whether the locale's language is written right to left (CLDR's character order): Arabic, Hebrew, Persian, Urdu, ...
type Widthsource
exp type Width = enum
How wide a name or field is written: Friday, Fri, F; a month by
its number (Numeric).
Variants
NoneWideShortNarrowNumeric
fn month_namesource
exp fn month_name(self locale: &Locale, month: Month, width: Width) -> &[u8]
The month's name as written alone, in a calendar's title or a menu: Wide, Short or Narrow, another width giving the wide name.
fn weekday_namesource
exp fn weekday_name(self locale: &Locale, day: Weekday, width: Width) -> &[u8]
The weekday's name as written alone: Wide, Short or Narrow, another width giving the wide name.
type ZoneNamesource
exp type ZoneName = enum
How a zone is named after a time: CET or EST where the locale has
such a name (else as Offset), GMT+1 (Offset), Paris time (City:
the locale's region format around the zone's city, as its name writes
it).
Variants
NoneShortOffsetCity
type DateFormatsource
exp type DateFormat = struct
What a date shows, the locale choosing the order, punctuation and
forms: Friday 20 November 2026, Fri, Nov 20, 1:30 PM.
Fields
exp weekday: Width = Width::NoneWide "Friday", Short "Fri", Narrow "F"
exp day: bool = trueexp month: Width = Width::Wideor Numeric "11", or None
exp year: bool = trueexp time: bool = falseexp seconds: bool = falseexp zone: ZoneName = ZoneName::NoneShort "CET", Offset "GMT+1", City "Paris time"
fn format_datesource
exp fn format_date(self locale: &Locale, date: &Date, format: &DateFormat, out: &var String)
Writes the day as the format shows a date (its time and zone left out).
fn format_momentsource
exp fn format_moment(
self locale: &Locale,
moment: i64,
zone: &Zone,
format: &DateFormat,
out: &var String
)
Writes the moment (nanoseconds since 1970 UTC) as the zone's clocks show it, in the format.
fn format_patternsource
exp fn format_pattern(
self locale: &Locale,
moment: i64,
zone: &Zone,
pattern: &[u8],
out: &var String
)
Writes the moment on the zone's clocks in a pattern of CLDR's letters
(EEEE d MMMM, HH:mm), the same order in every locale, names and
digits the locale's: G, y, M, L, d, E, c, a, h, H, K, k, m, s, S, z,
O, Z, X, x, v and V (VVV the zone's city), 'quoted' text and ''.
fn format_numbersource
exp fn format_number(self locale: &Locale, value: f64, fraction: u8, out: &var String)
Writes the number rounded to fraction decimals, half away from
zero, in the locale's digits and symbols, grouped as it groups:
1 234,5 in French, 1,234.5 in English, 12,34,567 in Hindi.
var out = String::empty();
Locale::of(&"en").format_number(1234.5, 1, &var out);
expect_equal(&out, &String::from(&"1,234.5"));
out.clear();
Locale::of(&"de").format_number(-0.125, 2, &var out);
expect_equal(&out, &String::from(&"-0,13"));
fn format_moneysource
exp fn format_money(self locale: &Locale, amount: i64, currency: &[u8], out: &var String)
Writes the price, an integer of the currency's minor unit (cents for
EUR and USD, yen for JPY), with its symbol in the locale's currency
format: 2450 EUR is 24,50 € in French, €24.50 in English.
var out = String::empty();
Locale::of(&"ja").format_money(2450, &"JPY", &var out);
expect_equal(&out, &String::from(&"¥2,450"));
out.clear();
Locale::of(&"en-US").format_money(-2450, &"CHF", &var out);
expect_equal(&out, &String::from(&"-CHF\u{a0}24.50"));
fn parse_numbersource
exp fn parse_number(self locale: &Locale, text: &[u8]) -> Option<f64>
The number the text writes in the locale, the inverse of
format_number made forgiving: the locale's digits or ASCII ones, its
minus sign or -, its group sign between digits of the whole part (any
space where that sign is a space), and its decimal sign, or a point
where the point groups nothing. None when the text writes no number.
expect(Locale::of(&"fr").parse_number(&"-1 234,5") == ::Some(-1234.5 to f64));
expect(Locale::of(&"en-US").parse_number(&"1,234.5") == ::Some(1234.5 to f64));
expect(Locale::of(&"de").parse_number(&"2,5") == ::Some(2.5 to f64));
expect(Locale::of(&"fr").parse_number(&"2.5") == ::Some(2.5 to f64));
expect(Locale::of(&"ar-EG").parse_number(&"١٢٣٫٥") == ::Some(123.5 to f64));
expect(Locale::of(&"en").parse_number(&"12a") is ::None);
fn parse_datesource
exp fn parse_date(self locale: &Locale, text: &[u8]) -> Option<Date>
The day the text writes in the locale's short date, its parts in the
locale's order and separated by any of / . -, spaces or what the
locale writes between them, its digits or ASCII ones; or written as ISO
8601 (2026-11-21), unless the locale writes the year, then the day,
then the month (Kyrgyz's 2026-21-11). A year of two digits falls between 80
years before this one and 20 after, as ICU places it.
const day = ::Some(Date::of(2026, Month::November, 21));
expect(Locale::of(&"en-GB").parse_date(&"21/11/2026") == day);
expect(Locale::of(&"en-US").parse_date(&"11/21/2026") == day);
expect(Locale::of(&"de").parse_date(&"21.11.26") == day);
expect(Locale::of(&"ja").parse_date(&"2026/11/21") == day);
expect(Locale::of(&"en-US").parse_date(&"2026-11-21") == day);
expect(Locale::of(&"en-GB").parse_date(&"31/02/2026") is ::None);
fn parse_timesource
exp fn parse_time(self locale: &Locale, text: &[u8]) -> Option<TimeOfDay>
The time of day the text writes: on a 24-hour clock (14:30, 14,
14.30), or a 12-hour one with the locale's day period or am and pm in
any case, dots and spaces aside (2:30 pm, 2 P.M., 午後2:30); its
digits or ASCII ones. Seconds may follow, and are dropped.
const at = ::Some(TimeOfDay::of(14, 30));
expect(Locale::of(&"en-GB").parse_time(&"14:30") == at);
expect(Locale::of(&"en-US").parse_time(&"2:30 PM") == at);
expect(Locale::of(&"en-US").parse_time(&"2:30p.m.") == at);
expect(Locale::of(&"ja").parse_time(&"午後2:30") == at);
expect(Locale::of(&"fr").parse_time(&"14") == ::Some(TimeOfDay::of(14, 0)));
expect(Locale::of(&"en").parse_time(&"12:00 am") == ::Some(TimeOfDay::of(0, 0)));
expect(Locale::of(&"en").parse_time(&"25:00") is ::None);