sonix-i18n

Crates.iosonix-i18n
lib.rssonix-i18n
version0.1.1
created_at2025-12-10 22:55:58.266965+00
updated_at2025-12-10 23:11:21.559153+00
descriptionA Rust internationalization library inspired by i18next - Created by SonickSeven
homepage
repositoryhttps://gitlab.com/public-sources1/rust/sonix-i18n
max_upload_size
id1978905
size27,212
SonickSeven (sonickseven)

documentation

https://docs.rs/crate/sonix-i18n/latest

README

sonix-i18n

A Rust internationalization library inspired by i18next - Created by SonickSeven

Installation

Add the following to your Cargo.toml file:

[dependencies]
sonix-i18n = "0.1.1"

Usage

1. Create your locale files

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!"
}

2. Load the locales and initialize the library

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
}

3. Using the t! macro

The 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.

  • Basic usage: t!("namespace:key")
  • With arguments: t!("namespace:key", {"arg": "value"})

4. Changing the language

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!
}
Commit count: 0

cargo fmt