refactor: remove unsafe in i18n

This commit is contained in:
Luk-ESC 2024-07-08 20:45:40 +02:00
parent b7bfda5a85
commit 462ada831c
2 changed files with 14 additions and 14 deletions

View file

@ -1,3 +1,4 @@
use std::sync::OnceLock;
use unic_langid::{langid, LanguageIdentifier}; use unic_langid::{langid, LanguageIdentifier};
fluent_templates::static_loader! { fluent_templates::static_loader! {
@ -32,14 +33,15 @@ pub const SUPPORTED_LANGUAGES: &[LanguageIdentifier] = &[
langid!("cs-cz") langid!("cs-cz")
]; ];
pub static mut LANG: LanguageIdentifier = langid!("en-us"); /// Fallback used if the system language is not supported
static FALLBACK: LanguageIdentifier = langid!("en-us");
pub static LANG: OnceLock<LanguageIdentifier> = OnceLock::new();
/// Set launcher language /// Set launcher language
pub fn set_lang(lang: LanguageIdentifier) -> anyhow::Result<()> { pub fn set_lang(lang: LanguageIdentifier) -> anyhow::Result<()> {
if SUPPORTED_LANGUAGES.iter().any(|item| item.language == lang.language) { if SUPPORTED_LANGUAGES.iter().any(|item| item.language == lang.language) {
unsafe { LANG.set(lang).expect("Can't overwrite language!");
LANG = lang
}
Ok(()) Ok(())
} }
@ -50,8 +52,8 @@ pub fn set_lang(lang: LanguageIdentifier) -> anyhow::Result<()> {
} }
/// Get launcher language /// Get launcher language
pub fn get_lang() -> LanguageIdentifier { pub fn get_lang() -> &'static LanguageIdentifier {
unsafe { LANG.clone() } LANG.get().expect("Language hasn't been initialized!")
} }
/// Get system language or default language if system one is not supported /// Get system language or default language if system one is not supported
@ -60,7 +62,7 @@ pub fn get_lang() -> LanguageIdentifier {
/// - `LC_ALL` /// - `LC_ALL`
/// - `LC_MESSAGES` /// - `LC_MESSAGES`
/// - `LANG` /// - `LANG`
pub fn get_default_lang() -> LanguageIdentifier { pub fn get_default_lang() -> &'static LanguageIdentifier {
let current = std::env::var("LC_ALL") let current = std::env::var("LC_ALL")
.unwrap_or_else(|_| std::env::var("LC_MESSAGES") .unwrap_or_else(|_| std::env::var("LC_MESSAGES")
.unwrap_or_else(|_| std::env::var("LANG") .unwrap_or_else(|_| std::env::var("LANG")
@ -69,11 +71,11 @@ pub fn get_default_lang() -> LanguageIdentifier {
for lang in SUPPORTED_LANGUAGES { for lang in SUPPORTED_LANGUAGES {
if current.starts_with(lang.language.as_str()) { if current.starts_with(lang.language.as_str()) {
return lang.clone(); return lang;
} }
} }
get_lang() &FALLBACK
} }
pub fn format_lang(lang: &LanguageIdentifier) -> String { pub fn format_lang(lang: &LanguageIdentifier) -> String {
@ -106,8 +108,7 @@ macro_rules! tr {
{ {
use fluent_templates::Loader; use fluent_templates::Loader;
#[allow(unused_unsafe)] $crate::i18n::LOCALES.lookup($crate::i18n::get_lang(), $id)
$crate::i18n::LOCALES.lookup(unsafe { $crate::i18n::LANG.as_ref() }, $id)
} }
}; };
@ -124,8 +125,7 @@ macro_rules! tr {
args.insert($key, FluentValue::from($value)); args.insert($key, FluentValue::from($value));
)* )*
#[allow(unused_unsafe)] $crate::i18n::LOCALES.lookup_complete($crate::i18n::get_lang(), $id, Some(&args))
$crate::i18n::LOCALES.lookup_complete(unsafe { $crate::i18n::LANG.as_ref() }, $id, Some(&args))
} }
}; };
} }

View file

@ -120,7 +120,7 @@ fn main() -> anyhow::Result<()> {
// CONFIG is initialized lazily so it will contain following changes as well // CONFIG is initialized lazily so it will contain following changes as well
let mut config = Config::get().expect("Failed to get config"); let mut config = Config::get().expect("Failed to get config");
config.launcher.language = i18n::format_lang(&i18n::get_default_lang()); config.launcher.language = i18n::format_lang(i18n::get_default_lang());
Config::update_raw(config).expect("Failed to update config"); Config::update_raw(config).expect("Failed to update config");
} }