egui_pop_i18n/lib.rs
1//! Translations for egui-pop applications, over Fluent.
2//!
3//! - [`I18nPlugin`]: the plugin. It loads catalogues (egui-pop's own,
4//! the application's, an override directory), declares the
5//! `appearance.language` setting, publishes `locale.changed`, declares
6//! the `i18n.resolve` step, adds the `i18n` debug collector, and
7//! installs the interface's strings provider so egui-pop's own text and
8//! what plugins declare are translated (see [`plugin`]).
9//! - [`I18n`]: the catalogues and the locale, shared; [`t`], [`text`]
10//! and [`tr!`] look text up. Without `App`, [`install`] puts an
11//! [`I18n`] in an egui context for them and for egui-pop's own text. A lookup tries the locale, its parents
12//! (`fr-CA`, then `fr`), then English; a key missing everywhere shows
13//! the English written in the code, else the key.
14//! - [`format`](mod@format): numbers, dates and plurals for the locale,
15//! in Rust and in catalogues (`NUMBER`, `DATETIME`).
16//! - [`locale`]: tags, the locale to use, fallback chains.
17//! - [`extract`] and [`check`]: for tools, the keys the code uses, the
18//! English catalogue written from them, and per locale the keys
19//! missing, unused or changed since translated.
20//!
21//! ```
22//! use egui_pop_api::App;
23//! use egui_pop_i18n::{Catalogue, I18nPlugin, args, plugin, t};
24//!
25//! static CATALOGUES: &[Catalogue] = &[
26//! Catalogue::new("en", "app.ftl", "hello = Hello, { $name }!\n"),
27//! Catalogue::new("fr", "app.ftl", "hello = Bonjour, { $name } !\n"),
28//! ];
29//!
30//! let ctx = egui::Context::default();
31//! let mut app = App::builder()
32//! .plugin(I18nPlugin::new().catalogues(CATALOGUES))
33//! .build(&ctx)
34//! .unwrap();
35//! plugin::set_language(app.handle(), "fr").unwrap();
36//! let mut said = String::new();
37//! let mut out = ctx.run_ui(egui::RawInput::default(), |ui| {
38//! app.show(ui);
39//! said = t(ui.ctx(), "hello", &args![name = "Ada"]);
40//! });
41//! out.textures_delta.clear();
42//! assert_eq!(said, "Bonjour, Ada !");
43//! ```
44
45pub mod catalog;
46pub mod check;
47pub mod extract;
48pub mod format;
49mod i18n;
50pub mod locale;
51pub mod plugin;
52
53pub use fluent_bundle::{FluentArgs, FluentValue};
54pub use i18n::{Args, Catalogue, EGUI_POP, I18n, Provider};
55pub use plugin::{I18nPlugin, ShowKeys, global, install, t, text};
56
57/// The text of a key from the catalogues last installed, by
58/// [`install`] or the connected [`I18nPlugin`] ([`global`]), with named
59/// arguments; the key itself
60/// when no catalogue has it. The extractor finds its literal keys.
61///
62/// ```
63/// use egui_pop_i18n::tr;
64///
65/// assert_eq!(tr!("some-key"), "some-key");
66/// assert_eq!(tr!("files", count = 3), "files");
67/// ```
68#[macro_export]
69macro_rules! tr {
70 ($key:literal $(, $name:ident = $value:expr)* $(,)?) => {
71 $crate::global().t($key, &$crate::args![$($name = $value),*])
72 };
73}