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 a embedded singleton manager or es-fluent-manager-bevy for bevy
Examples
Used in
Installation
Add the crate with the derive feature to access the procedural macros:
[]
= { = "*", = ["derive"] }
= "*"
# If you want to register modules with the embedded singleton and localize at runtime:
= "*"
# For Bevy integration: replace `es-fluent-manager-embedded` with `es-fluent-manager-bevy`
= "*"
Project configuration
Create an i18n.toml next to your Cargo.toml:
# Default fallback language (required)
= "en-US"
# 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"]
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 ;
// Usage:
// LoginError::InvalidPassword.to_fluent_string()
// LoginError::UserNotFound { username: "john" }.to_fluent_string()
// LoginError::Something("a", "b", "c").to_fluent_string()
// usage: UserProfile { name: "John", gender: "male" }.to_fluent_string()
#[derive(EsFluentChoice)]
Allows an enum to be used inside another message as a selector (e.g., for gender or status).
use ;
// usage: UserProfile { name: "John", gender: &Gender::Male }.to_fluent_string()
#[derive(EsFluentKv)]
Generates key-value pair enums for struct fields. This is perfect for generating UI labels, placeholders, or descriptions for a form object.
use EsFluentKv;
// Generates enums -> keys:
// LoginFormLabelKvFtl::{Variants} -> (login_form_label_kv_ftl-{variant})
// LoginFormDescriptionKvFtl::{Variants} -> (login_form_description_kv_ftl-{variant})
// usage: LoginFormLabelKvFtl::Username.to_fluent_string()
#[derive(EsFluentThis)]
Generates a helper implementation of the ThisFtl trait and registers the type's name as a key. This is similar to EsFluentKv (which registers fields), but for the parent type itself.
#[fluent_this(origin)]: Generates an implementation wherethis_ftl()returns the base key for the type.
use EsFluentThis;
// Generates key:
// (gender_this)
// usage: Gender::this_ftl()
#[fluent_this(members)]: Can be combined withKvderives to generate keys for members.
// Generates keys:
// (login_form_label_kv_ftl_this)
// (login_form_description_kv_ftl_this)
// usage: LoginFormDescriptionKvFtl::this_ftl()