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.
Exports
Section titled “Exports”Types
Interfaces
interface Localize
Section titled “interface Localize”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"
Localize.localize
Section titled “Localize.localize”fn localize(value: self, locale: Locale) -> String
interface Localize
enum NumberParseError
Section titled “enum NumberParseError”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
-
EmptyThe text is empty, or only spaces when parsing leniently.
-
Unexpectedtextatatis not part of a number in the locale. At the end of text that stops early,textis empty andatis the text’s length. -
GroupingA group separator at
atwhere the locale puts none. -
OutOfRangeThe number does not fit the type parsed into.
-
TooManyFractionDigitsThe amount has more digits after the decimal point than its currency’s
maxminor digits (Money.parse_in). -
WrongCurrencyThe text names
foundwhere the amount’s currency goes, which is notexpected(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ł`"
NumberParseError.to_string
Section titled “NumberParseError.to_string”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"
NumberParseError.inspect
Section titled “NumberParseError.inspect”impl Debug for NumberParseError
fn inspect(value: NumberParseError) -> String
impl Debug.inspect
NumberParseError.equal
Section titled “NumberParseError.equal”impl Equatable for NumberParseError
fn equal(a: NumberParseError, b: NumberParseError) -> Bool
impl Equatable.equal