locon
Locon localization for Rust: flat JSON locale files, lookup by key or by phrase, CLDR plurals, and an asset audit for CI. The files and the lookup rules are the same as in Locon for JavaScript, so a web or React Native app and Rust code can share one set of translations.
[]
= "0.1"
Locale files
One flat JSON object per locale, string → string:
// locales/de.json (the project locale: the language the app is written in)
// locales/en.json
{token}placeholders.- Plural forms are keys with the CLDR category as a suffix:
_zero,_one,_two,_few,_many,_other(_otheris required). - Duplicate keys and non-string values are rejected when loading.
Usage
use Locon;
// From a directory at runtime…
let locon = from_dir?;
// …or compiled into the binary.
let locon = from_sources?;
locon.set_locale;
locon.l; // by key: "Good morning"
locon.l; // by phrase: "Good morning"
locon.l; // "Hello Ann"
locon.l; // "1 card"
Lookup follows Locon:
- An input that is a key is used as is; otherwise it is looked up by its value in the project locale (the first key with that value wins).
- With a
count, the plural form of the target locale is chosen with ICU4X plural rules (Russian 1 / 3 / 5 / 21 all differ);{count}is filled in. - Fallback per key: target locale → project locale. Empty values count as untranslated. If nothing matches, the input itself is returned (interpolated).
App-wide
use t;
install;
set_locale;
t!; // "Good morning"
t!; // "3 cards" (count first)
t!; // any Display value
Without an installed Locon, t! returns the phrase interpolated, which keeps unit tests readable.
Device languages
// The user's preferred languages, most preferred first.
locon.use_device_locales; // → "de"
match_locale; // → zh-Hant
is_rtl; // true
Asset audit
locon::check audits the files against each other and against the phrases the code uses (collected
by your build tool or a source scan):
let assets = load_dir?;
let used = vec!;
for issue in check
It reports:
- phrases or keys used by the code that resolve to nothing;
- keys missing from a locale, or unknown to the project locale;
- placeholders added or dropped by a translation (
{count}may be spelled out in a plural form); - plural families without
_other; - duplicate values whose second key is unreachable by phrase;
- characters of a script the locale does not use (a stray copy-paste).
License
MIT or Apache-2.0, at your option.