Skip to main content

Crate locon

Crate locon 

Source
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/_other key 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");

For app-wide use, install one Locon and format with t!.

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 with Display.

Structs§

Issue
One problem, with the file it is about (locales/<locale>.json, or the source file of a phrase).
LoadError
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 locon the app-wide localization used by t() and t! (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>.json in a directory.
load_sources
Locale files given as (locale, json) pairs, e.g. compiled in with include_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 count in locale ("one", "few", "other", …).
set_locale
Changes the language of the installed localization; None selects 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.