Expand description
Internationalization (i18n) support for Reinhardt
This crate provides Django-style internationalization features including:
- Message translation with gettext-style API
- Plural forms support
- Context-aware translations
- Lazy translation evaluation
- Message catalog management
§Example
use reinhardt_i18n::{TranslationContext, set_active_translation, gettext, MessageCatalog};
use std::sync::Arc;
// Create a translation context with Japanese catalog
let mut ctx = TranslationContext::new("ja", "en-US");
let mut catalog = MessageCatalog::new("ja");
catalog.add_translation("Hello", "こんにちは");
ctx.add_catalog("ja", catalog).unwrap();
// Set as active translation context (scoped)
let _guard = set_active_translation(Arc::new(ctx));
// Translate messages
let greeting = gettext("Hello");
assert_eq!(greeting, "こんにちは");
// Guard is dropped here, restoring previous context§DI Integration
When the di feature is enabled, TranslationContext implements Injectable:
ⓘ
use reinhardt_di::{InjectionContext, SingletonScope, Injectable};
use reinhardt_i18n::TranslationContext;
async fn handler(ctx: &InjectionContext) {
let translation = TranslationContext::inject(ctx).await.unwrap();
// Use translation...
}Modules§
Structs§
- Catalog
Loader - Catalog loader for loading message catalogs from files or other sources
- Lazy
String - A lazily-evaluated translation string
- Message
Catalog - A message catalog containing translations for a specific locale
- Translation
Context - Translation context containing catalogs and locale settings.
- Translation
Guard - RAII guard for active TranslationContext scope.
Enums§
- I18n
Error - Error types for i18n operations
Functions§
- activate
- Activate a locale by creating a new translation context.
- activate_
with_ catalog - Activate a locale with its message catalog directly
- deactivate
- Deactivate the current locale and revert to English
- get_
active_ translation - Returns the currently active translation context, if any.
- get_
language - Get the currently active locale
- get_
locale - Get the currently active locale
- gettext
- Translate a message
- gettext_
lazy - Create a lazy translation that is evaluated when converted to string
- ngettext
- Translate a message with plural support
- ngettext_
lazy - Create a lazy plural translation
- npgettext
- Translate a message with context and plural support
- pgettext
- Translate a message with context
- set_
active_ translation - Sets the active translation context and returns a guard.
- set_
active_ translation_ permanent - Sets the active translation context permanently without returning a guard.