Caching translations
This commit is contained in:
parent
f4dca93598
commit
414609b6e0
3 changed files with 46 additions and 10 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1 +1,2 @@
|
|||
/target
|
||||
perf.data
|
||||
|
|
|
|||
|
|
@ -18,3 +18,6 @@ ratatui = "0.30.0"
|
|||
crossterm = "0.29.0"
|
||||
chrono = "0.4.42"
|
||||
rayon = "1.11.0"
|
||||
|
||||
[profile.release]
|
||||
debug = true
|
||||
|
|
|
|||
|
|
@ -1,17 +1,49 @@
|
|||
use crate::i18n::loader::LOCALES;
|
||||
use fluent_templates::{Loader, fluent_bundle::FluentValue};
|
||||
use once_cell::sync::Lazy;
|
||||
use std::borrow::Cow;
|
||||
use std::{collections::HashMap, str::FromStr};
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
collections::HashMap,
|
||||
str::FromStr,
|
||||
sync::{OnceLock, RwLock},
|
||||
};
|
||||
use unic_langid::LanguageIdentifier;
|
||||
|
||||
static SYSTEM_LOCALE: Lazy<LanguageIdentifier> = Lazy::new(|| {
|
||||
let sys_lang = sys_locale::get_locale().unwrap_or_else(|| String::from("pt-BR"));
|
||||
LanguageIdentifier::from_str(&sys_lang).expect("Invalid language")
|
||||
});
|
||||
static SYSTEM_LOCALE: OnceLock<LanguageIdentifier> = OnceLock::new();
|
||||
static TRANSLATION_CACHE: OnceLock<RwLock<HashMap<String, String>>> = OnceLock::new();
|
||||
const CACHE_INITIAL_SIZE: usize = 100;
|
||||
const CACHE_SIZE_LIMIT: usize = 1000;
|
||||
|
||||
fn get_system_locale() -> &'static LanguageIdentifier {
|
||||
SYSTEM_LOCALE.get_or_init(|| {
|
||||
let sys_lang = sys_locale::get_locale().unwrap_or_else(|| String::from("pt-BR"));
|
||||
LanguageIdentifier::from_str(&sys_lang).expect("Invalid language")
|
||||
})
|
||||
}
|
||||
|
||||
fn get_translation_cache() -> &'static RwLock<HashMap<String, String>> {
|
||||
TRANSLATION_CACHE.get_or_init(|| RwLock::new(HashMap::with_capacity(CACHE_INITIAL_SIZE)))
|
||||
}
|
||||
|
||||
pub fn t(key: &str) -> String {
|
||||
LOCALES.lookup(&*SYSTEM_LOCALE, key)
|
||||
let cache = get_translation_cache();
|
||||
|
||||
{
|
||||
let cache_read = cache.read().unwrap();
|
||||
if let Some(cached) = cache_read.get(key) {
|
||||
return cached.clone();
|
||||
}
|
||||
}
|
||||
|
||||
let result = LOCALES.lookup(&*get_system_locale(), key);
|
||||
|
||||
let mut cache_write = cache.write().unwrap();
|
||||
|
||||
if cache_write.len() >= CACHE_SIZE_LIMIT {
|
||||
cache_write.clear();
|
||||
}
|
||||
|
||||
cache_write.insert(key.to_string(), result.clone());
|
||||
result
|
||||
}
|
||||
|
||||
pub fn t_with_args(key: &str, args: &HashMap<&'static str, String>) -> String {
|
||||
|
|
@ -20,8 +52,8 @@ pub fn t_with_args(key: &str, args: &HashMap<&'static str, String>) -> String {
|
|||
args_map = {
|
||||
for (key, value) in args {
|
||||
map.insert(Cow::Borrowed(*key), FluentValue::from(value.clone()));
|
||||
};
|
||||
}
|
||||
&map
|
||||
};
|
||||
LOCALES.lookup_with_args(&*&SYSTEM_LOCALE, key, args_map)
|
||||
LOCALES.lookup_with_args(&get_system_locale(), key, args_map)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue