feat(i18n): add i18n on server side

- remove the hardcoded list of locales in favor of a discovry on start time
    - server will stop on badly formatted locale .json
    - add server.* entries for serer side translation

    server side translation will be used for templating and email
    note: no json in some embded html (like in /magic), amount of work was similar
This commit is contained in:
Edouard Vanbelle
2026-06-03 13:18:05 +02:00
parent e0f59aa942
commit 044bd76738
32 changed files with 1520 additions and 149 deletions
+36 -11
View File
@@ -14,18 +14,43 @@ pub struct LocaleDto {
impl From<Locale> for LocaleDto {
fn from(locale: Locale) -> Self {
let (code, name) = match locale {
Locale::English => ("en", "English"),
Locale::Spanish => ("es", "Español"),
Locale::French => ("fr", "Français"),
Locale::German => ("de", "Deutsch"),
Locale::Portuguese => ("pt", "Português"),
};
Self::from(&locale)
}
}
Self {
code: code.to_string(),
name: name.to_string(),
}
impl From<&Locale> for LocaleDto {
fn from(locale: &Locale) -> Self {
let code = locale.as_str().to_string();
let name = display_name_for(&code)
.map(str::to_string)
.unwrap_or_else(|| code.clone());
Self { code, name }
}
}
/// Endonym lookup for the locales shipped under `static/locales/`. New
/// locales added in PR-A's `LocaleRegistry::discover` should be added
/// here too; an unknown code falls back to itself, which is safe but
/// looks rough in a language switcher.
fn display_name_for(code: &str) -> Option<&'static str> {
match code {
"en" => Some("English"),
"es" => Some("Español"),
"fr" => Some("Français"),
"de" => Some("Deutsch"),
"pt" => Some("Português"),
"it" => Some("Italiano"),
"nl" => Some("Nederlands"),
"pl" => Some("Polski"),
"ru" => Some("Русский"),
"ja" => Some("日本語"),
"ko" => Some("한국어"),
"zh" => Some("中文"),
"zh-tw" => Some("繁體中文"),
"ar" => Some("العربية"),
"fa" => Some("فارسی"),
"hi" => Some("हिन्दी"),
_ => None,
}
}
@@ -23,8 +23,9 @@ impl I18nApplicationService {
/// Get a translation for a key and locale
pub async fn translate(&self, key: &str, locale: Option<Locale>) -> I18nResult<String> {
let locale = locale.unwrap_or_default();
self.i18n_service.translate(key, locale).await
self.i18n_service
.translate(key, locale.unwrap_or_default())
.await
}
/// Load translations for a locale
@@ -38,7 +39,7 @@ impl I18nApplicationService {
let mut results = Vec::new();
for locale in locales {
let result = self.i18n_service.load_translations(locale).await;
let result = self.i18n_service.load_translations(locale.clone()).await;
results.push((locale, result));
}