ferro-lang 0.2.49

Localization for the Ferro web framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use std::collections::HashMap;

use crate::error::LangError;
use crate::interpolation::interpolate;
use crate::loader::{load_translations, normalize_locale};
use crate::pluralization::select_plural_form;

/// Core translation engine.
///
/// Loads JSON translation files from a directory structure, pre-merges
/// fallback translations, and provides lookup with interpolation and
/// pluralization.
pub struct Translator {
    translations: HashMap<String, HashMap<String, String>>,
    fallback: String,
}

impl Translator {
    /// Load translations from `{path}/{locale}/*.json` with the given fallback locale.
    ///
    /// The fallback locale's keys are pre-merged into every other locale so
    /// runtime lookup is a single `HashMap::get`.
    pub fn load(path: impl AsRef<str>, fallback: impl Into<String>) -> Result<Self, LangError> {
        let fallback = fallback.into();
        let translations = load_translations(path.as_ref(), &fallback)?;
        Ok(Self {
            translations,
            fallback: normalize_locale(&fallback),
        })
    }

    /// Look up a translation key with parameter interpolation.
    ///
    /// Returns the translated string with `:param` placeholders replaced.
    /// If the key is not found, returns the key itself (no panic, no Option).
    ///
    /// Resolution order: exact locale → base language (region stripped) →
    /// configured fallback. The base-language step lets a request for
    /// `it-IT` resolve against translations loaded under `lang/it/`.
    pub fn get(&self, locale: &str, key: &str, params: &[(&str, &str)]) -> String {
        let locale = normalize_locale(locale);
        let value = self
            .translations
            .get(&locale)
            .and_then(|m| m.get(key))
            .or_else(|| {
                base_language(&locale)
                    .and_then(|base| self.translations.get(base).and_then(|m| m.get(key)))
            })
            .or_else(|| {
                self.translations
                    .get(&self.fallback)
                    .and_then(|m| m.get(key))
            });

        match value {
            Some(template) => interpolate(template, params),
            None => {
                tracing::warn!(locale = %locale, key, "translation key not found");
                key.to_string()
            }
        }
    }

    /// Look up a pluralized translation key.
    ///
    /// Selects the correct plural form from pipe-separated values, then
    /// applies parameter interpolation. A `:count` parameter is automatically
    /// added with the string representation of `count`.
    ///
    /// Uses the same resolution order as [`Self::get`]: exact → base language →
    /// configured fallback.
    pub fn choice(&self, locale: &str, key: &str, count: i64, params: &[(&str, &str)]) -> String {
        let locale = normalize_locale(locale);
        let value = self
            .translations
            .get(&locale)
            .and_then(|m| m.get(key))
            .or_else(|| {
                base_language(&locale)
                    .and_then(|base| self.translations.get(base).and_then(|m| m.get(key)))
            })
            .or_else(|| {
                self.translations
                    .get(&self.fallback)
                    .and_then(|m| m.get(key))
            });

        match value {
            Some(template) => {
                let form = select_plural_form(template, count);
                let count_str = count.to_string();
                let mut all_params: Vec<(&str, &str)> = params.to_vec();
                all_params.push(("count", &count_str));
                interpolate(&form, &all_params)
            }
            None => {
                tracing::warn!(locale = %locale, key, "translation key not found");
                key.to_string()
            }
        }
    }

    /// Check if a translation key exists for the given locale.
    pub fn has(&self, locale: &str, key: &str) -> bool {
        let locale = normalize_locale(locale);
        self.translations
            .get(&locale)
            .is_some_and(|m| m.contains_key(key))
    }

    /// Return all available locale identifiers.
    pub fn locales(&self) -> Vec<&str> {
        self.translations.keys().map(|s| s.as_str()).collect()
    }
}

/// Strip the region subtag from a normalized locale.
///
/// Returns `Some("it")` for `"it-it"`, `Some("zh")` for `"zh-hans-cn"`,
/// and `None` for bare language tags like `"it"` that have no region.
fn base_language(locale: &str) -> Option<&str> {
    locale.split_once('-').map(|(base, _)| base)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::path::{Path, PathBuf};
    use std::sync::atomic::{AtomicU64, Ordering};

    static COUNTER: AtomicU64 = AtomicU64::new(0);

    /// Create a uniquely-named temp directory per test invocation.
    fn unique_dir(label: &str) -> PathBuf {
        let n = COUNTER.fetch_add(1, Ordering::Relaxed);
        let dir =
            std::env::temp_dir().join(format!("ferro_lang_{}_{}_{}", label, std::process::id(), n));
        let _ = fs::remove_dir_all(&dir);
        dir
    }

    /// Write the standard en + es fixture into the given directory.
    fn write_fixtures(dir: &Path) {
        let en_dir = dir.join("en");
        fs::create_dir_all(&en_dir).unwrap();
        fs::write(
            en_dir.join("messages.json"),
            serde_json::json!({
                "welcome": "Welcome, :name!",
                "items.count": "One item|:count items",
                "cart.summary": "{0} Your cart is empty|{1} :count item in your cart|[2,*] :count items in your cart",
                "only_en": "English only"
            })
            .to_string(),
        )
        .unwrap();

        let es_dir = dir.join("es");
        fs::create_dir_all(&es_dir).unwrap();
        fs::write(
            es_dir.join("messages.json"),
            serde_json::json!({
                "welcome": "Bienvenido, :name!",
                "items.count": "Un elemento|:count elementos"
            })
            .to_string(),
        )
        .unwrap();
    }

    fn cleanup(dir: &PathBuf) {
        let _ = fs::remove_dir_all(dir);
    }

    #[test]
    fn load_succeeds() {
        let dir = unique_dir("load");
        write_fixtures(&dir);
        let t = Translator::load(dir.to_str().unwrap(), "en").unwrap();
        assert!(t.locales().contains(&"en"));
        assert!(t.locales().contains(&"es"));
        cleanup(&dir);
    }

    #[test]
    fn get_with_interpolation() {
        let dir = unique_dir("get_interp");
        write_fixtures(&dir);
        let t = Translator::load(dir.to_str().unwrap(), "en").unwrap();
        assert_eq!(
            t.get("en", "welcome", &[("name", "Alice")]),
            "Welcome, Alice!"
        );
        cleanup(&dir);
    }

    #[test]
    fn get_returns_key_when_missing() {
        let dir = unique_dir("get_missing");
        write_fixtures(&dir);
        let t = Translator::load(dir.to_str().unwrap(), "en").unwrap();
        assert_eq!(t.get("en", "nonexistent.key", &[]), "nonexistent.key");
        cleanup(&dir);
    }

    #[test]
    fn choice_returns_plural_form() {
        let dir = unique_dir("choice_plural");
        write_fixtures(&dir);
        let t = Translator::load(dir.to_str().unwrap(), "en").unwrap();
        assert_eq!(t.choice("en", "items.count", 1, &[]), "One item");
        assert_eq!(t.choice("en", "items.count", 5, &[]), "5 items");
        cleanup(&dir);
    }

    #[test]
    fn choice_auto_adds_count_param() {
        let dir = unique_dir("choice_count");
        write_fixtures(&dir);
        let t = Translator::load(dir.to_str().unwrap(), "en").unwrap();
        assert_eq!(t.choice("en", "items.count", 42, &[]), "42 items");
        cleanup(&dir);
    }

    #[test]
    fn choice_explicit_ranges() {
        let dir = unique_dir("choice_ranges");
        write_fixtures(&dir);
        let t = Translator::load(dir.to_str().unwrap(), "en").unwrap();
        assert_eq!(t.choice("en", "cart.summary", 0, &[]), "Your cart is empty");
        assert_eq!(
            t.choice("en", "cart.summary", 1, &[]),
            "1 item in your cart"
        );
        assert_eq!(
            t.choice("en", "cart.summary", 3, &[]),
            "3 items in your cart"
        );
        cleanup(&dir);
    }

    #[test]
    fn has_returns_correct() {
        let dir = unique_dir("has");
        write_fixtures(&dir);
        let t = Translator::load(dir.to_str().unwrap(), "en").unwrap();
        assert!(t.has("en", "welcome"));
        assert!(!t.has("en", "nonexistent"));
        cleanup(&dir);
    }

    #[test]
    fn locales_returns_all() {
        let dir = unique_dir("locales");
        write_fixtures(&dir);
        let t = Translator::load(dir.to_str().unwrap(), "en").unwrap();
        let mut locales = t.locales();
        locales.sort();
        assert_eq!(locales, vec!["en", "es"]);
        cleanup(&dir);
    }

    #[test]
    fn fallback_locale_works() {
        let dir = unique_dir("fallback");
        write_fixtures(&dir);
        let t = Translator::load(dir.to_str().unwrap(), "en").unwrap();
        // "only_en" exists in en but not es — should be pre-merged into es
        assert_eq!(t.get("es", "only_en", &[]), "English only");
        cleanup(&dir);
    }

    #[test]
    fn locale_normalization() {
        let dir = unique_dir("normalization");
        write_fixtures(&dir);

        let en_us_dir = dir.join("en_US");
        fs::create_dir_all(&en_us_dir).unwrap();
        fs::write(
            en_us_dir.join("messages.json"),
            serde_json::json!({
                "greeting": "Hey!"
            })
            .to_string(),
        )
        .unwrap();

        let t = Translator::load(dir.to_str().unwrap(), "en").unwrap();
        assert!(t.has("en-us", "greeting"));
        assert!(t.has("en_US", "greeting"));
        assert!(t.has("EN_US", "greeting"));
        cleanup(&dir);
    }

    #[test]
    fn nested_json_flattened() {
        let dir = unique_dir("nested");
        write_fixtures(&dir);

        let fr_dir = dir.join("fr");
        fs::create_dir_all(&fr_dir).unwrap();
        fs::write(
            fr_dir.join("auth.json"),
            serde_json::json!({
                "auth": {
                    "login": "Connexion",
                    "register": "Inscription"
                }
            })
            .to_string(),
        )
        .unwrap();

        let t = Translator::load(dir.to_str().unwrap(), "en").unwrap();
        assert_eq!(t.get("fr", "auth.login", &[]), "Connexion");
        assert_eq!(t.get("fr", "auth.register", &[]), "Inscription");
        cleanup(&dir);
    }

    #[test]
    fn empty_dir_errors() {
        let dir = unique_dir("empty");
        fs::create_dir_all(&dir).unwrap();
        let result = Translator::load(dir.to_str().unwrap(), "en");
        assert!(result.is_err());
        cleanup(&dir);
    }

    // ── regional locale → base language fallback ──────────────────────
    //
    // Mobile browsers virtually always send region-tagged Accept-Language
    // (e.g. `it-IT`, `en-US`), but translation files are typically loaded
    // under base-language directories (`lang/it/`, `lang/en/`). The
    // translator must try the base language before falling through to
    // the configured fallback locale, otherwise every mobile user sees
    // the fallback locale's strings regardless of device language.

    #[test]
    fn get_regional_locale_falls_back_to_base_language() {
        let dir = unique_dir("regional_base");
        write_fixtures(&dir);
        let t = Translator::load(dir.to_str().unwrap(), "en").unwrap();
        // Request "es-MX" — no es-MX dir exists, but "es" does.
        // Must return Spanish, not English (the configured fallback).
        assert_eq!(
            t.get("es-MX", "welcome", &[("name", "Ana")]),
            "Bienvenido, Ana!"
        );
        cleanup(&dir);
    }

    #[test]
    fn get_regional_locale_prefers_exact_match_over_base() {
        let dir = unique_dir("regional_exact");
        write_fixtures(&dir);
        // Add an exact es-MX dir with a different greeting
        let es_mx = dir.join("es-mx");
        fs::create_dir_all(&es_mx).unwrap();
        fs::write(
            es_mx.join("messages.json"),
            serde_json::json!({"welcome": "¡Qué onda, :name!"}).to_string(),
        )
        .unwrap();
        let t = Translator::load(dir.to_str().unwrap(), "en").unwrap();
        // Exact es-mx match wins over base "es"
        assert_eq!(
            t.get("es-MX", "welcome", &[("name", "Ana")]),
            "¡Qué onda, Ana!"
        );
        cleanup(&dir);
    }

    #[test]
    fn get_regional_locale_uses_global_fallback_when_no_base() {
        let dir = unique_dir("regional_no_base");
        write_fixtures(&dir);
        let t = Translator::load(dir.to_str().unwrap(), "en").unwrap();
        // Request "fr-CA" — no fr-CA, no fr loaded; falls through to "en".
        assert_eq!(
            t.get("fr-CA", "welcome", &[("name", "Léa")]),
            "Welcome, Léa!"
        );
        cleanup(&dir);
    }

    #[test]
    fn choice_regional_locale_falls_back_to_base_language() {
        let dir = unique_dir("regional_choice");
        write_fixtures(&dir);
        let t = Translator::load(dir.to_str().unwrap(), "en").unwrap();
        // es has its own plural form; es-MX request should reach it
        // via base-language fallback, not slip through to English.
        assert_eq!(t.choice("es-MX", "items.count", 5, &[]), "5 elementos");
        cleanup(&dir);
    }

    #[test]
    fn get_underscore_regional_input_falls_back_to_base() {
        let dir = unique_dir("regional_underscore");
        write_fixtures(&dir);
        let t = Translator::load(dir.to_str().unwrap(), "en").unwrap();
        // Accept-Language hyphens are the common form, but the API also
        // accepts underscore form. Both must normalize and fall back.
        assert_eq!(
            t.get("es_MX", "welcome", &[("name", "Ana")]),
            "Bienvenido, Ana!"
        );
        cleanup(&dir);
    }

    #[test]
    fn base_language_helper() {
        assert_eq!(super::base_language("it-it"), Some("it"));
        assert_eq!(super::base_language("zh-hans-cn"), Some("zh"));
        assert_eq!(super::base_language("it"), None);
        assert_eq!(super::base_language(""), None);
    }
}