🧹 move default locale logic from main file
This logic needs to be done in localization.rs. Also expands usage of environment variables: `LC_ALL`, `LC_MESSAGES`, `LANG`.
This commit is contained in:
parent
c1c663f248
commit
52351e998a
|
@ -26,22 +26,22 @@ macro_rules! fl {
|
|||
}};
|
||||
}
|
||||
|
||||
// Get the `Localizer` to be used for localizing this library.
|
||||
/// Get the `Localizer` to be used for localizing this library.
|
||||
pub fn localizer() -> Box<dyn Localizer> {
|
||||
Box::from(DefaultLocalizer::new(&*LANGUAGE_LOADER, &Localizations))
|
||||
}
|
||||
|
||||
// Get translated text
|
||||
/// Get translated text
|
||||
pub fn get_locale_text(message_id: &str) -> String {
|
||||
LANGUAGE_LOADER.get(message_id)
|
||||
}
|
||||
|
||||
// Get available languages
|
||||
/// Get available languages
|
||||
pub fn get_available_languages() -> Result<Vec<unic_langid::LanguageIdentifier>, I18nEmbedError> {
|
||||
LANGUAGE_LOADER.available_languages(&Localizations)
|
||||
}
|
||||
|
||||
// Check if language is available
|
||||
/// Check if language is available
|
||||
pub fn check_language_valid(requested_language: &str) -> bool {
|
||||
let available_languages = get_available_languages().unwrap();
|
||||
available_languages.iter().any(|x| {
|
||||
|
@ -49,3 +49,17 @@ pub fn check_language_valid(requested_language: &str) -> bool {
|
|||
lang == requested_language
|
||||
})
|
||||
}
|
||||
|
||||
/// Get system language or default language if system one is not supported
|
||||
///
|
||||
/// Checks env variables in following order:
|
||||
/// - `LC_ALL`
|
||||
/// - `LC_MESSAGES`
|
||||
/// - `LANG`
|
||||
pub fn get_default_lang() -> String {
|
||||
std::env::var("LC_ALL").unwrap_or_else(|_| {
|
||||
std::env::var("LC_MESSAGES").unwrap_or_else(|_| {
|
||||
std::env::var("LANG").unwrap_or_else(|_| String::from("en_US.UTF-8"))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
@ -350,7 +350,7 @@ pub fn get_best_locale(
|
|||
return Ok(preferences["default_locale"].as_str().unwrap().to_owned());
|
||||
}
|
||||
|
||||
let locale_name = std::env::var("LC_MESSAGES").unwrap_or_else(|_| String::from("en_US.UTF-8"));
|
||||
let locale_name = crate::localization::get_default_lang();
|
||||
let sys_locale =
|
||||
string_substr(locale_name.as_str(), 0, locale_name.find('.').unwrap_or(usize::MAX))?;
|
||||
let two_letters = string_substr(sys_locale, 0, 2)?;
|
||||
|
|
Loading…
Reference in New Issue