rust-i18n-derive

Crates.iorust-i18n-derive
lib.rsrust-i18n-derive
version1.0.1
created_at2025-12-30 18:57:46.194655+00
updated_at2025-12-30 22:24:10.326497+00
descriptionA Rust derive macro for internationalization (i18n) support.
homepage
repositoryhttps://github.com/xairaven/rust-i18n-derive
max_upload_size
id2013151
size9,550
Alex Kovalov (xairaven)

documentation

README

rust-i18n-derive

Crates.io Docs License

A concise, type-safe derive macro for rust-i18n key mapping.

rust-i18n-derive provides a convenient way to bind Rust Enums to your translation keys. Instead of using raw string literals (like "menu.open") throughout your codebase, you can map them once in an Enum definition and use the generated methods to retrieve keys or localized strings.

This crate works alongside rust-i18n, acting as a bridge between strongly typed Rust structures and localization files.

🚀 Features

  • Type-Safe Keys: Use Enum variants (e.g., Label::Login) instead of error-prone string literals.
  • Centralized Mapping: Keep the association between code and translation keys in one place.
  • Compile-time Enforcement: The macro ensures that every enum variant has a mapped key via #[tag("...")].
  • Helper Methods: Automatically implements .key() and .localize() methods for your Enums.

📖 Usage

  1. Setup rust-i18n

    Ensure you have initialized rust-i18n in your project root (usually in lib.rs or main.rs), pointing to your locales directory:

    // Load I18n macro (from rust-i18n crate)
    rust_i18n::i18n!("locales");
    
  2. Derive Localized

    Import the trait and derive macro, then annotate your enum variants with #[tag("...")].

    use rust_i18n_derive::Localized;
    
    #[derive(Debug, Localized)]
    pub enum MainMenu {
        #[tag("menu.file.open")]
        Open,
    
        #[tag("menu.file.save")]
        Save,
    
        #[tag("menu.help.about")]
        About,
    }
    
    fn main() {
        let item = MainMenu::Open;
    
        // 1. Get the raw key (useful for logging or debugging)
        assert_eq!(item.key(), "menu.file.open");
    
        // 2. Get the localized string (uses rust_i18n::t! under the hood)
        // Assuming you have a locale file where "menu.file.open" = "Open File"
        println!("{}", item.localize()); 
    }
    

🛠 How it works

The macro generates an implementation of the Localized trait for your Enum.

For the example above, it generates code equivalent to:

impl Localized for MainMenu {
    fn key(&self) -> &'static str {
        match self {
            Self::Open => "menu.file.open",
            Self::Save => "menu.file.save",
            Self::About => "menu.help.about",
        }
    }

    fn localize(&self) -> String {
        rust_i18n::t!(self.key())
    }
}

⚠️ Error Handling

The macro validates your code at compile time. If you forget to add a #[tag("...")] attribute to a variant, the compiler will panic with a helpful error message pointing exactly to the missing line:

error: Missing #[tag("...")] for variant `Close`
  --> src/main.rs:15:5
   |
15 |     Close,
   |     ^^^^^

🤝 Contribution

Contributions are welcome! Please feel free to submit a Pull Request or open an issue on GitHub.

📄 License

This project is licensed under the MIT License.

Commit count: 0

cargo fmt