Guides

Loading

dir, lazy_dir, files, lazy_files, messages, remote with ensure_async, extend for libraries, eviction, reload, subscribe and watch.
SourceWhereWhat happens
.dir("locales")native, WASIevery file is read at build()
.lazy_dir("locales")native, WASIbuild() only lists the files; each is read on first use
.files(iter)everywhereany iterator of (path, bytes) (include_bytes!, include_dir, rust-embed); parsed at build()
.lazy_files(iter)everywherethe same, parsed on first use
.messages(locale, page, json)everywhereJSON in code: tests, generated data
.remote(manifest)browserthe paths are known, the app brings the bytes (ensure_async, add_file)
.extend(iter)everywherefiles of a library, under the app's own

Paths follow the folder layout: de.json is the root of de, pages/settings/de.json the settings page.

Lazy files

let i18n = I18n::builder().lazy_dir("locales").locale("de").fallback("en").build()?;
i18n.pages();                         // known from the file names, nothing read yet
i18n.page("settings").t("title");     // reads pages/settings/de.json, and more only if needed
  • A lookup reads only the layers it reaches, each file once.
  • A lazy file that cannot be read is skipped and reported (stderr in debug builds, or a tracing warning). Use dir or files if a broken file must stop the start.

remote and ensure_async

const MANIFEST: [&str; 4] = ["en.json", "de.json", "pages/settings/en.json", "pages/settings/de.json"];

let i18n = I18n::builder().remote(MANIFEST).locale("de").fallback("en").build()?;
i18n.ensure_async("de", Some("settings"), |path| async move {
    let response = gloo_net::http::Request::get(&format!("/locales/{path}")).send().await?;
    response.binary().await
}).await?;
  • ensure_async fetches what the locale's chain still lacks for the page, all files at once. The closure returns any Future, so it works with any runtime and any HTTP client.
  • A file another ensure_async is already fetching is not fetched twice.
  • The files of one call become visible together.
  • A failed fetch returns Error::Fetch { path, message } and keeps what was there; the other files of the call are still added. Calling ensure_async again fetches only what is still missing.
  • By hand: missing_files(locale, page) gives the paths, add_file(path, bytes) adds one. add_file accepts only manifest paths (Error::UnknownFile otherwise).
  • Until the files arrive, lookups answer from what is in: the root, the fallback, messages.

Translations from libraries

// in the library
pub fn locales() -> impl Iterator<Item = (&'static str, &'static [u8])> { /* include_dir walk */ }

// in the app
I18n::builder().extend(my_lib::locales()).extend(other_lib::locales()).dir("locales").build()?;

Layers merge per locale and page: extend calls in order, then the app's files, then messages. Later wins, so the app can override any string of a library. A library should keep its keys under its own top-level key ("my_lib": { … }).

Page eviction

I18n::builder().lazy_dir("locales").evict_pages(50).pin_page("tray").build()?;

At most 50 lazily loaded pages stay in memory; loading one more drops the least recently used. Roots, eagerly loaded files, pages with messages / add_messages and pinned pages are never evicted. An evicted local page is read again on next use; a remote one is missing again until the next ensure_async.

Reload, subscribe and watch

let sub = i18n.subscribe(|change| println!("{} v{}", change.locale, change.version));
i18n.reload()?;     // reads every source again
drop(sub);          // unsubscribe
  • reload() rebuilds from the builder's sources. Readers see the old catalog until the new one is in; a broken file makes reload fail and keeps the old catalog. Messages from add_messages are not kept: pass them to the builder if they must survive.
  • subscribe calls the listener with Change { locale, version } after set_locale to another locale, add_messages, reload, and when add_file or ensure_async brings files. Dropping the Subscription stops it.
  • Feature watch: i18n.watch() reloads when dir / lazy_dir files change. Debug builds only; in release and on wasm it does nothing. Dropping the guard stops it. The Tauri plugin calls it for you.