| Crates.io | i18n-rs |
| lib.rs | i18n-rs |
| version | 1.0.0 |
| created_at | 2019-04-09 12:14:48.094654+00 |
| updated_at | 2025-11-02 15:51:42.552142+00 |
| description | A Rust library for internationalization (i18n) support |
| homepage | |
| repository | |
| max_upload_size | |
| id | 126814 |
| size | 18,038 |
i18n-rs is a lightweight internationalization helper designed to be embedded in any Rust web framework. It focuses on a familiar JSON-based translation file so you can reuse the dictionaries you already maintain while keeping your web stack framework agnostic.
Add the crate to your project:
[dependencies]
i18n-rs = "1.0.0"
The crate expects a JSON object whose top-level keys are language identifiers. Each language contains nested objects and/or string values. Nested keys are automatically flattened using dot notation.
{
"en": {
"greeting": {
"welcome": "Welcome",
"farewell": "Goodbye"
}
},
"fr": {
"greeting": {
"welcome": "Bienvenue"
}
}
}
The example above yields the keys greeting.welcome and greeting.farewell.
use i18n_rs::Translator;
static JSON: &str = r#"{
"en": {
"welcome": "Welcome!",
"farewell": "See you later!",
"button": {
"submit": "Submit"
}
},
"es": {
"welcome": "¡Bienvenido!",
"button": {
"submit": "Enviar"
}
}
}"#;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let translator = Translator::builder()
.fallback_language("en")
.load_from_str(JSON)?;
assert_eq!(
translator.translate("es", "button.submit"),
Some("Enviar")
);
// Missing keys gracefully use the fallback language.
assert_eq!(
translator.translate_with_fallback("es", "farewell"),
Some("See you later!")
);
Ok(())
}