Fixing translations
This commit is contained in:
parent
dbe196f388
commit
0dc51af51c
9 changed files with 41 additions and 136 deletions
|
|
@ -1,13 +1,27 @@
|
|||
use crate::i18n::loader::LOCALES;
|
||||
use fluent_templates::Loader;
|
||||
use std::str::FromStr;
|
||||
use fluent_templates::{Loader, fluent_bundle::FluentValue};
|
||||
use once_cell::sync::Lazy;
|
||||
use std::borrow::Cow;
|
||||
use std::{collections::HashMap, str::FromStr};
|
||||
use unic_langid::LanguageIdentifier;
|
||||
|
||||
fn get_sys_locale() -> 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")
|
||||
}
|
||||
});
|
||||
|
||||
pub fn t(key: &str) -> String {
|
||||
LOCALES.lookup(&get_sys_locale(), key)
|
||||
LOCALES.lookup(&*SYSTEM_LOCALE, key)
|
||||
}
|
||||
|
||||
pub fn t_with_args(key: &str, args: &HashMap<&'static str, String>) -> String {
|
||||
let mut map = HashMap::new();
|
||||
let args_map: &HashMap<Cow<'static, _>, FluentValue<'_>>;
|
||||
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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue