Skip to content

Commit

Permalink
refactor: remove unsafe in i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
Luk-ESC committed Jul 8, 2024
1 parent b7bfda5 commit 462ada8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions src/i18n.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::sync::OnceLock;
use unic_langid::{langid, LanguageIdentifier};

fluent_templates::static_loader! {
Expand Down Expand Up @@ -32,14 +33,15 @@ pub const SUPPORTED_LANGUAGES: &[LanguageIdentifier] = &[
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
pub fn set_lang(lang: LanguageIdentifier) -> anyhow::Result<()> {
if SUPPORTED_LANGUAGES.iter().any(|item| item.language == lang.language) {
unsafe {
LANG = lang
}
LANG.set(lang).expect("Can't overwrite language!");

Ok(())
}
Expand All @@ -50,8 +52,8 @@ pub fn set_lang(lang: LanguageIdentifier) -> anyhow::Result<()> {
}

/// Get launcher language
pub fn get_lang() -> LanguageIdentifier {
unsafe { LANG.clone() }
pub fn get_lang() -> &'static LanguageIdentifier {
LANG.get().expect("Language hasn't been initialized!")
}

/// Get system language or default language if system one is not supported
Expand All @@ -60,7 +62,7 @@ pub fn get_lang() -> LanguageIdentifier {
/// - `LC_ALL`
/// - `LC_MESSAGES`
/// - `LANG`
pub fn get_default_lang() -> LanguageIdentifier {
pub fn get_default_lang() -> &'static LanguageIdentifier {
let current = std::env::var("LC_ALL")
.unwrap_or_else(|_| std::env::var("LC_MESSAGES")
.unwrap_or_else(|_| std::env::var("LANG")
Expand All @@ -69,11 +71,11 @@ pub fn get_default_lang() -> LanguageIdentifier {

for lang in SUPPORTED_LANGUAGES {
if current.starts_with(lang.language.as_str()) {
return lang.clone();
return lang;
}
}

get_lang()
&FALLBACK
}

pub fn format_lang(lang: &LanguageIdentifier) -> String {
Expand Down Expand Up @@ -106,8 +108,7 @@ macro_rules! tr {
{
use fluent_templates::Loader;

#[allow(unused_unsafe)]
$crate::i18n::LOCALES.lookup(unsafe { $crate::i18n::LANG.as_ref() }, $id)
$crate::i18n::LOCALES.lookup($crate::i18n::get_lang(), $id)
}
};

Expand All @@ -124,8 +125,7 @@ macro_rules! tr {
args.insert($key, FluentValue::from($value));
)*

#[allow(unused_unsafe)]
$crate::i18n::LOCALES.lookup_complete(unsafe { $crate::i18n::LANG.as_ref() }, $id, Some(&args))
$crate::i18n::LOCALES.lookup_complete($crate::i18n::get_lang(), $id, Some(&args))
}
};
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn main() -> anyhow::Result<()> {
// CONFIG is initialized lazily so it will contain following changes as well
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");
}
Expand Down

0 comments on commit 462ada8

Please sign in to comment.