rust-l10n
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.
Features
- 🌍 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
Installation
Add this to your Cargo.toml:
[]
= "0.1"
= "0.2" # Required for automatic initialization
Quick Start
use ctor;
use ;
// Register translations at module initialization
Distributed Translation Registration
Each module can register its own translations independently:
// auth.rs
// file_manager.rs
Language Detection Priority
- Forced language via
force_language() L10N_TEST_MODEenvironment variable- Standard locale environment variables:
LANGUAGELC_ALLLC_MESSAGESLANG
L10N_DEFAULT_LANGUAGEenvironment variable- Default fallback:
"en"
API Reference
Core Functions
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
Macros
t!("phrase")- Translate macrof!("phrase {}", arg)- Format macroe!("error: {}", arg)- Error macroregister_translations! { ... }- Bulk registration macro
Environment Variables
LANGUAGE,LC_ALL,LC_MESSAGES,LANG- Standard locale variablesL10N_DEFAULT_LANGUAGE- Set default language (fallback)L10N_TEST_MODE- Force specific language for testingL10N_SKIP_DETECTION- Disable automatic detection
Testing
The library provides dependency injection for environment variables, making it easy to test:
Examples
Run the examples:
# Basic usage
# Run with Japanese locale
LANGUAGE=ja
# Modular translations
Comparison with Other i18n Libraries
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:
- Porting projects from Go
- Microservices and modular applications
- Projects where translations should live close to the code
- Applications requiring runtime translation registration
License
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.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Credits
Inspired by go-l10n - A lightweight i18n library for Go.