pacsea/i18n/
translations.rs1use std::collections::HashMap;
4
5pub type TranslationMap = HashMap<String, String>;
7
8#[must_use]
21pub fn translate(key: &str, translations: &TranslationMap) -> Option<String> {
22 translations.get(key).cloned()
23}
24
25pub fn translate_with_fallback(
41 key: &str,
42 translations: &TranslationMap,
43 fallback_translations: &TranslationMap,
44) -> String {
45 if let Some(translation) = translations.get(key) {
47 return translation.clone();
48 }
49
50 if let Some(translation) = fallback_translations.get(key) {
52 tracing::debug!(
54 "Translation key '{}' not found in primary locale, using fallback",
55 key
56 );
57 return translation.clone();
58 }
59
60 tracing::debug!(
63 "Missing translation key: '{}'. Returning key as-is. Please add this key to locale files.",
64 key
65 );
66 key.to_string()
67}
68
69#[cfg(test)]
70mod tests {
71 use super::*;
72
73 #[test]
74 fn test_translate() {
75 let mut translations = HashMap::new();
76 translations.insert("app.titles.search".to_string(), "Suche".to_string());
77
78 assert_eq!(
79 translate("app.titles.search", &translations),
80 Some("Suche".to_string())
81 );
82 assert_eq!(translate("app.titles.help", &translations), None);
83 }
84
85 #[test]
86 fn test_translate_with_fallback() {
87 let mut primary = HashMap::new();
88 primary.insert("app.titles.search".to_string(), "Suche".to_string());
89
90 let mut fallback = HashMap::new();
91 fallback.insert("app.titles.search".to_string(), "Search".to_string());
92 fallback.insert("app.titles.help".to_string(), "Help".to_string());
93
94 assert_eq!(
95 translate_with_fallback("app.titles.search", &primary, &fallback),
96 "Suche"
97 );
98 assert_eq!(
99 translate_with_fallback("app.titles.help", &primary, &fallback),
100 "Help"
101 );
102 assert_eq!(
103 translate_with_fallback("app.titles.missing", &primary, &fallback),
104 "app.titles.missing"
105 );
106 }
107}