cache i18n categories

This commit is contained in:
Damien Elmes 2020-02-16 20:07:52 +10:00
parent 9247e5de7d
commit 4fe47b7be4

View File

@ -25,6 +25,7 @@ macro_rules! tr_strs {
} }
}; };
} }
use std::collections::HashMap;
pub use tr_strs; pub use tr_strs;
/// The folder containing ftl files for the provided language. /// The folder containing ftl files for the provided language.
@ -124,13 +125,13 @@ impl I18n {
inner: Arc::new(Mutex::new(I18nInner { inner: Arc::new(Mutex::new(I18nInner {
langs, langs,
available_ftl_folders: supported, available_ftl_folders: supported,
cache: Default::default(),
})), })),
} }
} }
pub fn get(&self, group: StringsGroup) -> I18nCategory { pub fn get(&self, group: StringsGroup) -> Arc<I18nCategory> {
let inner = self.inner.lock().unwrap(); self.inner.lock().unwrap().get(group)
I18nCategory::new(&*inner.langs, &*inner.available_ftl_folders, group)
} }
} }
@ -139,6 +140,19 @@ struct I18nInner {
langs: Vec<LanguageIdentifier>, langs: Vec<LanguageIdentifier>,
// the available ftl folder subset of the user's preferred languages // the available ftl folder subset of the user's preferred languages
available_ftl_folders: Vec<PathBuf>, available_ftl_folders: Vec<PathBuf>,
cache: HashMap<StringsGroup, Arc<I18nCategory>>,
}
impl I18nInner {
pub fn get(&mut self, group: StringsGroup) -> Arc<I18nCategory> {
let langs = &self.langs;
let avail = &self.available_ftl_folders;
self.cache
.entry(group)
.or_insert_with(|| Arc::new(I18nCategory::new(langs, avail, group)))
.clone()
}
} }
pub struct I18nCategory { pub struct I18nCategory {