Skip to main content

Crate reinhardt_i18n

Crate reinhardt_i18n 

Source
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§

po_parser
Gettext .po file parser
utils
Utility functions for internationalization

Structs§

CatalogLoader
Catalog loader for loading message catalogs from files or other sources
LazyString
A lazily-evaluated translation string
MessageCatalog
A message catalog containing translations for a specific locale
TranslationContext
Translation context containing catalogs and locale settings.
TranslationGuard
RAII guard for active TranslationContext scope.

Enums§

I18nError
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.