A tiny, fast localization library with zero runtime dependencies and #![no_std] support.
This crate provides a macro-based API for defining compile-time locales and localized
expressions without dynamic memory allocation or hash maps. All localized expressions
are stored as static arrays, enabling localization via simple indexing. The locale can
be managed manually or via the built-in AtomicUsize storage with Relaxed ordering.
A program that asks the user to choose a language, enter their name, and then greets them in the selected language:
use localize_it::init_locale;
use std::io::{stdin, stdout, Write};
// Define available locales and enable built-in storage
init_locale!(En, Ru, storage = true);
// Define localized expressions (can be any compile-time type)
expressions!(
ENTER_LANGUAGE => {
En: "Enter En or Ru: ",
Ru: "Введите En или Ru: ",
},
ENTER_YOUR_NAME => {
En: "Please, enter your name: ",
Ru: "Пожалуйста, введите ваше имя: ",
},
HELLO: fn(&str) -> String => {
En: |name: &str| format!("Hello, {name}!"),
Ru: |name: &str| format!("Привет, {name}!"),
},
);
// Simple input helper for demonstration purposes
fn input() -> String {
let mut temp = String::new();
stdout().flush().unwrap();
stdin().read_line(&mut temp).unwrap();
temp.trim().to_string()
}
fn main() {
// Explicitly use a specific locale
print!("{}", localize!(ENTER_LANGUAGE, Locale::En));
let lang = input();
// Set the locale in storage, falls back to default (En)
storage::set_from_caseless_str_or_default(&lang);
// Use the locale from storage
print!("{}", localize!(ENTER_YOUR_NAME));
let name = input();
// Use a callable expression
println!("{}", localize!(HELLO as (&name)));
}A recommended way to organize your project is to create a dedicated locale module that handles locale
initialization and contains grouped expression modules. For example:
src/
├─ main.rs
└─ locale/
├─ mod.rs # Initialization locale here
├─ error.rs # First module with expressions
└─ ui.rs # Second module with expressions
- Not possible to update or add the translations without recompiling
- No plans to add automatic gender agreement, numeral declension, etc
Add the following to your Cargo.toml:
[dependencies]
localize_it = "2.2.0"This project is licensed under either of
at your option.