Guides
Loading
dir, lazy_dir, files, lazy_files, messages, remote with ensure_async, extend for libraries, eviction, reload, subscribe and watch.
| Source | Where | What happens |
|---|---|---|
.dir("locales") | native, WASI | every file is read at build() |
.lazy_dir("locales") | native, WASI | build() only lists the files; each is read on first use |
.files(iter) | everywhere | any iterator of (path, bytes) (include_bytes!, include_dir, rust-embed); parsed at build() |
.lazy_files(iter) | everywhere | the same, parsed on first use |
.messages(locale, page, json) | everywhere | JSON in code: tests, generated data |
.remote(manifest) | browser | the paths are known, the app brings the bytes (ensure_async, add_file) |
.extend(iter) | everywhere | files 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
tracingwarning). Usedirorfilesif 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_asyncfetches what the locale's chain still lacks for the page, all files at once. The closure returns anyFuture, so it works with any runtime and any HTTP client.- A file another
ensure_asyncis 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. Callingensure_asyncagain fetches only what is still missing. - By hand:
missing_files(locale, page)gives the paths,add_file(path, bytes)adds one.add_fileaccepts only manifest paths (Error::UnknownFileotherwise). - 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 makesreloadfail and keeps the old catalog. Messages fromadd_messagesare not kept: pass them to the builder if they must survive.subscribecalls the listener withChange { locale, version }afterset_localeto another locale,add_messages,reload, and whenadd_fileorensure_asyncbrings files. Dropping theSubscriptionstops it.- Feature
watch:i18n.watch()reloads whendir/lazy_dirfiles change. Debug builds only; in release and on wasm it does nothing. Dropping the guard stops it. The Tauri plugin calls it for you.