Skip to content

std::localize

Text for a reader in a locale: the Localize interface, and the error locale-aware number parsing reports. Display is locale-independent: "${1234}" is 1234 everywhere, because that text goes into file names, headers and CSV. Text a person reads goes through a function that takes a Locale: Int.format, Float.format, Decimal.format and their format_percent forms, String.join_list, or Localize.localize, which a type implements to say how it reads in a locale. Each parse_in function reads what the matching formatter writes, strictly by default. The data comes from CLDR and travels with the program: a program carries the locales its nomi.toml lists under [i18n], and a locale it does not list formats as its nearest listed parent, or as CLDR’s root.

Import with import std::localize.

Types

Interfaces

interface Localize
    fn localize(value: self, locale: Locale) -> String
end

A value as a reader of locale reads it, as Display is how it reads in a log line. Int, Float and Decimal give their format with its defaults, and String itself. Std also implements it for Money (Money.format with the currency’s symbol), Date, Time, NaiveDateTime and DateTime (format with its default styles), Duration (Duration.format in long words), Phrase (Phrase.format) and Noun (its nominative for one thing).

assert Localize.localize(1234567, Locale"de") == "1.234.567"
assert Localize.localize("Tak", Locale"pl") == "Tak"
fn localize(value: self, locale: Locale) -> String

interface Localize

enum NumberParseError
    Empty
    Unexpected { at: Int, text: String }
    Grouping { at: Int }
    OutOfRange
    TooManyFractionDigits { max: Int }
    WrongCurrency { expected: Currency, found: String }
end

Why text is not a number in a locale. at counts grapheme clusters from the start of the text, as String.length does.

Variants

  • Empty

    The text is empty, or only spaces when parsing leniently.

  • Unexpected

    text at at is not part of a number in the locale. At the end of text that stops early, text is empty and at is the text’s length.

  • Grouping

    A group separator at at where the locale puts none.

  • OutOfRange

    The number does not fit the type parsed into.

  • TooManyFractionDigits

    The amount has more digits after the decimal point than its currency’s max minor digits (Money.parse_in).

  • WrongCurrency

    The text names found where the amount’s currency goes, which is not expected (Money.parse_in).

assert Int.parse_in("12,34,567", Locale"en") == Err(NumberParseError.Grouping{at: 2})

assert Display.to_string(NumberParseError.WrongCurrency{expected: Currency"EUR", found: "zł"})
    == "expected an amount in EUR, found `zł`"

impl Display for NumberParseError

fn to_string(e: NumberParseError) -> String

impl Display.to_string

What is wrong, for a person to read.

assert Display.to_string(NumberParseError.Unexpected{at: 5, text: ","}) == "unexpected `,` at 5"

assert Display.to_string(NumberParseError.Unexpected{at: 3, text: ""})
    == "the text ends at 3 before the number does"

impl Debug for NumberParseError

fn inspect(value: NumberParseError) -> String

impl Debug.inspect

impl Equatable for NumberParseError

fn equal(a: NumberParseError, b: NumberParseError) -> Bool

impl Equatable.equal