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.
- 🌍 Automatic Language Detection - Detects language from environment variables (
LANGUAGE,LC_ALL,LC_MESSAGES, etc.) - 📦 Distributed Translation Registration - Each module can register its own translations independently
- 🚀 Simple API - Easy-to-use functions:
t(),f(),e() - 🔧 Zero Configuration - Works out of the box with sensible defaults
- 🧪 Test-Friendly - Dependency injection for environment variables makes testing easy
- ⚡ Lightweight - Minimal dependencies and small binary size
Add this to your Cargo.toml:
[dependencies]
rust-l10n = "0.1"
ctor = "0.2" # Required for automatic initializationuse 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")
}
}- Forced language via
force_language() L10N_TEST_MODEenvironment variable- Standard locale environment variables:
LANGUAGELC_ALLLC_MESSAGESLANG
L10N_DEFAULT_LANGUAGEenvironment variable- Default fallback:
"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 language
t!("phrase")- Translate macrof!("phrase {}", arg)- Format macroe!("error: {}", arg)- Error macroregister_translations! { ... }- Bulk registration macro
LANGUAGE,LC_ALL,LC_MESSAGES,LANG- Standard locale variablesL10N_DEFAULT_LANGUAGE- Set default language (fallback)L10N_TEST_MODE- Force English language for consistent testing
The 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 modularUnlike 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:
- Porting projects from Go
- Microservices and modular applications
- Projects where translations should live close to the code
- Applications requiring runtime translation registration
This project is licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributions are welcome! Please feel free to submit a Pull Request.
Inspired by go-l10n - A lightweight i18n library for Go.