rust-l10n

Crates.iorust-l10n
lib.rsrust-l10n
version0.1.1
created_at2025-08-10 12:50:16.908294+00
updated_at2025-08-10 22:43:57.847072+00
descriptionA lightweight internationalization library ported from go-l10n
homepage
repositoryhttps://github.com/ideamans/rust-l10n
max_upload_size
id1788885
size51,620
Kunihiko Miyanaga (miyanaga)

documentation

README

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.

ζ—₯本θͺžη‰ˆ README

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:

[dependencies]
rust-l10n = "0.1"
ctor = "0.2"  # Required for automatic initialization

Quick Start

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: こんにけは
}

Distributed Translation Registration

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

Language Detection Priority

  1. Forced language via force_language()
  2. L10N_TEST_MODE environment variable
  3. Standard locale environment variables:
    • LANGUAGE
    • LC_ALL
    • LC_MESSAGES
    • LANG
  4. L10N_DEFAULT_LANGUAGE environment variable
  5. Default fallback: "en"

API Reference

Core Functions

  • t(phrase) - Translate a phrase
  • f(phrase, args) - Format and translate with arguments
  • e(phrase, args) - Create translated error message
  • register(lang, lexicon) - Register translations for a language
  • force_language(lang) - Force a specific language
  • reset_language() - Reset to automatic detection
  • detect_language() - Get the currently detected language

Macros

  • t!("phrase") - Translate macro
  • f!("phrase {}", arg) - Format macro
  • e!("error: {}", arg) - Error macro
  • register_translations! { ... } - Bulk registration macro

Environment Variables

  • LANGUAGE, LC_ALL, LC_MESSAGES, LANG - Standard locale variables
  • L10N_DEFAULT_LANGUAGE - Set default language (fallback)
  • L10N_TEST_MODE - Force English language for consistent testing

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"), "こんにけは");
    }
}

Examples

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

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

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.

Commit count: 0

cargo fmt