| Crates.io | rust-l10n |
| lib.rs | rust-l10n |
| version | 0.1.1 |
| created_at | 2025-08-10 12:50:16.908294+00 |
| updated_at | 2025-08-10 22:43:57.847072+00 |
| description | A lightweight internationalization library ported from go-l10n |
| homepage | |
| repository | https://github.com/ideamans/rust-l10n |
| max_upload_size | |
| id | 1788885 |
| size | 51,620 |
A lightweight internationalization (i18n) library for Rust, inspired by go-l10n. This library provides simple, distributed translation support with automatic language detection from environment variables.
LANGUAGE, LC_ALL, LC_MESSAGES, etc.)t(), f(), e()Add this to your Cargo.toml:
[dependencies]
rust-l10n = "0.1"
ctor = "0.2" # Required for automatic initialization
use ctor::ctor;
use rust_l10n::{register_translations, t, f};
// Register translations at module initialization
#[ctor]
fn init() {
register_translations! {
ja: {
"Hello" => "γγγ«γ‘γ―",
"Welcome to {}" => "{}γΈγγγγ",
},
es: {
"Hello" => "Hola",
"Welcome to {}" => "Bienvenido a {}",
}
}
}
fn main() {
// Automatic language detection from environment
println!("{}", t!("Hello"));
println!("{}", f!("Welcome to {}", "Rust"));
// Force a specific language
rust_l10n::force_language("ja");
println!("{}", t!("Hello")); // Output: γγγ«γ‘γ―
}
Each module can register its own translations independently:
// auth.rs
mod auth {
use ctor::ctor;
use rust_l10n::{register_translations, t};
#[ctor]
fn init() {
register_translations! {
ja: {
"Invalid credentials" => "θͺθ¨Όζ
ε ±γη‘εΉγ§γ",
"Login successful" => "γγ°γ€γ³γ«ζεγγΎγγ",
}
}
}
pub fn login(user: &str, pass: &str) -> Result<String, String> {
// Your authentication logic here
Ok(t!("Login successful"))
}
}
// file_manager.rs
mod file_manager {
use ctor::ctor;
use rust_l10n::{register_translations, t};
#[ctor]
fn init() {
register_translations! {
ja: {
"File not found" => "γγ‘γ€γ«γθ¦γ€γγγΎγγ",
"File saved" => "γγ‘γ€γ«γδΏεγγΎγγ",
}
}
}
pub fn save_file(name: &str) -> String {
t!("File saved")
}
}
force_language()L10N_TEST_MODE environment variableLANGUAGELC_ALLLC_MESSAGESLANGL10N_DEFAULT_LANGUAGE environment variable"en"t(phrase) - Translate a phrasef(phrase, args) - Format and translate with argumentse(phrase, args) - Create translated error messageregister(lang, lexicon) - Register translations for a languageforce_language(lang) - Force a specific languagereset_language() - Reset to automatic detectiondetect_language() - Get the currently detected languaget!("phrase") - Translate macrof!("phrase {}", arg) - Format macroe!("error: {}", arg) - Error macroregister_translations! { ... } - Bulk registration macroLANGUAGE, LC_ALL, LC_MESSAGES, LANG - Standard locale variablesL10N_DEFAULT_LANGUAGE - Set default language (fallback)L10N_TEST_MODE - Force English language for consistent testingThe library provides dependency injection for environment variables, making it easy to test:
#[cfg(test)]
mod tests {
use rust_l10n::{force_language, t};
#[test]
fn test_japanese_translation() {
force_language("ja");
assert_eq!(t("Hello"), "γγγ«γ‘γ―");
}
}
Run the examples:
# Basic usage
cargo run --example basic
# Run with Japanese locale
LANGUAGE=ja cargo run --example basic
# Modular translations
cargo run --example modular
Unlike other Rust i18n libraries that use compile-time optimization and centralized translation files, rust-l10n follows a distributed approach where each module manages its own translations. This makes it particularly suitable for:
This project is licensed under either of
at your option.
Contributions are welcome! Please feel free to submit a Pull Request.
Inspired by go-l10n - A lightweight i18n library for Go.