| Crates.io | sonix-i18n |
| lib.rs | sonix-i18n |
| version | 0.1.1 |
| created_at | 2025-12-10 22:55:58.266965+00 |
| updated_at | 2025-12-10 23:11:21.559153+00 |
| description | A Rust internationalization library inspired by i18next - Created by SonickSeven |
| homepage | |
| repository | https://gitlab.com/public-sources1/rust/sonix-i18n |
| max_upload_size | |
| id | 1978905 |
| size | 27,212 |
A Rust internationalization library inspired by i18next - Created by SonickSeven
Add the following to your Cargo.toml file:
[dependencies]
sonix-i18n = "0.1.1"
This library uses the same structure as i18next. You need to create a JSON
file for each language you want to support. The files should be structured as
follows:
locales/en/common.json
{
"welcome": "Welcome to our application {{user}}!",
"greet": "Hello"
}
locales/es/common.json
{
"welcome": "¡Bienvenido a nuestra aplicación {{user}}!",
"greet": "¡Hola!"
}
You need to load the content of your locale files into a HashMap and then pass
it to the init function.
use std::collections::HashMap;
use serde_json::Value;
use sonix_i18n::{init, t};
fn main() {
let mut resources = HashMap::new();
// Load English translations
let en_common: Value = serde_json::from_str(include_str!("../locales/en/common.json")).unwrap();
let mut en_map = HashMap::new();
en_map.insert("common".to_string(), en_common);
resources.insert("en".to_string(), en_map);
// Load Spanish translations
let es_common: Value = serde_json::from_str(include_str!("../locales/es/common.json")).unwrap();
let mut es_map = HashMap::new();
es_map.insert("common".to_string(), es_common);
resources.insert("es".to_string(), es_map);
// Initialize the library
init(resources, Some("en".to_string())).unwrap();
// Use the t! macro to get translations
println!("{}", t!("common:welcome", {"user": "John"}));
println!("{}", t!("common:greet")); // without arguments
}
t! macroThe t! macro is the main way to get translations. It takes a key as an
argument, which is a string that identifies the translation you want to get.
t!("namespace:key")t!("namespace:key", {"arg": "value"})You can change the language at runtime using the change_language function.
use sonix_i18n::{change_language, t};
fn main() {
// ... initialization code ...
println!("{}", t!("common:welcome", {"user": "John"})); // -> Welcome to our application John!
change_language("es").unwrap();
println!("{}", t!("common:welcome", {"user": "John"})); // -> ¡Bienvenido a nuestra aplicación John!
}