es-fluent-derive
Procedural macros for the es-fluent localization system.
This crate is the engine that transforms your Rust structs and enums into Fluent messages. It is designed to be used via the es-fluent crate, not directly.
All macros provided by this crate are fully independent and composable. You can use them individually or together on the same type depending on your needs.
Features
#[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 EsFluent;
// 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()
Namespaces (optional)
You can split generated messages into multiple .ftl files by adding a namespace:
// -> {crate}/ui.ftl
;
// -> {crate}/{file_stem}.ftl
;
// -> {crate}/ui/button.ftl
;
For EsFluentThis, the same syntax is available via #[fluent_this(namespace = "...")].
#[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(EsFluentVariants)]
Generates key-value pair enums for struct fields. This is perfect for generating UI labels, placeholders, or descriptions for a form object.
use EsFluentVariants;
// Generates enums -> keys:
// LoginFormLabelVariants::{Variants} -> (login_form_label-{variant})
// LoginFormDescriptionVariants::{Variants} -> (login_form_description-{variant})
// usage: LoginFormLabelVariants::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 EsFluentVariants (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 withEsFluentVariantsderives to generate keys for members.
// Generates keys:
// (login_form_label_this)
// (login_form_description_this)
// usage: LoginFormDescriptionVariants::this_ftl()