larastvel-core 0.1.0

Larastvel framework core - inspired by Laravel
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
use std::collections::HashMap;
use std::fmt;
use std::path::Path;
use std::sync::{Mutex, OnceLock};

use serde_json::Value;

#[derive(Debug, Clone)]
pub struct TranslationError(pub String);

impl fmt::Display for TranslationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl std::error::Error for TranslationError {}

impl From<String> for TranslationError {
    fn from(s: String) -> Self {
        TranslationError(s)
    }
}

static GLOBAL_TRANSLATOR: OnceLock<Mutex<Translator>> = OnceLock::new();

fn global_translator() -> &'static Mutex<Translator> {
    GLOBAL_TRANSLATOR.get_or_init(|| Mutex::new(Translator::new("en", "en")))
}

pub struct TranslationConfig {
    pub locale: String,
    pub fallback_locale: String,
    pub lang_path: Option<String>,
}

impl Default for TranslationConfig {
    fn default() -> Self {
        Self {
            locale: "en".to_string(),
            fallback_locale: "en".to_string(),
            lang_path: None,
        }
    }
}

#[derive(Debug, Clone)]
pub struct Translator {
    locale: String,
    fallback_locale: String,
    translations: HashMap<String, HashMap<String, Value>>,
    fallback_translations: HashMap<String, Value>,
    lang_path: Option<String>,
}

impl Translator {
    pub fn new(locale: &str, fallback_locale: &str) -> Self {
        Self {
            locale: locale.to_string(),
            fallback_locale: fallback_locale.to_string(),
            translations: HashMap::new(),
            fallback_translations: HashMap::new(),
            lang_path: None,
        }
    }

    pub fn with_lang_path(mut self, path: &str) -> Self {
        self.lang_path = Some(path.to_string());
        self
    }

    pub fn set_locale(&mut self, locale: &str) {
        self.locale = locale.to_string();
    }

    pub fn locale(&self) -> &str {
        &self.locale
    }

    pub fn set_fallback_locale(&mut self, locale: &str) {
        self.fallback_locale = locale.to_string();
    }

    pub fn fallback_locale(&self) -> &str {
        &self.fallback_locale
    }

    pub fn load_json(&mut self, locale: &str, json: &str) {
        let parsed: Value = serde_json::from_str(json).unwrap_or(Value::Null);
        if let Value::Object(map) = parsed {
            let flat = flatten_json(&map, "");
            if locale == self.fallback_locale {
                self.fallback_translations = flat;
            } else {
                self.translations.insert(locale.to_string(), flat.clone());
                if locale == self.locale {
                    self.fallback_translations = flat;
                }
            }
        }
    }

    pub fn load_file(&mut self, locale: &str, path: &str) -> Result<(), TranslationError> {
        let content = std::fs::read_to_string(path).map_err(|e| e.to_string())?;
        self.load_json(locale, &content);
        Ok(())
    }

    pub fn load_directory(&mut self, dir: &str) -> Result<(), TranslationError> {
        let dir_path = Path::new(dir);
        if !dir_path.exists() {
            return Ok(());
        }
        for entry in std::fs::read_dir(dir_path).map_err(|e| e.to_string())? {
            let entry = entry.map_err(|e| e.to_string())?;
            let path = entry.path();
            if path.extension().and_then(|s| s.to_str()) == Some("json") {
                if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) {
                    self.load_file(stem, path.to_str().unwrap())?;
                }
            }
        }
        Ok(())
    }

    fn resolve(&self, key: &str, locale: &str) -> Option<&str> {
        if locale == self.fallback_locale {
            return self.fallback_translations.get(key).and_then(|v| v.as_str());
        }
        if let Some(trans) = self.translations.get(locale) {
            if let Some(val) = trans.get(key).and_then(|v| v.as_str()) {
                return Some(val);
            }
        }
        if locale != self.fallback_locale {
            return self.fallback_translations.get(key).and_then(|v| v.as_str());
        }
        None
    }

    fn replace_params(template: &str, params: &HashMap<String, String>) -> String {
        let mut result = template.to_string();
        for (key, value) in params {
            result = result.replace(&format!(":{}", key), value);
        }
        result
    }

    pub fn get(&self, key: &str, replace: Option<HashMap<String, String>>) -> String {
        let template = self
            .resolve(key, &self.locale)
            .or_else(|| self.resolve(key, &self.fallback_locale))
            .unwrap_or(key);

        if let Some(params) = replace {
            Self::replace_params(template, &params)
        } else {
            template.to_string()
        }
    }

    pub fn choice(
        &self,
        key: &str,
        count: i64,
        replace: Option<HashMap<String, String>>,
    ) -> String {
        let template = self
            .resolve(key, &self.locale)
            .or_else(|| self.resolve(key, &self.fallback_locale))
            .unwrap_or(key);

        let translated = select_plural(template, count);

        if let Some(params) = replace {
            Self::replace_params(&translated, &params)
        } else {
            translated
        }
    }

    pub fn has(&self, key: &str) -> bool {
        self.resolve(key, &self.locale).is_some()
            || self.resolve(key, &self.fallback_locale).is_some()
    }

    pub fn has_for_locale(&self, key: &str, locale: &str) -> bool {
        if locale == self.fallback_locale {
            return self
                .fallback_translations
                .get(key)
                .and_then(|v| v.as_str())
                .is_some();
        }
        if let Some(trans) = self.translations.get(locale) {
            if trans.get(key).and_then(|v| v.as_str()).is_some() {
                return true;
            }
        }
        false
    }
}

pub fn set_locale(locale: &str) {
    let mut t = global_translator().lock().unwrap();
    t.set_locale(locale);
}

pub fn locale() -> String {
    let t = global_translator().lock().unwrap();
    t.locale().to_string()
}

pub fn set_fallback_locale(locale: &str) {
    let mut t = global_translator().lock().unwrap();
    t.set_fallback_locale(locale);
}

pub fn load_translation_json(locale: &str, json: &str) {
    let mut t = global_translator().lock().unwrap();
    t.load_json(locale, json);
}

pub fn load_translation_file(locale: &str, path: &str) -> Result<(), TranslationError> {
    let mut t = global_translator().lock().unwrap();
    t.load_file(locale, path)
}

pub fn load_translation_directory(dir: &str) -> Result<(), TranslationError> {
    let mut t = global_translator().lock().unwrap();
    t.load_directory(dir)
}

pub fn __(key: &str) -> String {
    let t = global_translator().lock().unwrap();
    t.get(key, None)
}

pub fn __with(key: &str, replace: HashMap<String, String>) -> String {
    let t = global_translator().lock().unwrap();
    t.get(key, Some(replace))
}

pub fn trans_choice(key: &str, count: i64) -> String {
    let t = global_translator().lock().unwrap();
    t.choice(key, count, None)
}

pub fn trans_choice_with(key: &str, count: i64, replace: HashMap<String, String>) -> String {
    let t = global_translator().lock().unwrap();
    t.choice(key, count, Some(replace))
}

pub fn has_translation(key: &str) -> bool {
    let t = global_translator().lock().unwrap();
    t.has(key)
}

fn flatten_json(map: &serde_json::Map<String, Value>, prefix: &str) -> HashMap<String, Value> {
    let mut result = HashMap::new();
    for (key, value) in map {
        let full_key = if prefix.is_empty() {
            key.clone()
        } else {
            format!("{}.{}", prefix, key)
        };
        match value {
            Value::Object(inner) => {
                result.extend(flatten_json(inner, &full_key));
            }
            _ => {
                result.insert(full_key, value.clone());
            }
        }
    }
    result
}

fn select_plural(template: &str, count: i64) -> String {
    let segments: Vec<&str> = template.split('|').collect();
    if segments.len() <= 1 {
        return template.replace(":count", &count.to_string());
    }

    let mut has_bracket = false;
    for segment in &segments {
        let segment = segment.trim();

        let extract = segment.split_once(']').or_else(|| segment.split_once('}'));

        if let Some((range_part, message)) = extract {
            has_bracket = true;
            let open = range_part.find(['[', '{']);
            let range = match open {
                Some(idx) => &range_part[idx + 1..],
                None => continue,
            };
            let message = message.trim();

            if let Some((start, end)) = range.split_once(',') {
                let start_val: i64 = start.trim().parse().unwrap_or(0);
                let end_str = end.trim();
                if end_str == "*" {
                    if count >= start_val {
                        return message.replace(":count", &count.to_string());
                    }
                } else {
                    let end_val: i64 = end_str.parse().unwrap_or(i64::MAX);
                    if count >= start_val && count <= end_val {
                        return message.replace(":count", &count.to_string());
                    }
                }
            } else {
                let single: i64 = range.trim().parse().unwrap_or(0);
                if count == single {
                    return message.replace(":count", &count.to_string());
                }
            }
        }
    }

    if !has_bracket {
        if count == 1 {
            return segments[0].trim().replace(":count", &count.to_string());
        }
        if segments.len() >= 2 {
            return segments[1].trim().replace(":count", &count.to_string());
        }
    }

    segments
        .last()
        .unwrap_or(&template)
        .trim()
        .replace(":count", &count.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn setup_translator() -> Translator {
        let mut t = Translator::new("en", "en");
        t.load_json(
            "en",
            r#"{
            "hello": "Hello World",
            "greeting": "Hello :name",
            "auth": {
                "failed": "These credentials do not match our records.",
                "throttle": "Too many attempts. Please try again in :seconds seconds."
            },
            "messages": {
                "welcome": "Welcome to :app"
            },
            "apples": "{0} No apples|{1} One apple|[2,*] :count apples"
        }"#,
        );
        t.load_json(
            "id",
            r#"{
            "hello": "Halo Dunia",
            "greeting": "Halo :name",
            "auth": {
                "failed": "Kredensial ini tidak cocok dengan catatan kami."
            }
        }"#,
        );
        t
    }

    #[test]
    fn test_simple_key() {
        let t = setup_translator();
        assert_eq!(t.get("hello", None), "Hello World");
    }

    #[test]
    fn test_key_with_replacement() {
        let t = setup_translator();
        let params = HashMap::from([("name".to_string(), "Taylor".to_string())]);
        assert_eq!(t.get("greeting", Some(params)), "Hello Taylor");
    }

    #[test]
    fn test_dot_notation() {
        let t = setup_translator();
        assert_eq!(
            t.get("auth.failed", None),
            "These credentials do not match our records."
        );
    }

    #[test]
    fn test_dot_notation_with_replacement() {
        let t = setup_translator();
        let params = HashMap::from([("seconds".to_string(), "60".to_string())]);
        assert_eq!(
            t.get("auth.throttle", Some(params)),
            "Too many attempts. Please try again in 60 seconds."
        );
    }

    #[test]
    fn test_nested_dot_notation() {
        let t = setup_translator();
        let params = HashMap::from([("app".to_string(), "Larastvel".to_string())]);
        assert_eq!(
            t.get("messages.welcome", Some(params)),
            "Welcome to Larastvel"
        );
    }

    #[test]
    fn test_missing_key_returns_key() {
        let t = setup_translator();
        assert_eq!(t.get("nonexistent.key", None), "nonexistent.key");
    }

    #[test]
    fn test_locale_switching() {
        let mut t = setup_translator();
        assert_eq!(t.get("hello", None), "Hello World");
        t.set_locale("id");
        assert_eq!(t.get("hello", None), "Halo Dunia");
    }

    #[test]
    fn test_fallback_to_english() {
        let mut t = setup_translator();
        t.set_locale("id");
        assert_eq!(
            t.get("auth.throttle", None),
            "Too many attempts. Please try again in :seconds seconds."
        );
    }

    #[test]
    fn test_locale_missing_entirely() {
        let mut t = setup_translator();
        t.set_locale("fr");
        assert_eq!(t.get("hello", None), "Hello World");
    }

    #[test]
    fn test_plural_zero() {
        let t = setup_translator();
        assert_eq!(t.choice("apples", 0, None), "No apples");
    }

    #[test]
    fn test_plural_one() {
        let t = setup_translator();
        assert_eq!(t.choice("apples", 1, None), "One apple");
    }

    #[test]
    fn test_plural_many() {
        let t = setup_translator();
        assert_eq!(t.choice("apples", 5, None), "5 apples");
    }

    #[test]
    fn test_plural_with_replacement() {
        let t = setup_translator();
        assert_eq!(t.choice("apples", 10, None), "10 apples");
    }

    #[test]
    fn test_has_translation() {
        let t = setup_translator();
        assert!(t.has("hello"));
        assert!(t.has("auth.failed"));
        assert!(!t.has("nothing.here"));
    }

    #[test]
    fn test_has_for_locale() {
        let t = setup_translator();
        assert!(t.has_for_locale("hello", "en"));
        assert!(t.has_for_locale("hello", "id"));
        assert!(!t.has_for_locale("hello", "fr"));
    }

    #[test]
    fn test_global_functions() {
        load_translation_json("en", r#"{"hello": "Hello"}"#);
        set_locale("en");
        assert_eq!(__("hello"), "Hello");
    }

    #[test]
    fn test_global_locale() {
        set_locale("en");
        assert_eq!(locale(), "en");
        set_locale("id");
        assert_eq!(locale(), "id");
        set_locale("en");
    }

    #[test]
    fn test_global_fallback() {
        set_fallback_locale("en");
        assert_eq!(
            super::super::translation::Translator::new("en", "en").fallback_locale(),
            "en"
        );
    }

    #[test]
    fn test_flatten_json() {
        let mut map = serde_json::Map::new();
        map.insert(
            "auth".to_string(),
            Value::Object({
                let mut m = serde_json::Map::new();
                m.insert("failed".to_string(), Value::String("Failed".to_string()));
                m
            }),
        );
        let flat = flatten_json(&map, "");
        assert_eq!(
            flat.get("auth.failed").and_then(|v| v.as_str()),
            Some("Failed")
        );
    }

    #[test]
    fn test_choice_missing_key() {
        let t = setup_translator();
        assert_eq!(t.choice("nonexistent", 1, None), "nonexistent");
    }

    #[test]
    fn test_choice_simple_plural() {
        let mut t = Translator::new("en", "en");
        t.load_json("en", r#"{"items": "item|items"}"#);
        assert_eq!(t.choice("items", 1, None), "item");
    }

    #[test]
    fn test_translator_config_default() {
        let config = TranslationConfig::default();
        assert_eq!(config.locale, "en");
        assert_eq!(config.fallback_locale, "en");
    }

    #[test]
    fn test_has_translation_global() {
        load_translation_json("en", r#"{"exists": "yes"}"#);
        set_locale("en");
        assert!(has_translation("exists"));
        assert!(!has_translation("nope"));
    }

    #[test]
    fn test_load_directory_nonexistent() {
        let mut t = Translator::new("en", "en");
        assert!(t.load_directory("/tmp/nonexistent_lang_dir_12345").is_ok());
    }

    #[test]
    fn test_load_file_nonexistent() {
        let mut t = Translator::new("en", "en");
        assert!(t.load_file("en", "/tmp/nonexistent.json").is_err());
    }
}