1 unstable release
Uses new Rust 2024
| 0.1.0 | Sep 22, 2025 |
|---|
#112 in Internationalization (i18n)
13KB
159 lines
⚡ i18n-gen
A CLI tool that generates fast, type-safe Rust code for translations.
Designed to work seamlessly with i18n-runtime.
✨ Features
- 🔧 Reads
messages.schema.json+locales/*.json - 🔑 Generates a
MessageKeyenum (type-safe keys) - ⚡ Produces
phfmaps for O(1) lookups - 📂 Outputs clean Rust files into
src/generated_i18n/ - 🔄 Works with
i18n-runtime’s fallback logic (en-IN-BR → en-IN → en)
📦 Installation
cargo install i18n-gen
🚀 Usage
- Define your schema in
messages.schema.json:
{
"keys": ["welcome", "login_success", "login_failed"]
}
- Add locale files in
locales/. Examplelocales/en.json:
{
"welcome": "Welcome!",
"login_success": "You have logged in successfully.",
"login_failed": "Login failed. Please try again."
}
- Run the generator:
i18n-gen ./ ./src/generated_i18n
- Generated files:
src/generated_i18n/generated_keys.rs # MessageKey enum
src/generated_i18n/locales/EN.rs # en.json → phf::Map
src/generated_i18n/locales/HI_IN.rs # hi-IN.json → phf::Map
src/generated_i18n/mod.rs # registry
📦 Integration in your app
In src/main.rs:
// include generated code
include!("generated_i18n/generated_keys.rs");
mod generated_i18n { include!("generated_i18n/mod.rs"); }
use i18n_runtime::{I18n, Locale};
fn main() {
let registry = generated_i18n::get_generated_registry();
let i18n = I18n::from_generated_registry(registry, "en");
let msg = i18n
.get_by_str_key(&Locale::new("en"), MessageKey::Welcome.as_str())
.unwrap();
println!("{}", msg); // Welcome!
}
🔄 Workflow
- Edit your JSON files in
locales/ - Re-run
i18n-gento regenerate Rust code - Rebuild your app → translations are now compiled in and lightning-fast ⚡
❓ FAQ
Q: Do I need this tool?
➡️ Only if you want compile-time PHF mode for maximum performance.
For dynamic translation loading, use i18n-runtime alone.
Q: Should I check in generated files to git? ➡️ Yes, recommended. Makes builds reproducible and avoids requiring the generator on every machine.
Q: Can I run it automatically in build.rs?
➡️ Yes! Just call i18n-gen from your build script. Or run it manually in CI.
🧩 Ecosystem
- i18n-runtime — runtime API
- i18n-gen — generator CLI (this crate)
📜 License
MIT
Dependencies
~1–4MB
~76K SLoC