Skip to main content

garbage_code_hunter/common/
i18n_ext.rs

1//! Extended i18n helpers for entertainment tools.
2
3/// Check if the language is Chinese.
4pub fn is_chinese(lang: &str) -> bool {
5    matches!(
6        lang.to_lowercase().replace('_', "-").as_str(),
7        "zh" | "zh-cn" | "chinese"
8    )
9}
10
11/// Select a string based on language.
12pub fn t<'a>(lang: &'a str, zh: &'a str, en: &'a str) -> &'a str {
13    if is_chinese(lang) {
14        zh
15    } else {
16        en
17    }
18}
19
20/// Select an owned String based on language.
21pub fn t_owned(lang: &str, zh: &str, en: &str) -> String {
22    if is_chinese(lang) {
23        zh.to_string()
24    } else {
25        en.to_string()
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    // ── is_chinese ────────────────────────────────────────────────
34
35    /// Objective: Verify all standard Chinese locale variants are recognized.
36    /// Invariants: Case insensitive; underscores normalized to hyphens.
37    #[test]
38    fn test_is_chinese_standard_variants() {
39        assert!(is_chinese("zh-CN"), "zh-CN should be Chinese");
40        assert!(is_chinese("zh"), "zh should be Chinese");
41        assert!(is_chinese("chinese"), "chinese string should be Chinese");
42        assert!(
43            is_chinese("zh_CN"),
44            "zh_CN (underscore) should be normalized to zh-CN"
45        );
46    }
47
48    /// Objective: Verify non-Chinese locales are correctly rejected.
49    /// Invariants: en, ja, fr, de, etc. are not Chinese.
50    #[test]
51    fn test_is_chinese_non_chinese() {
52        assert!(!is_chinese("en-US"), "en-US is not Chinese");
53        assert!(!is_chinese("en"), "en is not Chinese");
54        assert!(!is_chinese("ja"), "ja is not Chinese");
55        assert!(!is_chinese("fr"), "fr is not Chinese");
56        assert!(!is_chinese("de"), "de is not Chinese");
57    }
58
59    /// Objective: Verify case insensitivity in is_chinese.
60    #[test]
61    fn test_is_chinese_case_insensitive() {
62        assert!(is_chinese("ZH"), "ZH uppercase should match");
63        assert!(is_chinese("Zh-cn"), "Zh-cn mixed case should match");
64        assert!(is_chinese("Chinese"), "Chinese capitalized should match");
65    }
66
67    /// Objective: Verify empty string is not considered Chinese.
68    #[test]
69    fn test_is_chinese_empty() {
70        assert!(!is_chinese(""), "empty string is not Chinese");
71    }
72
73    // ── t ─────────────────────────────────────────────────────────
74
75    /// Objective: Verify t selects zh text for Chinese lang, en text otherwise.
76    /// Invariants: Returns reference to one of the two input strings (no allocation).
77    #[test]
78    fn test_t_basic() {
79        assert_eq!(
80            t("zh-CN", "中文", "English"),
81            "中文",
82            "zh-CN => Chinese text"
83        );
84        assert_eq!(
85            t("en-US", "中文", "English"),
86            "English",
87            "en-US => English text"
88        );
89    }
90
91    /// Objective: Verify t falls back to English for unknown locales.
92    #[test]
93    fn test_t_fallback_for_unknown() {
94        assert_eq!(
95            t("klingon", "中文", "English"),
96            "English",
97            "unknown locale => English"
98        );
99        assert_eq!(
100            t("", "中文", "English"),
101            "English",
102            "empty locale => English"
103        );
104    }
105
106    // ── t_owned ───────────────────────────────────────────────────
107
108    /// Objective: Verify t_owned returns owned String with correct value.
109    #[test]
110    fn test_t_owned_basic() {
111        assert_eq!(
112            t_owned("zh", "中文", "English"),
113            "中文",
114            "zh => Chinese String"
115        );
116        assert_eq!(
117            t_owned("en", "中文", "English"),
118            "English",
119            "en => English String"
120        );
121    }
122
123    /// Objective: Verify t_owned handles identical zh/en strings without duplication.
124    #[test]
125    fn test_t_owned_identical_strings() {
126        let result = t_owned("en", "same", "same");
127        assert_eq!(
128            result, "same",
129            "identical zh/en still returns correct value"
130        );
131    }
132}