Skip to main content

Crate i18n_next

Crate i18n_next 

Source
Expand description

Crates.io docs.rs Documentation

§i18n-next

i18n for Rust on the same locale files as nuxt-i18n-micro: global and per-page translations, plurals and Intl-compatible formats, native and wasm, with no runtime dependencies.

Documentation: https://s00d.github.io/i18n-next-rs/

Status: pre-release (0.1). Root and page chunks, fallback chains, i18n.json, plurals (CLDR rules as in Intl.PluralRules), linked messages, Text (the Rust <i18n-t>), t! and scope, numbers, dates and relative time as Intl shows them (own CLDR 48 data and time zones), lazy and remote loading, and a Rust guest for the Tauri plugin. Not published yet.

use i18n_next::{I18n, t, tc};

let i18n = I18n::builder()
    .locale("ru")
    .fallback("en")
    .files([
        ("en.json", br#"{"nav": {"home": "Home"}, "hi": "Hi, {name}!"}"#.as_slice()),
        ("ru.json", r#"{"hi": "Привет, {name}!",
            "apples": "нет яблок | {count} яблоко | {count} яблока | {count} яблок"}"#.as_bytes()),
        ("pages/settings/ru.json", r#"{"title": "Настройки"}"#.as_bytes()),
    ])
    .build()?;
assert_eq!(t!(i18n => "hi", name = "Аня"), "Привет, Аня!");
assert_eq!(t!(i18n => "nav.home"), "Home");                    // fallback
assert_eq!(t!(i18n => "title", @page = "settings"), "Настройки");
assert_eq!(tc!(i18n => "apples", 21), "21 яблоко");            // CLDR plural rules

let hi = i18n.text("hi").arg("name", "Ann");                   // lazy: shown when displayed
i18n.set_locale("en")?;
assert_eq!(hi.to_string(), "Hi, Ann!");

Or install it once and write t!("hi", name = "Ann") anywhere; scope() sets the locale of a request or a task. Default features: std, format, tz, global; default-features = false is the micro build. I18N_NEXT_LOCALES=en,de,ru at build time keeps only the CLDR data you ship.

Guides, platforms (desktop, Android, iOS, browser, Tauri guest) and the feature list: https://s00d.github.io/i18n-next-rs/.

License: MIT.

Macros§

key
A const Key: const QUIT: Key = key!("menu.quit");.
t
A Text: t!("key", name = value, …).
tc
t! with the count second, the JS habit: tc!("apples", n, name = ..) is t!("apples", count = n, name = ..); tc!(i18n => "apples", n) too.
td
A date formatted now (JS td), a String: td!(t) and td!(t, "name") (a named format) on the implicit instance, in the scope’s locale; td!(i18n => t) and td!(i18n => t, "name") on an explicit I18n or View. Feature format.
tdr
A date relative to now (JS tdr), a String: tdr!(t) on the implicit instance, tdr!(i18n => t) on an explicit I18n or View. Feature format.
tn
A number formatted now (JS tn), a String: tn!(n) and tn!(n, "name") (a named format) on the implicit instance, in the scope’s locale; tn!(i18n => n) and tn!(i18n => n, "name") on an explicit I18n or View. Feature format.

Structs§

Args
Parameters for {name} placeholders, plus an optional default text.
Builder
Builds an I18n. Every call is optional; later calls of the same kind add to or override earlier ones, builder calls override i18n.json.
Change
What changed: the active locale after the write and the new snapshot version.
CheckReport
What I18n::check found, sorted. Display lists it line by line.
Count
The count of I18n::tc: an integer, or (feature format) an f64.
DateArg
A date or a relative date to show later: what Arg::date, Arg::date_as, Arg::relative and Text::date hold. Its formatter is a pointer set by those constructors, so an app that never shows a date links none of the date code.
I18n
The translation catalog: Arc inside, cheap to clone, Send + Sync + 'static.
Key
A const translation key (key!): Display resolves it through the nearest scope or the global instance, like t!(key).
LocaleInfo
Public facts about one locale: from i18n.json, builder calls and the files found.
Number
A number for I18n::tn: any primitive integer or float, held as JS holds it, an f64 (an integer past 2^53 becomes the nearest one, as for Count).
PluralCtx
What a custom plural rule (Text::plural_rule) gets: JS customPluralRule(key, count, params, locale, getter).
RelativeOptions
Options of I18n::tdr_with, Intl.RelativeTimeFormat’s: numeric (always by default, auto says yesterday / now) and style (long by default, short, narrow).
Scope
Locale, page and instance for a closure or a future, without passing them down (run(locale, fn) of @i18n-micro/node). Start with scope.
Scoped
A future run in a scope (Scope::wrap).
Subscription
A subscription made by I18n::subscribe: dropping it unsubscribes. It does not keep the catalog alive.
Text
A translation shown later: the Rust counterpart of <i18n-t>.
Timestamp
An instant for I18n::td and I18n::tdr, held as a JS Date holds it: whole milliseconds since 1970-01-01 UTC, within ±8.64e15 (±100 000 000 days). Anything else is an invalid date: it formats as Invalid Date.
View
A cheap view of an I18n: one page and/or another locale, with the same lookups. Clone + Send + Sync + 'static; it reads the live catalog, so it sees later changes.

Enums§

Arg
One parameter value, printed like JS String(value).
CheckItem
One finding of I18n::check. page is "index" for the root.
Error
What went wrong while building the catalog or switching it.
LinkError
Why a link left its message as written.
Part
One piece of Text::parts: text (parameters filled in), or a placeholder no argument fills, for the caller to render.

Functions§

global
The installed instance, if any.
install
Makes i18n the global instance of the process: t! without =>, t(), Key and the free functions read it when no scope sets an instance.
locale
The locale an implicit Text resolves in now: the nearest scope’s, else the implicit instance’s active locale; None without an instance.
negotiate
The best of available for an Accept-Language header or a comma-separated list of tags (navigator.languages joined): entries by q (ties keep their order), each tried as is, then shortened (de-AT → de), then as any available of the same language (de → de-at, one with the same script first: zh-Hant-TW → zh-hant-hk over zh-hans); * takes the first available. A tag with q=0 is refused: never chosen, not by * and not by shortening or the same-language step. An entry with a malformed weight (q=2, q = 0.5, any other parameter) is dropped (RFC 9110). Case-insensitive, _ equals -. Returns the code as available spells it. Only the first 32 entries within the first 4 KiB count (cut at the last , in that span): the work stays bounded for any header a client sends.
page
A view of page name on the implicit instance, in the scope’s locale if one is set; None without an instance.
scope
A new scope frame; nothing is set until Scope::run or Scope::wrap.
set_locale
Switches the active locale of the implicit instance (see I18n::set_locale).
t
A Text of key on the implicit instance (nearest scope, else the global one), what t!("key") builds.
td
I18n::td on the implicit instance (see tn()); without one, en in UTC. What td! calls.
td_as
I18n::td_as on the implicit instance (see tn()).
tdr
I18n::tdr on the implicit instance (see tn()); without one, en and the system clock. What tdr! calls.
tn
I18n::tn on the implicit instance: the nearest scope’s, else the global one, in the scope’s locale (else the active one); without either, en. What tn! calls.
tn_as
I18n::tn_as on the implicit instance (see tn()).
version
The crate version, as published (CARGO_PKG_VERSION).