Expand description
§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
constKey: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 = ..)ist!("apples", count = n, name = ..);tc!(i18n => "apples", n)too.- td
- A date formatted now (JS
td), aString:td!(t)andtd!(t, "name")(a named format) on the implicit instance, in the scope’s locale;td!(i18n => t)andtd!(i18n => t, "name")on an explicitI18norView. Featureformat. - tdr
- A date relative to now (JS
tdr), aString:tdr!(t)on the implicit instance,tdr!(i18n => t)on an explicitI18norView. Featureformat. - tn
- A number formatted now (JS
tn), aString:tn!(n)andtn!(n, "name")(a named format) on the implicit instance, in the scope’s locale;tn!(i18n => n)andtn!(i18n => n, "name")on an explicitI18norView. Featureformat.
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 overridei18n.json. - Change
- What changed: the active locale after the write and the new snapshot version.
- Check
Report - What
I18n::checkfound, sorted. Display lists it line by line. - Count
- The count of
I18n::tc: an integer, or (featureformat) anf64. - DateArg
- A date or a relative date to show later: what
Arg::date,Arg::date_as,Arg::relativeandText::datehold. 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:
Arcinside, cheap to clone,Send + Sync + 'static. - Key
- A
consttranslation key (key!):Displayresolves it through the nearest scope or the global instance, liket!(key). - Locale
Info - 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, anf64(an integer past 2^53 becomes the nearest one, as forCount). - Plural
Ctx - What a custom plural rule (
Text::plural_rule) gets: JScustomPluralRule(key, count, params, locale, getter). - Relative
Options - Options of
I18n::tdr_with,Intl.RelativeTimeFormat’s:numeric(alwaysby default,autosaysyesterday/now) andstyle(longby 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 withscope. - 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::tdandI18n::tdr, held as a JSDateholds it: whole milliseconds since 1970-01-01 UTC, within ±8.64e15 (±100 000 000 days). Anything else is an invalid date: it formats asInvalid 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). - Check
Item - One finding of
I18n::check.pageis"index"for the root. - Error
- What went wrong while building the catalog or switching it.
- Link
Error - 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
i18nthe global instance of the process:t!without=>,t(),Keyand the free functions read it when no scope sets an instance. - locale
- The locale an implicit
Textresolves in now: the nearest scope’s, else the implicit instance’s active locale;Nonewithout an instance. - negotiate
- The best of
availablefor anAccept-Languageheader or a comma-separated list of tags (navigator.languagesjoined): entries byq(ties keep their order), each tried as is, then shortened (de-AT→de), then as anyavailableof the same language (de→de-at, one with the same script first:zh-Hant-TW→zh-hant-hkoverzh-hans);*takes the first available. A tag withq=0is 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 asavailablespells 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
nameon the implicit instance, in the scope’s locale if one is set;Nonewithout an instance. - scope
- A new scope frame; nothing is set until
Scope::runorScope::wrap. - set_
locale - Switches the active locale of the implicit instance (see
I18n::set_locale). - t
- A
Textofkeyon the implicit instance (nearest scope, else the global one), whatt!("key")builds. - td
I18n::tdon the implicit instance (seetn()); without one,enin UTC. Whattd!calls.- td_as
I18n::td_ason the implicit instance (seetn()).- tdr
I18n::tdron the implicit instance (seetn()); without one,enand the system clock. Whattdr!calls.- tn
I18n::tnon the implicit instance: the nearestscope’s, else the global one, in the scope’s locale (else the active one); without either,en. Whattn!calls.- tn_as
I18n::tn_ason the implicit instance (seetn()).- version
- The crate version, as published (
CARGO_PKG_VERSION).