es-fluent
Derive macros and utilities for authoring strongly-typed messages with Project Fluent.
This framework gives you:
- Derives to turn enums/structs into Fluent message IDs and arguments.
- A cli to generate ftl files skeleton and other utilities.
- Language Enum Generation
- Integration via the embedded manager, the Dioxus manager, or es-fluent-manager-bevy for Bevy
Examples
Used in
Version compatibility
| Surface | Version line | Runtime |
|---|---|---|
es-fluent, CLI, embedded manager, language enum |
0.16.x |
General Rust |
es-fluent-manager-dioxus |
0.7.x |
Dioxus 0.7.x |
es-fluent-manager-bevy |
0.18.x |
Bevy 0.18.x |
Installation
Add es-fluent; derive macros are enabled by default:
[]
= "0.16"
= "0.9"
# If you want to register modules with the embedded context and localize at runtime:
# Default zero-setup runtime manager for this quick start.
= "0.16"
# For Dioxus apps, enable only the runtime surface you use.
# es-fluent-manager-dioxus = { version = "0.7", features = ["client"] }
# es-fluent-manager-dioxus = { version = "0.7", features = ["ssr"] }
# For Bevy integration: replace `es-fluent-manager-embedded` with `es-fluent-manager-bevy`
# es-fluent-manager-bevy = "0.18.13"
es_fluent_manager_embedded::EmbeddedI18n::try_new_with_language(...) is the simplest embedded startup path:
use langid;
Use try_new_with_language_strict(...) instead when every discovered module
must support the startup locale.
For ordinary applications, keep an explicit concrete manager handle in application state and use typed lookup on that handle:
[]
= "0.16"
= "0.16"
Register the embedded module from a library-reachable module, usually
src/i18n.rs declared by pub mod i18n; in src/lib.rs:
// src/i18n.rs
pub use ;
define_i18n_module!;
use EsFluent;
use EmbeddedI18n;
use langid;
Prefer localize_message(...) on the concrete manager handle. Raw string-ID
lookup remains available in es-fluent-manager-core for integration code.
Application-facing APIs are intentionally enum-first. Custom integrations that
need to distinguish missing lookups from message ID fallback can use
FluentLocalizerExt::try_localize_message(...).
For custom runtime integrations, create a FluentManager, select the initial
language, and either wrap it in your integration type or import the public
extension trait for generic typed lookup:
[]
= "0.16"
= "0.16"
use ;
use FluentManager;
use langid;
For Dioxus, es-fluent-manager-dioxus provides a provider component,
hook-based client helpers, typed context-bound localization, and signal-backed
locale state behind the client feature. Its ssr feature provides a
request-scoped runtime. Dioxus code should use
DioxusI18n::localize_message(...), ManagedI18n::localize_message(...), or
typed label helpers through the component or SSR request context.
Dioxus does not use the generic embedded localizer handle or install a
process-wide localizer.
For Bevy, systems that need direct localization can request BevyI18n as a
SystemParam and call localize_message(...) on it. The plugin also exposes
RequestedLanguageId and ActiveLanguageId for systems that need to
distinguish user intent from the currently published locale. Detailed Bevy
asset readiness and fallback-manager behavior is documented in
crates/es-fluent-manager-bevy/docs/ARCHITECTURE.md.
Project configuration
For a new crate, start with the CLI scaffold:
This creates i18n.toml, assets/locales/en/, src/i18n.rs, and a
pub mod i18n; declaration in src/lib.rs. Use --manager dioxus or
--manager bevy for framework-specific scaffolding, and --build-rs to add
locale asset rebuild tracking. Use --locales fr-FR,zh-CN to create more
locale directories, --namespaces ui,errors to write a namespace allowlist,
and --update-cargo-toml to add the matching dependencies. When --build-rs
is also passed, the manifest update includes es-fluent-build under
[build-dependencies]. For Dioxus manifests, --dioxus-runtime client,
--dioxus-runtime ssr, or
--dioxus-runtime client,ssr selects the generated manager features; omitting
it enables both.
Before writing anything, init checks generated-file conflicts, directory
targets, and Cargo.toml parseability when manifest updates are requested.
init creates a library target because CLI inventory collection reads library
targets. Put derived message types in src/lib.rs or another library crate;
binary-only derived types in src/main.rs are not discovered by generate.
Or create an i18n.toml next to your Cargo.toml manually:
# Default fallback language (required)
= "en"
# Path to FTL assets relative to the config file (required)
= "assets/locales"
# Features to enable if the crate’s es-fluent derives are gated behind a feature (optional)
= ["my-feature"]
# Optional allowlist of namespace values for FTL file splitting
= ["ui", "errors", "messages"]
Locale directory names use canonical BCP-47 tags. The executable README example
ships en, fr-FR, and zh-CN, with en as the fallback locale.
Add a new language later by seeding it from the fallback locale:
For pre-commit or CI checks, cargo es-fluent status --all reports pending
generation, formatting, sync, orphan cleanup, and validation work without
writing files.
Incremental builds for locale assets
If your crate uses the embedded, Dioxus, or Bevy manager macros, they discover
locales at compile time by scanning assets_dir. To ensure locale folder/file
renames (for example fr to fr-FR) trigger rebuilds, add es-fluent-build
to build dependencies and call the tracking helper from build.rs. Crates that
only use the derive macros do not need this setup.
[]
= "0.16"
// build.rs
Namespaces (optional)
You can route specific types into separate .ftl files by adding a namespace. All derive macros support the same namespace options:
EsFluent
use EsFluent;
;
EsFluentLabel
use EsFluentLabel;
;
;
EsFluentVariants
use EsFluentVariants;
Output Layout
- Default:
assets_dir/{locale}/{crate}.ftl - Namespaced:
assets_dir/{locale}/{crate}/{namespace}.ftl
When namespaces are used, namespace files are treated as the canonical split
for that locale, and {crate}.ftl can still participate as an optional base
resource for non-namespaced messages.
Namespace Values
namespace = "name"- explicit namespace stringnamespace = file- uses the source file stem (e.g.,src/ui/button.rs->button)namespace(file(relative))- uses the file path relative to the crate root, stripssrc/, and removes the extension (e.g.,src/ui/button.rs->ui/button)namespace = folder- uses the source file parent folder (e.g.,src/ui/button.rs->ui)namespace(folder(relative))- uses the parent folder path relative to the crate root, stripssrc/when nested, and keepssrcfor root module files (e.g.,src/ui/button.rs->ui)
Literal string namespaces are validated at compile time as safe relative namespace paths. If namespaces = [...] is set in i18n.toml, both the compiler and the CLI validate that string-based namespaces used by your code are in that allowlist.
Derives
#[derive(EsFluent)]
Turns an enum or struct into a localizable message.
- Enums: Each variant becomes a message ID (e.g.,
MyEnum::Variant->my_enum-Variant). - Structs: The struct itself becomes the message ID (e.g.,
MyStruct->my_struct). - Fields: Fields are automatically exposed as arguments to the Fluent message.
use ;
let _ = i18n.localize_message;
let _ = i18n.localize_message;
let _ = i18n.localize_message;
let _ = i18n.localize_message;
let welcome = WelcomeMessage ;
let _ = i18n.localize_message;
Common derive attributes:
arg_name = "..."on a field renames that exposed Fluent argument (works on struct fields, enum named fields, and enum tuple fields).#[fluent(skip)]on a field excludes that field from generated arguments.#[fluent(value = "...")]or#[fluent(value(...))]transforms a field before inserting it as a Fluent argument.#[fluent(key = "...")]on an enum variant overrides that variant's key suffix.#[fluent(resource = "...")]on an enum overrides the base key,domain = "..."routes lookup to a specific manager domain, andskip_inventorysuppresses CLI inventory registration.domain = "..."is enum-only. Struct messages resolve in the current crate's domain.- Optional-argument omission is generated for direct
Option<T>fields, including paths likestd::option::Option<T>. Type aliases toOption<T>are treated like ordinary field types. #[fluent_variants(skip)]omits a struct field or enum variant from generated variant enums;keys = [...]values must be lowercase snake_case.
Skipped single-field enum variants:
#[fluent(skip)] on a single-field enum variant suppresses that variant's own
key and delegates context-bound rendering to the wrapped value. This is useful for
transparent wrapper enums.
use EsFluent;
let _ = i18n.localize_message;
## NetworkError
network_error-ApiUnavailable = API is unavailable
#[derive(EsFluentChoice)]
Allows an enum to be used inside another message as a selector (e.g., for gender or status).
use ;
use FluentMessage;
let greeting = Greeting ;
let _ = i18n.localize_message;
#[derive(EsFluentVariants)]
Generates key-value pair enums for struct fields or enum variants. This is useful for generating UI labels, placeholders, or descriptions for a form object, and it can also expose enum variants as localizable keys.
use EsFluentVariants;
// Generates enums -> keys:
// LoginFormVariantsLabelVariants::{Variants} -> (login_form_variants_label_variants-{variant})
// LoginFormVariantsDescriptionVariants::{Variants} -> (login_form_variants_description_variants-{variant})
use FluentMessage;
let _ = i18n.localize_message;
// Generates enum -> keys:
// SettingsTabVariants::{General, Notifications, Privacy}
// -> (settings_tab_variants-{variant})
let _ = i18n.localize_message;
#[derive(EsFluentLabel)]
Generates a helper implementation of the FluentLabel trait and registers the
type's name as a key. This is similar to EsFluentVariants (which registers
field- or variant-derived keys), but for the parent type itself.
origin: Enabled by default.#[derive(EsFluentLabel)]and#[derive(EsFluentLabel)] #[fluent_label(origin)]both generate the type-level label. Use#[fluent_label(origin = false)]when deriving only variant labels throughEsFluentVariants.#[fluent_label(origin)]: Explicitly generates an implementation wherelocalize_label(localizer)returns the base key for the type.
use EsFluentLabel;
// Generates key:
// (gender_label_only_label)
use FluentLabel;
let _ = localize_label;
#[fluent_label(variants)]: Can be combined withEsFluentVariantsderives to generate keys for variants.
// Generates keys:
// (login_form_combined_label_variants_label)
// (login_form_combined_description_variants_label)
use FluentLabel;
let _ = localize_label;