Formats
Formats need the format feature (on by default). They follow Intl.NumberFormat, Intl.DateTimeFormat and
Intl.RelativeTimeFormat, with the locale's iso as the tag (else its code), as @i18n-micro/core does.
Numbers
use i18n_next::{I18n, t, tn};
let i18n = I18n::builder()
.config("i18n.json") // numberFormats, locales[].iso
.formats(r#"{"en": {"short": {"notation": "compact"}}}"#)
.build()?;
i18n.tn(1234567.891); // "1,234,567.891" in en, "1 234 567,891" in ru
i18n.tn_as(1234.5, "currency"); // a named format: "$1,234.50"
i18n.text("total").number(9.99); // {number} in a Text, as <i18n-t :number>
t!("total", @number = 9.99); // the same with the macro
tn!(1234.5); // the implicit instance
i18n.language_name("de"); // Some("Deutsch")
tn takes any integer or float. A tag without data formats as en.
Named formats are Vue I18n's numberFormats: {"<locale>": {"<name>": {<Intl options>}}}, from i18n.json and
Builder::formats (a name given again wins). A name is looked up by the locale code, then shorter
(zh-Hant-TW → zh-Hant → zh). An unknown name warns and formats with the defaults, as JS does.
Intl.NumberFormat option | Supported |
|---|---|
style: decimal, minimumFractionDigits, maximumFractionDigits, minimumIntegerDigits | yes |
useGrouping: true / false / "always" / "auto" / "min2" | yes |
style: percent | yes |
style: currency + currency, currencyDisplay: symbol / narrowSymbol / code | yes |
currencyDisplay: name | with the currency-names feature |
notation: compact (compactDisplay: short) with style: decimal | yes |
the locale's default numbering system (ar-EG → ١٬٢٣٤) | yes |
style: unit, signDisplay other than auto, roundingMode, significant digits, notation: scientific / engineering, compactDisplay: long, compact currency or percent, another numberingSystem | no |
An unsupported option, or a value Intl rejects (currency: "usdx"), fails build() with
Error::UnsupportedFormat. Unknown keys are ignored.
Dates and relative time
use i18n_next::{I18n, RelativeOptions, t, td, tdr};
let i18n = I18n::builder()
.config("i18n.json") // datetimeFormats, locales[].iso
.datetime_formats(r#"{"en": {"long": {"dateStyle": "full", "timeStyle": "short"}}}"#)
.time_zone("Europe/Berlin") // default: the system zone
.build()?;
let at = 1_704_428_640_000i64; // ms since 1970, as a JS Date holds it
i18n.td(at); // "1/5/2024"; ru "05.01.2024"
i18n.td_as(at, "long"); // "Friday, January 5, 2024 at 5:24 AM"
i18n.tdr(at - 3 * 86_400_000); // "3 days ago"
i18n.tdr_with(at - 86_400_000, RelativeOptions::default().auto()); // "yesterday"
i18n.text("due").date(at); // {date} in a Text, as <i18n-t :date>
t!("seen", @relative = at); // {relativeDate}, as <i18n-t :relative-date>
td!(at); td!(at, "long"); tdr!(at); // the implicit instance
tdtakes milliseconds (i64orf64) or aSystemTime; with thetimefeature also its date types. A value JS cannot hold showsInvalid Date.- Without options
tdshows year, month and day, numeric, in the instance's zone. tdruses the largest unit that fits at least once (a year is 365 days, a month 30, no weeks), rounded down.- Named formats are Vue I18n's
datetimeFormats, looked up like number formats. A format's owntimeZonewins over the instance's. - Dates are Gregorian. For
th,fa,fa-AF,psandshn-THadd-u-ca-gregoryto theiriso(th-u-ca-gregory) so the frontend shows Gregorian dates too.
| Option | Supported |
|---|---|
dateStyle (full … short), timeStyle (medium, short), both together | yes |
weekday, year, month, day, hour, minute, second, any combination | yes |
hour12, hourCycle | yes |
timeZone (IANA) | yes; only UTC without the tz feature |
calendar: "gregory", localeMatcher, formatMatcher | yes |
relative numeric: always / auto, style: long / short / narrow (RelativeOptions) | yes |
timeStyle: full / long, timeZoneName, era, dayPeriod, fractionalSecondDigits, other calendars | no |
An unsupported option or a value Intl rejects fails build() with Error::UnsupportedFormat; an unknown zone in
Builder::time_zone fails with Error::UnknownTimeZone.
Time zones and the clock
I18n::builder().time_zone("Europe/Berlin").clock(|| now_ms).build()?;
- The zone is
time_zonewhen set, else the system's. Where the system zone comes from on each platform: Platforms. set_system_time_zone(name)sets the system zone by name, once, before the first date. The Tauri plugin does this with the WebView's zone.clockis "now" fortdr, in milliseconds (JSFormatService({ now })). Fix it in tests, or give the client the server's time. By default the system clock; in the browserDate.now().
Locale data
The number and date data are built into the binary. By default that is the CLDR modern locales. Set
I18N_NEXT_LOCALES at build time to keep only the locales you ship:
I18N_NEXT_LOCALES=en,de,ru cargo build --release # these, their parents (de-CH brings de) and en
I18N_NEXT_LOCALES=all cargo build --release # every locale
cargo build --release # unset or `modern`: the CLDR modern locales
A tag with no data fails the build. A locale left out formats as its nearest selected parent, else en.
currency-names (off by default) adds the currency names for currencyDisplay: name. The data is large: pick the
locales with I18N_NEXT_LOCALES when you turn it on.