Expand description
Locon localization for Rust.
- One flat JSON object per locale (
locales/de.json:{"greet": "Guten Morgen"}): the same files as Locon for JavaScript, so web, mobile and Rust code can share them. - Lookup by key or by phrase: an input that is not a key is looked up by its value in the
project (source) locale, so code can read
l("Guten Morgen"). {token}placeholders; CLDR plural forms as_zero/_one/_two/_few/_many/_otherkey suffixes, chosen with ICU4X plural rules;{count}filled automatically.- Per-key fallback to the project locale; device language matching (
de-AT→de,zh-TW→zh-Hant); right-to-left detection. check: the Locon asset audit (missing and unknown keys, placeholders, plural_other, unreachable duplicates, stray scripts, unresolved phrases), for CI.
let locon = locon::Locon::from_sources("de", &[
("de", r#"{"cards_one": "{count} Karte", "cards_other": "{count} Karten"}"#),
("en", r#"{"cards_one": "{count} card", "cards_other": "{count} cards"}"#),
]).unwrap();
locon.set_locale(Some("en"));
assert_eq!(locon.l("{count} Karten", &[], Some(3.0), None), "3 cards");Macros§
- t
- Localized text through the installed localization:
t!("Good morning"),t!("{count} cards", count = n),t!("Hello {name}", name = user.name).count(written first) selects the plural form and fills{count}; other values are anything withDisplay.
Structs§
- Issue
- One problem, with the file it is about (
locales/<locale>.json, or the source file of a phrase). - Load
Error - A locale file that cannot be read or is not a flat object of strings.
- Locon
- The localization of one app: its locale files, project locale and current locale.
Functions§
- check
- Audits the locale files against each other and against the phrases the code uses
(
used: phrase or key → file). Returns every problem found. - global
- The installed localization, if any.
- install
- Makes
loconthe app-wide localization used byt()andt!(first call wins). - interpolate
- Replaces
{token}placeholders; unknown tokens stay as they are. - is_
plural_ key - is_rtl
- Right-to-left locales (Arabic, Hebrew, Persian, Urdu, … or an explicit RTL script).
- load_
dir - Reads every
<locale>.jsonin a directory. - load_
sources - Locale files given as
(locale, json)pairs, e.g. compiled in withinclude_str!. - locale
- The current locale of the installed localization.
- match_
locale - The first of the device’s preferred tags (in order) the app has: the same tag, else the same language and script, else the same language. Returns the app’s locale name as written.
- parse_
locale - Parses one locale file: a flat JSON object of strings, without duplicate keys.
- plural_
base key_one→key; other keys unchanged.- plural_
category - CLDR plural category of
countinlocale("one","few","other", …). - set_
locale - Changes the language of the installed localization;
Noneselects the project locale. - t
- Formats text through the installed localization (
install). Without one the phrase is interpolated as it is, like Locon’s last resort (handy in unit tests).
Type Aliases§
- Assets
- locale → key → value.