🌐 Internationalization (i18n)
Global enterprise applications require serving localized user interface strings and translated error messages based on the client's Accept-Language HTTP header.
ferrox-i18n provides multi-language translation dictionaries and locale resolution.
1. Dictionary Setup
use ferrox_i18n::I18nManager;
let mut i18n = I18nManager::new("en");
// Register translations
i18n.add_translation("en", "welcome", "Welcome to Ferrox!");
i18n.add_translation("it", "welcome", "Benvenuto in Ferrox!");
i18n.add_translation("es", "welcome", "¡Bienvenido a Ferrox!");
2. Resolving Translations from HTTP Headers
Extract locale from Axum request headers and translate message keys:
use axum::http::HeaderMap;
pub fn get_localized_greeting(headers: &HeaderMap, i18n: &I18nManager) -> String {
let accept_lang = headers.get("Accept-Language")
.and_then(|v| v.to_str().ok())
.unwrap_or("en");
let locale = i18n.resolve_locale(accept_lang);
i18n.translate(locale, "welcome")
}