use super::*;
impl I18nCatalog {
#[must_use]
pub fn new(default_locale: String) -> Self {
Self {
locale: Signal::new(default_locale.clone()),
fallback_locale: default_locale,
tables: HashMap::new(),
}
}
pub fn insert(&mut self, locale: String, key: String, value: String) {
self.get_mut_tables()
.entry(locale)
.or_default()
.insert(key, value);
}
#[must_use]
pub fn t(&self, key: &str) -> String {
let active: String = self.get_locale().get();
if let Some(found) = self
.get_tables()
.get(&active)
.and_then(|map: &HashMap<String, String>| map.get(key))
{
return found.clone();
}
if let Some(found) = self
.get_tables()
.get(self.get_fallback_locale())
.and_then(|map: &HashMap<String, String>| map.get(key))
{
return found.clone();
}
key.to_string()
}
}