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
@@ -1,126 +1,180 @@
//! Filesystem-backed translation lookup.
//!
//! Reads JSON files under `static/locales/` (the same source the
//! frontend's `i18n.js` uses). Supports nested keys (`magic_link.invite.subject`
//! walks `{"magic_link":{"invite":{"subject":"…"}}}`) and falls back to
//! English when the resolved locale doesn't have the requested key.
//!
//! Locale validity is delegated to the [`LocaleRegistry`]
//! (`crate::common::locale`). This service does not maintain its own
//! list of supported codes — `available_locales` and `is_supported`
//! both consult the registry, so adding a 17th locale is a JSON-drop
//! operation with no Rust patch.
use serde_json::Value;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::fs;
use tokio::sync::RwLock;
use crate::domain::services::i18n_service::{I18nError, I18nResult, I18nService, Locale};
use crate::common::locale::{Locale, LocaleRegistry};
use crate::domain::services::i18n_service::{I18nError, I18nResult, I18nService};
/// File system implementation of the I18nService
pub struct FileSystemI18nService {
/// Base directory containing translation files
translations_dir: PathBuf,
/// Cached translations (locale code -> JSON data)
/// Validated registry of supported locale codes — built once at
/// startup. `None` only in the [`dummy`](Self::dummy) test path.
registry: Option<Arc<LocaleRegistry>>,
/// Cached translations (locale code → JSON tree).
cache: RwLock<HashMap<Locale, Value>>,
}
impl FileSystemI18nService {
/// Create a dummy service for testing
/// Create a dummy service for testing — no registry, no files on
/// disk. Translation lookups will fail; this exists for stubs in
/// non-i18n test code that just needs the type to compile.
pub fn dummy() -> Self {
Self {
translations_dir: PathBuf::from("/tmp/dummy_translations"),
registry: None,
cache: RwLock::new(HashMap::new()),
}
}
/// Creates a new file system i18n service
pub fn new(translations_dir: PathBuf) -> Self {
/// Construct a service rooted at `translations_dir`. The
/// [`LocaleRegistry`] should be the one built at boot (see
/// `common/di.rs`) — it gates which locale codes are accepted by
/// `is_supported` / `available_locales`.
pub fn new(translations_dir: PathBuf, registry: Arc<LocaleRegistry>) -> Self {
Self {
translations_dir,
registry: Some(registry),
cache: RwLock::new(HashMap::new()),
}
}
/// Get translation file path for a locale
fn get_locale_file_path(&self, locale: Locale) -> PathBuf {
/// Get translation file path for a locale.
fn locale_file_path(&self, locale: &Locale) -> PathBuf {
self.translations_dir
.join(format!("{}.json", locale.as_str()))
}
/// Get a nested key from JSON data
fn get_nested_value(&self, data: &Value, key: &str) -> Option<String> {
let parts: Vec<&str> = key.split('.').collect();
/// Walk a dotted key (`"server.magic_link.subject"`) against a
/// JSON tree, returning the matched string if every segment
/// resolves and the terminal value is a string.
fn lookup_nested<'a>(data: &'a Value, key: &str) -> Option<&'a str> {
let mut current = data;
for part in key.split('.') {
current = current.get(part)?;
}
current.as_str()
}
for part in &parts[0..parts.len() - 1] {
if let Some(next) = current.get(part) {
current = next;
/// Apply `{{name}}` substitutions to `template` using the
/// (name, value) pairs in `args`. Mirrors the frontend regex
/// `/\{\{\s*([^}]+)\s*\}\}/g` (see `static/js/core/i18n.js:117`):
/// unmatched placeholders are left intact, whitespace inside
/// `{{ … }}` is ignored, and the substitution is single-pass so
/// values containing `{{x}}` won't be re-expanded.
fn interpolate(template: &str, args: &[(&str, &str)]) -> String {
if args.is_empty() || !template.contains("{{") {
return template.to_string();
}
let mut out = String::with_capacity(template.len());
let mut rest = template;
while let Some(open) = rest.find("{{") {
out.push_str(&rest[..open]);
let after_open = &rest[open + 2..];
let Some(close) = after_open.find("}}") else {
// No closing braces — copy the remainder verbatim.
out.push_str("{{");
out.push_str(after_open);
return out;
};
let name = after_open[..close].trim();
let after_close = &after_open[close + 2..];
if let Some((_, value)) = args.iter().find(|(n, _)| *n == name) {
out.push_str(value);
} else {
return None;
// Unknown placeholder — preserve the literal so it's
// obvious in QA that a key wasn't passed.
out.push_str("{{");
out.push_str(&after_open[..close]);
out.push_str("}}");
}
rest = after_close;
}
if let Some(last_part) = parts.last()
&& let Some(value) = current.get(last_part)
&& value.is_string()
{
return value.as_str().map(|s| s.to_string());
}
None
out.push_str(rest);
out
}
}
impl I18nService for FileSystemI18nService {
async fn translate(&self, key: &str, locale: Locale) -> I18nResult<String> {
// Check if translations are cached
// First attempt — the requested locale, cached or freshly loaded.
{
let cache = self.cache.read().await;
if let Some(translations) = cache.get(&locale) {
if let Some(value) = self.get_nested_value(translations, key) {
return Ok(value);
}
// Try to use English as fallback if we couldn't find the key
if locale != Locale::English
&& let Some(english_translations) = cache.get(&Locale::English)
&& let Some(value) = self.get_nested_value(english_translations, key)
{
return Ok(value);
}
return Err(I18nError::KeyNotFound(key.to_string()));
if let Some(translations) = cache.get(&locale)
&& let Some(value) = Self::lookup_nested(translations, key)
{
return Ok(value.to_string());
}
// Fall back to English while we still hold the read lock.
let english = Locale::english();
if locale != english
&& let Some(translations) = cache.get(&english)
&& let Some(value) = Self::lookup_nested(translations, key)
{
return Ok(value.to_string());
}
}
// If not cached, load translations and try again
self.load_translations(locale).await?;
// Cold-load the requested locale and try once more.
self.load_translations(locale.clone()).await?;
{
let cache = self.cache.read().await;
if let Some(translations) = cache.get(&locale) {
if let Some(value) = self.get_nested_value(translations, key) {
return Ok(value);
}
// Try to use English as fallback
if locale != Locale::English
&& let Some(english_translations) = cache.get(&Locale::English)
&& let Some(value) = self.get_nested_value(english_translations, key)
{
return Ok(value);
}
if let Some(translations) = cache.get(&locale)
&& let Some(value) = Self::lookup_nested(translations, key)
{
return Ok(value.to_string());
}
let english = Locale::english();
if locale != english
&& let Some(translations) = cache.get(&english)
&& let Some(value) = Self::lookup_nested(translations, key)
{
return Ok(value.to_string());
}
}
Err(I18nError::KeyNotFound(key.to_string()))
}
async fn translate_args(
&self,
key: &str,
locale: Locale,
args: &[(&str, &str)],
) -> I18nResult<String> {
let template = self.translate(key, locale).await?;
Ok(Self::interpolate(&template, args))
}
async fn load_translations(&self, locale: Locale) -> I18nResult<()> {
let file_path = self.get_locale_file_path(locale);
tracing::info!(
let file_path = self.locale_file_path(&locale);
tracing::debug!(
target: "oxicloud::i18n",
"Loading translations for locale {} from {:?}",
locale.as_str(),
file_path
);
// Check if file exists
if !file_path.exists() {
return Err(I18nError::InvalidLocale(locale.as_str().to_string()));
}
// Read and parse file
let content = fs::read_to_string(&file_path)
.await
.map_err(|e| I18nError::LoadError(format!("Failed to read translation file: {}", e)))?;
@@ -129,28 +183,61 @@ impl I18nService for FileSystemI18nService {
I18nError::LoadError(format!("Failed to parse translation file: {}", e))
})?;
// Update cache
{
let mut cache = self.cache.write().await;
cache.insert(locale, translations);
}
tracing::info!("Translations loaded for locale {}", locale.as_str());
Ok(())
}
async fn available_locales(&self) -> Vec<Locale> {
vec![
Locale::English,
Locale::Spanish,
Locale::French,
Locale::German,
Locale::Portuguese,
]
match &self.registry {
Some(reg) => reg.iter().collect(),
None => vec![Locale::english()],
}
}
async fn is_supported(&self, locale: Locale) -> bool {
let file_path = self.get_locale_file_path(locale);
file_path.exists()
match &self.registry {
Some(reg) => reg.parse(locale.as_str()).is_some(),
None => locale.is_english(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn interpolate_replaces_named_placeholders() {
let out = FileSystemI18nService::interpolate(
"Hello {{name}}, you have {{count}} new messages.",
&[("name", "Alice"), ("count", "3")],
);
assert_eq!(out, "Hello Alice, you have 3 new messages.");
}
#[test]
fn interpolate_tolerates_whitespace_inside_braces() {
let out = FileSystemI18nService::interpolate("hi {{ who }}", &[("who", "there")]);
assert_eq!(out, "hi there");
}
#[test]
fn interpolate_preserves_unmatched_placeholders() {
// Caller forgot to pass `name` — keep the literal in the output
// so QA can see what was missed.
let out = FileSystemI18nService::interpolate("Hello {{name}}", &[]);
assert_eq!(out, "Hello {{name}}");
}
#[test]
fn interpolate_is_single_pass() {
// A value containing `{{x}}` should NOT be re-expanded —
// otherwise an untrusted arg could trigger key lookup.
let out =
FileSystemI18nService::interpolate("{{greeting}}", &[("greeting", "Hello {{name}}")]);
assert_eq!(out, "Hello {{name}}");
}
}