Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Translations

egui-pop-i18n translates an egui-pop application with Fluent: catalogues per locale, a fallback chain ending in English, a language setting, numbers, dates and plurals written for the locale, and tools that find the keys the code uses. It is a plugin built on egui-pop’s public API; the facade re-exports it as egui_pop::i18n with the i18n feature:

egui-pop = { version = "0.3", features = ["i18n"] }

The plugin

I18nPlugin loads egui-pop’s own catalogues (English and French: menus, the palette, the settings window, dialogs, the shortcuts editor, the workbench, devtools, the platform services), the application’s, and optionally a directory read at start-up whose messages replace the built-in ones, so translators try their work without rebuilding:

use egui_pop::egui;
use egui_pop::i18n::{Catalogue, I18nPlugin, args, plugin, t};
use egui_pop::prelude::*;

static CATALOGUES: &[Catalogue] = &[
    // Usually `include_str!("../locales/fr/app.ftl")`.
    Catalogue::new("en", "app.ftl", "greeting = Hello, { $name }!\n"),
    Catalogue::new("fr", "app.ftl", "greeting = Bonjour, { $name } !\n"),
];

let ctx = egui::Context::default();
let mut app = App::builder()
    .plugins(DefaultPlugins)
    .plugin(
        I18nPlugin::new()
            .catalogues(CATALOGUES)
            .dir("locales-override")
            .env("MYAPP_LANG"),
    )
    .build(&ctx)
    .unwrap();

plugin::set_language(app.handle(), "fr").unwrap();
let mut shown = String::new();
let mut out = ctx.run_ui(egui::RawInput::default(), |ui| {
    app.show(ui);
    shown = t(ui.ctx(), "greeting", &args![name = "Ada"]);
});
out.textures_delta.clear();
assert_eq!(shown, "Bonjour, Ada !");
// egui-pop's own strings follow.
assert_eq!(
    egui_pop::core::strings::tr(&ctx, &egui_pop::core::strings::RETRY),
    "Réessayer"
);

Catalogues are Fluent files, one directory per locale (locales/<locale>/*.ftl). A build script can list a directory and generate the Catalogue array with include_str!; a handful of files are as easily listed by hand.

Looking text up

  • t(ctx, "key", &args![…]): the text in the context’s language; text(ctx, "key", "English", &args![…]) gives the English to show when no catalogue has the key.
  • tr!("key", name = value): the same from anywhere, through the catalogues last installed (the connected plugin’s).
  • I18n::t, I18n::text: on the catalogues themselves (I18nPlugin::i18n() gives them), in tests or worker threads.

A key is a Fluent message id, optionally with an attribute (command-app-quit.title). A lookup tries the locale, its parents (fr-CA, then fr, then a regional catalogue of the same language), then English. A key missing everywhere shows the English written in the code, else the key itself.

egui-pop’s interface asks egui_pop::core::strings, which the plugin answers. What plugins declare is translated where it is drawn, through keys derived from stable ids, never from English, so rewording English keeps every translation:

WhatKey
a command’s title, descriptioncommand-<id>.title, .description
a command’s categorycategory-<english>
a panel’s titlepanel-<id>.title
a submenu’s labelmenu-<id>.label
a setting’s label, descriptionsetting-<id>.label, .description
one value of a choice settingsetting-<id>-choice-<value>
a plugin’s title (a settings section)plugin-<id>.title
a clipboard type’s title (“a colour”)clip-<format>.title

Every character of an id other than an ASCII letter, a digit or - becomes - (app.quit gives command-app-quit.title).

A command’s title may say { $application }, in its English and in its translations: it is filled with the application’s name (AppBuilder::name), so “About { $application }” reads “About Helios” and its translation “À propos de { $application }” reads “À propos de Helios”. The extractor (declarations) keeps it a placeable in the English catalogue (extract::plain_with); every other brace of a title stays text.

The commands every panel gets (<panel>.focus, <panel>.toggle) read texts shared by all panels, with a { $panel } placeholder filled with the panel’s translated title: egui-pop-panel-focus-title, egui-pop-panel-focus-description, egui-pop-panel-toggle-title, egui-pop-panel-toggle-description, in egui-pop’s own catalogue. An application translates its panels’ titles and nothing more; a translation under the command’s own key (command-<id>.title) still wins. The extractor leaves those commands’ texts out.

An application’s catalogue may define a key egui-pop’s own catalogue already has (a better word for egui-pop-cancel in its domain): its text overrides egui-pop’s, I18n::overridden lists it and the plugin publishes an i18n.overridden warning on the journal. A key defined twice by the application itself is still refused. egui-pop’s catalogues are base catalogues (Catalogue::as_base).

A plugin shipping its own catalogue adds it with I18n::add_owned: every message must carry the plugin’s id (acme-…, command-acme-…), so it cannot rewrite another’s text.

Without the application

An application that does not use App builds an I18n and installs it in its egui context with install(&ctx, &i18n): t(ctx, ..), text(ctx, ..) and of(ctx) read it, tr! reads it as the global catalogues, and egui-pop’s own text is translated through a Provider installed as the context’s strings provider. The plugin installs its catalogues the same way when it connects.

use egui_pop::egui;
use egui_pop::i18n::{I18n, args, install, t, tr};

let i18n = I18n::with_egui_pop("fr");
i18n.add("fr", "hello = Bonjour, { $name } !\n").unwrap();
let ctx = egui::Context::default();
install(&ctx, &i18n);
assert_eq!(t(&ctx, "hello", &args![name = "Ada"]), "Bonjour, Ada !");
assert_eq!(tr!("hello", name = "Ada"), "Bonjour, Ada !");

The I18n is shared: i18n.set_locale("en") or a catalogue added later shows at the next lookup, without installing again. The global tr! reads is the process’s: the last install wins. What the plugin adds on top (the language setting, locale.changed, the i18n.resolve step, the debug collector) is left to the application.

Numbers, dates and plurals

Plurals select on the locale’s rules; numbers are written with its separators; DATETIME writes dates:

use egui_pop::i18n::{I18n, args};

let i18n = I18n::new("fr");
i18n.add("fr", "
photos = { $count ->
    [one] { $count } photo
   *[other] { $count } photos
}
size = { NUMBER($mib, maximumFractionDigits: 1) } Mio
saved = Enregistré le { DATETIME($when, dateStyle: \"long\") }
").unwrap();
assert_eq!(i18n.t("photos", &args![count = 1]), "1 photo");
assert_eq!(i18n.t("photos", &args![count = 2400]), "2\u{202f}400 photos");
assert_eq!(i18n.t("size", &args![mib = 12.345]), "12,3 Mio");
assert_eq!(i18n.t("saved", &args![when = "2026-10-12"]), "Enregistré le 12 octobre 2026");

egui-pop’s own strings (egui_pop_core::strings) take typed arguments. tr_args places text as it is, even text that looks like a number: a folder named “2026” stays “2026”, not “2,026”. A count or a size goes through tr_with as an Arg::Num, built with into(), so it is written with the locale’s separators:

use egui_pop::core::strings::{self, StrKey};
use egui_pop::egui;

const OPENED: StrKey = StrKey::new("opened", "{ $folder }: { $count } entries");
let ctx = egui::Context::default();
let text = strings::tr_with(&ctx, &OPENED, &[("folder", "2026".into()), ("count", 3.into())]);
assert_eq!(text, "2026: 3 entries");

egui_pop::i18n::format does the same from Rust (number, date, time, and quantity, a number and its unit with the locale’s space between them: a narrow no-break space in French, a plain space in English; sliders use it through egui_pop_core::strings::quantity). Month and weekday names are known for English and French; other languages get numeric dates.

The language setting and its signal

The plugin declares appearance.language in the host’s store, like every setting: system (an environment variable named with I18nPlugin::env, then the system’s language), or a locale with a catalogue, each shown by its own name. Changing it switches the locale; the plugin publishes locale.changed ({ "from", "to", "setting" }) on the host’s journal and the next frame is drawn in the new language.

The i18n.resolve step

Lookups are a step (ADR 0011): a plugin may observe them, redirect them (another key or locale) or replace them. Without hooks, lookups do not run the step; with one, each distinct lookup runs it once ({ "key", "locale", "args" }, its result the text or null), at the next frame, and its answer is kept until the locale or the catalogues change. ShowKeys replaces every lookup by its key, to see which key a piece of text comes from; it is connected while the developer.show_keys setting is on:

use egui_pop::egui;
use egui_pop::i18n::{I18nPlugin, ShowKeys};
use egui_pop::model::commands::Origin;
use egui_pop::model::values::{Change, Set, Value};
use egui_pop::prelude::*;

let ctx = egui::Context::default();
let mut app = App::builder()
    .plugin(I18nPlugin::new())
    .plugin(ShowKeys)
    .build(&ctx)
    .unwrap();
egui_pop::core::lock(&app.handle().store())
    .set(Set::new(
        "settings/developer.show_keys",
        Change::To(Value::Bool(true)),
        Origin::User,
    ))
    .unwrap();
let mut shown = String::new();
for _ in 0..4 {
    let mut out = ctx.run_ui(egui::RawInput::default(), |ui| {
        app.show(ui);
        shown = egui_pop::core::strings::tr(ui.ctx(), &egui_pop::core::strings::RETRY);
    });
    out.textures_delta.clear();
}
assert_eq!(shown, "egui-pop-retry");

Debugging

The i18n collector of the debug bridge (shown in devtools’ collectors tab) gives the locale, its chain, the locales and, per locale, the keys looked up that its language lacks; set_locale and clear_missing act on them, for the user (devtools): they write, so the bridge’s agent is refused them (Agents). The pseudo-locale x-pseudo shows English accented and lengthened, to spot text that is not translated and layouts too tight for longer languages.

Tools: extracting and checking keys

egui_pop::i18n::extract finds the keys an application uses:

  • scan_tree(src, Pattern::DEFAULTS) reads Rust sources for literal keys (tr!("key"), t(ctx, "key", …), text(ctx, "key", "English", …), StrKey::new("key", "English"), lookup(ctx, "key", "English")); an application adds its own patterns (Message::new, key first, English second). scan_dir gives only the keys;
  • declarations(app, &[]) lists what a built application declares, under the derived keys.

How a call is matched: by its callee’s path, however long it is and however it is laid out. A pattern Message::new takes Message::new(…), app_core::Message::new(…), crate::strings::Message::new(…) and ::app_core::Message::new(…), with the path, the arguments or both split across lines and comments in between; t takes egui_pop_i18n::t(…). A method (x.t(…)) or a definition (fn t(…)) is not a call. The key must be a string literal and a Fluent key; a call whose key is not is skipped. A constructor, a pattern whose callee ends in ::new (StrKey::new, Message::new), declares a message, so a constructor call the scanner cannot read is reported rather than skipped silently: its key is not a string literal (Message::new(id, …)), not a Fluent key, or its English is not a string literal (concat!(…), a constant; the key is then kept without English). Each such call is in Scan::unread, with its file and line (src/ui.rs:12: `app::Message::new(…)` not read: the key is not a string literal), and logged as a warning. The catalogue check, out_of_date(path, current, fresh, &scan.unread), gives None when the catalogue on disk is what the code gives now, else a failure message that lists those calls, the usual cause of a key missing.

messages merges them; english writes the English catalogue (sorted, deterministic), update merges them into an existing one (keeping the English written there for keys the code gives none, reporting keys added, changed and unused), and report gives, per locale, the keys missing and unused. egui_pop::i18n::check checks a catalogue directory for faults (a file that does not parse, a key defined twice, no English), coverage, and keys whose English changed since they were translated (an english.hashes sidecar per locale, written by stamp). An application wires them into its own tasks (cargo xtask i18n extract, for instance):

use egui_pop::i18n::extract::{self, English, Found, Messages};

let found = extract::scan_source(
    r#"ui.label(tr!("app-welcome")); let k = StrKey::new("app-save", "Save");"#,
    "src/main.rs",
    extract::Pattern::DEFAULTS,
);
let messages: Messages = extract::messages(&found).unwrap();
assert_eq!(messages["app-save"], English::Pattern("Save".into()));
let updated = extract::update("app-welcome = Welcome!\n", &messages, "", false);
assert_eq!(updated.added, ["app-save"]);
assert!(updated.source.contains("app-welcome = Welcome!"));

// An application's constructor, under any path; a call it cannot read
// is listed by the catalogue check.
let patterns = [extract::Pattern { callee: "Message::new", key: 0, english: Some(1) }];
let scan = extract::scan(
    "const A: Message = app_core::Message::new(\n    \"app-ready\",\n    \"Ready\",\n);\n\
     let b = app_core::Message::new(id, \"Busy\");",
    "src/status.rs",
    &patterns,
);
assert_eq!(scan.found[0].key, "app-ready");
assert_eq!(scan.unread[0].origin, "src/status.rs:5");
let failure = extract::out_of_date("en/app.ftl", "", "app-ready = Ready\n", &scan.unread);
assert!(failure.unwrap().contains("src/status.rs:5"));
let _: Vec<Found> = found;