Skip to content
  1. Docs
  2. enzUI framework
  3. Developers

Localization

Fluent catalogs, language selection, CLDR formatting, right-to-left layout and translating framework strings.

enzui_core::localization (re-exported as enzui_widgets::localization) supplies Fluent message catalogs, language negotiation, scoped widget defaults and ICU4X formatting backed by CLDR data. Your app's messages and the framework's own controls go through the same catalog.

The framework's strings ship in English, German, Spanish, French and Hungarian. A Localizer uses them after the app's catalog for the same language, so framework controls follow the user's language without the app shipping their strings.

Catalogs and the localizer

Add UTF-8 Fluent resources with CatalogBuilder::add_resource and freeze the builder with build. add_resource takes source text and does no file or network access; you choose how to embed or ship translation files. A resource with a syntax error or duplicate entry is rejected whole. Catalogs are immutable, thread-safe and cheap to clone.

Create a Localizer with Localizer::new(catalog, preferred, fallback): an ordered Vec<Locale> of preferences and an explicit fallback. preferred_locales reads the Linux preferences: the first valid value of LC_ALL, LC_MESSAGES, then LANG picks the base locale, and unless that is C or POSIX, LANGUAGE preferences come before it. The final default is en-US.

Lookups fall back per message and per attribute, in this order:

  1. the app's catalog for the languages matching the user's preferences;
  2. the framework's translation for those languages;
  3. the app's catalog for its fallback language;
  4. built-in English.

Using it everywhere

Give the localizer to the setup route:

rust
app_setup::run_with_services(program, AppServices::default().localizer(localizer.clone()))

boot, update, view, title and subscription then run in its scope, so localization::text("key") and localized!("key", "name" => value) read the app's catalog wherever it needs a string, with no global helper of its own:

rust
self.status = enzui_core::localized!("added", "count" => count);

Keep the clone in the app state: select on it changes the language, and the next view shows it. select returns true and advances generation only when the preferences change. Include the generation in keys for translated cached subtrees, and regenerate app-owned strings stored outside the view. Background work does not inherit the scope; pass it a clone.

For windows in different languages, create separate localizers over the same catalog and build each window's root with its own Localizer::view, which keeps the locale through layout, lazy overlays, event handling and accessibility collection.

text returns display text; text_with takes named Arguments. Numeric arguments use cardinal plural rules; NUMBER($value, type: "ordinal") selects ordinal rules. resolve reports which language supplied the text and any formatting failures.

Controls and translated text

Text, secure, multiline, autocomplete and combo fields, checkboxes, toggles, radio groups and date pickers accept owned labels and placeholders, so pass Localizer::text results straight in. Keep identity separate from text: pin widget ids when labels are translated, and use RadioChoice::with_label to keep an option's key apart from its label.

Formatting

NeedAPI
Exact numbersLocale::format_decimal with CLDR grouping, separators and numbering systems
DatesLocale::format_date (short, medium, long, full); format_month_day omits the year
TimesLocale::format_time with the locale's hour cycle and day periods
Week startLocale::first_weekday from CLDR region data
CalendarsCalendar and date-picker defaults use the localizer's CLDR names and native numerals
Money, percent, relative time, durationsWith the locale-formatters feature: format_currency(&amount, "EUR"), format_percent, format_relative_time(seconds), format_duration(duration, DurationStyle)
File sizesenzui_widgets::file_browser::format_bytes
SortingLocale::collator(Collation); Collation::NATURAL sorts file2 before file10
Searchingenzui_core::text_fold::contains matches without case or accents (cafe finds Café)

Fluent's NUMBER supports no currency or percent style, and DATETIME is unavailable. Format dates, times, money and percentages through Locale and pass the result to the message as text.

The environment can set a locale per category: LANG=en_US.UTF-8 LC_TIME=de_DE.UTF-8 shows English text with German dates. Localizer::locale_for(LocaleCategory) answers for Time, Numeric, Collate and Monetary, and set_category_locale(category, None) makes a category follow the preferred locale again.

Editable time fields keep an explicit, reversible grammar from Localizer::time_format (ASCII digits, a colon, the locale's hour cycle and translated AM/PM). Language changes never alter stored dates, timestamps or numbers.

Right-to-left layout

In a right-to-left locale (Arabic, Hebrew, Persian, Urdu) framework widgets mirror on their own: the app shell and titlebar (window controls move left, close outermost), toolbar rows, sidebar and menu rows, breadcrumbs, tab strips, tree views, disclosures, text fields, sliders and switches. Each takes .direction(..) to override the locale. Focus and accessibility order stay logical.

Your own layout mirrors where you opt in:

  • row![..].mirrored(rtl) and keyed_row(..).mirrored(rtl) lay out from the right while keeping logical child order. Read rtl from localizer.locale().direction() or enzui_core::placement::ambient_direction().
  • enz_text aligns TextAlign::Start by default; Left and Right stay physical.
  • Icons named by meaning (navigation.back) flip; literal glyph names (chevron-right) do not.

ENZUI_TEXT_DIRECTION=rtl forces right-to-left whatever the locale, for testing.

Translating framework strings

The complete English catalog is crates/enzui-core/src/localization/locales/en-US.ftl, also available as localization::ENGLISH. For a language the framework does not ship, add a translated copy to your app's catalog for that locale, keeping message ids and argument names. An app resource for a shipped language can override any framework message.

Finding untranslated text

Select the pseudo-locale en-XA (localization::PSEUDO_LOCALE), for example with LANGUAGE=en_XA. Every catalog message then shows accented and about 40 percent longer, in brackets: Close becomes [Çļöšé ~~]. Plain English on screen was never routed through the catalog, and a missing closing bracket marks clipped text.

Coverage

localization::coverage(reference, translation) lists missing and obsolete keys. Use the report to check translation coverage in your own tests.

Contributing a framework translation

  1. Copy en-US.ftl to <language>.ftl in the same directory and translate every value, keeping keys, { $argument } names and NUMBER(..) calls. Adapt plural variants to the language's CLDR categories.
  2. Add the file to FRAMEWORK_TRANSLATIONS in crates/enzui-core/src/localization/mod.rs.
  3. Run cargo wt --lib shipped_translations_are_complete.
  4. Check the result in a running app with LANGUAGE=<language>.

Ship fonts that cover your app's scripts; catalogs do not include font assets.