i18n-gen

Crates.ioi18n-gen
lib.rsi18n-gen
version0.1.0
created_at2025-09-22 10:43:54.155393+00
updated_at2025-09-22 10:43:54.155393+00
descriptionCLI tool that generates fast, type-safe Rust i18n code (MessageKey enum + PHF maps) from JSON schema and locales.
homepage
repositoryhttps://github.com/sumitsharansatsangi/i18n-runtime
max_upload_size
id1849829
size20,234
Sumit Kumar (sumitsharansatsangi)

documentation

README

⚡ 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 MessageKey enum (type-safe keys)
  • ⚡ Produces phf maps 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

  1. Define your schema in messages.schema.json:
{
  "keys": ["welcome", "login_success", "login_failed"]
}
  1. Add locale files in locales/. Example locales/en.json:
{
  "welcome": "Welcome!",
  "login_success": "You have logged in successfully.",
  "login_failed": "Login failed. Please try again."
}
  1. Run the generator:
i18n-gen ./ ./src/generated_i18n
  1. 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

  1. Edit your JSON files in locales/
  2. Re-run i18n-gen to regenerate Rust code
  3. 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


📜 License

MIT


Commit count: 3

cargo fmt