diff --git a/rslib/src/i18n/mod.rs b/rslib/src/i18n/mod.rs index 9e7a43caa..2156822c6 100644 --- a/rslib/src/i18n/mod.rs +++ b/rslib/src/i18n/mod.rs @@ -25,6 +25,7 @@ macro_rules! tr_strs { } }; } +use std::collections::HashMap; pub use tr_strs; /// The folder containing ftl files for the provided language. @@ -124,13 +125,13 @@ impl I18n { inner: Arc::new(Mutex::new(I18nInner { langs, available_ftl_folders: supported, + cache: Default::default(), })), } } - pub fn get(&self, group: StringsGroup) -> I18nCategory { - let inner = self.inner.lock().unwrap(); - I18nCategory::new(&*inner.langs, &*inner.available_ftl_folders, group) + pub fn get(&self, group: StringsGroup) -> Arc { + self.inner.lock().unwrap().get(group) } } @@ -139,6 +140,19 @@ struct I18nInner { langs: Vec, // the available ftl folder subset of the user's preferred languages available_ftl_folders: Vec, + cache: HashMap>, +} + +impl I18nInner { + pub fn get(&mut self, group: StringsGroup) -> Arc { + 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 {