Platforms
The API is the same everywhere; your code needs no cfg. The Tauri plugin is native only.
| Target | What works |
|---|---|
| desktop (Linux, macOS, Windows) | everything: dir, lazy_dir, watch, the plugin |
| Android, iOS | everything except reading a folder: embed the files |
wasm32-unknown-unknown (browser, Rust frontend in Tauri) | no file system: dir / lazy_dir don't exist; use files, lazy_files or remote |
wasm32-wasip1 / wasip2 | file system available, dir works |
Shipping locale files
A relative path in dir("locales") or config("i18n.json") resolves against the working directory. That works under
cargo run and tauri dev, but an app bundle opened from Finder, Explorer or a launcher starts elsewhere and finds
nothing. Use relative paths for development only.
Embed the files with files and config_json: the same code on desktop, Android and iOS.
macro_rules! file {
($path:literal) => { ($path, &include_bytes!(concat!("../../locales/", $path))[..]) };
}
let i18n = I18n::builder()
.config_json(include_str!("../../i18n.json"))
.files([file!("en.json"), file!("ru.json"), file!("pages/settings/en.json"), file!("pages/settings/ru.json")])
.build()?;
include_dir or rust-embed work too: any iterator of (path, bytes).
Or ship them as resources (desktop) to keep the files editable. List them in tauri.conf.json
("bundle": { "resources": { "../locales": "locales", "../i18n.json": "i18n.json" } }), then build the catalog from
an absolute path and register the plugin in setup:
tauri::Builder::default()
.setup(|app| {
let res = app.path().resource_dir()?;
let i18n = I18n::builder()
.config(res.join("i18n.json"))
.dir(res.join("locales"))
.build()?;
app.handle().plugin(tauri_plugin_i18n_next::Builder::new(i18n).build())?;
Ok(())
})
For release builds also set I18N_NEXT_LOCALES to the locales you ship (Formats).
Time zones
In a Tauri app nothing is needed: the plugin uses the WebView's zone, so Rust and the frontend show the same dates on every OS. Elsewhere the system zone comes from:
| Where | System zone | Named zones |
|---|---|---|
| Linux, macOS, iOS | TZ, then /etc/localtime | the system's zone files |
| Windows | TZ; else UTC | a tzdb built into the crate |
| Android | the system | the system tzdata |
| browser | Intl | Intl |
Windows names its zones its own way and the crate reads no registry, so a plain Rust program there shows UTC unless told otherwise:
let i18n = I18n::builder().time_zone("Europe/Berlin").build()?; // a fixed zone
// or, once and before the first date, a zone you got elsewhere:
let taken = i18n.set_system_time_zone("Europe/Berlin"); // false: unknown zone, or too late
The tzdb feature adds the built-in tzdb on other systems too, for example a container without
/usr/share/zoneinfo.
Mobile
- Embed the files (above): app resources are not a regular file system.
- The start locale comes from the WebView's
navigator.languages, unless a saved one applies. - The protocol is
http://i18n-next.localhoston Android andi18n-next://localhoston iOS: allow both in the CSP. A page served from an Android dev host needsorigins.
Browser
Embed the files with files / lazy_files, or fetch them with remote + 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()?;
// on navigation (wasm-bindgen-futures, Leptos resources, …)
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?;
i18n_next::install(i18n)?;
view! { <h1>{t!("greeting", name = "Leptos").to_string()}</h1> }
- The crate brings no HTTP client and no
web-sys: use gloo-net,reqwestorfetchthroughjs-sys. - A failed request returns
Error::Fetch; the page answers from the root and the fallback until a later call works. Details: Loading. evict_pages(n)keeps a long-running app's memory bounded.- Re-render on
subscribe; pick the start locale withnegotiateovernavigator.languages.
Rust frontend inside Tauri
The tauri-guest feature connects a Rust frontend (Leptos, Yew, Dioxus) to the plugin, as the JS package does.
i18n-next = { version = "0.1", features = ["tauri-guest"] }
use i18n_next::tauri_guest;
let i18n = tauri_guest::connect("index").await?; // builds an I18n from the plugin and fills it
i18n_next::install(i18n.clone())?; // t!("key") in components
let _sub = i18n.subscribe(|change| { /* render again */ });
tauri_guest::set_page("settings").await?; // then t!("title", @page = "settings")
tauri_guest::set_locale("de").await?; // Rust, native labels and every window
| Function | JS equivalent | What |
|---|---|---|
connect(page) | connect(i18n, { page }) | builds an I18n from the plugin and loads page |
set_page(page) | link.setPage | loads the page and keeps it following locale switches |
set_locale(code) | link.setLocale | switches Rust and every window |
preload(page) | link.preload | loads a page without showing it |
load(page) | link.load | loads a page about to be shown (a router guard) |
refresh() | link.refresh | reloads what changed |
disconnect() | link.close | stops following changes |
- Changes from Rust and other windows are followed; misses go to the dev report in debug builds.
- Errors:
Error::Fetchoutside a Tauri WebView or when a read fails;Error::NotInstalledbeforeconnect;Error::UnknownLocalefromset_locale. - Under a CSP add
'wasm-unsafe-eval'toscript-src(CSP).
One binary can run in the browser and in Tauri: check for __TAURI_INTERNALS__ once and load pages with
tauri_guest::set_page or ensure_async.
async fn show(page: &str) -> Result<(), i18n_next::Error> {
if in_tauri() {
return tauri_guest::set_page(page).await;
}
let i18n = i18n_next::global().ok_or(i18n_next::Error::NotInstalled)?;
i18n.ensure_async(&i18n.locale(), Some(page), fetch).await
}