-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
328 additions
and
350 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,9 @@ | ||
use once_cell::sync::Lazy; | ||
use prpr::l10n::{LANGS, LANG_IDENTS}; | ||
use std::collections::HashSet; | ||
use std::error::Error; | ||
use std::fmt::Display; | ||
use std::path::Path; | ||
use walkdir::WalkDir; | ||
use prpr_l10n::tools::check_langfile; | ||
|
||
#[derive(Debug)] | ||
struct IllegalLanguages { | ||
pub languages: Vec<String>, | ||
} | ||
|
||
impl Display for IllegalLanguages { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{:?}", self.languages)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl Error for IllegalLanguages {} | ||
|
||
fn get_ftl_files(path: &Path) -> Result<HashSet<String>, Box<dyn Error>> { | ||
let mut files = HashSet::new(); | ||
for entry in WalkDir::new(path) { | ||
let entry = entry?; | ||
if entry.file_type().is_file() { | ||
let path = entry.path(); | ||
if path.extension().map_or(false, |ext| ext == "ftl") { | ||
let relative_path = path.strip_prefix(path.parent().unwrap())?; | ||
let normalized = relative_path.to_string_lossy().replace('\\', "/"); | ||
files.insert(normalized); | ||
} | ||
} | ||
} | ||
Ok(files) | ||
} | ||
#[test] | ||
fn check_langid() -> anyhow::Result<()> { | ||
// Lang ID is illegal if panicked | ||
Lazy::force(&LANG_IDENTS); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn check_langfile() -> Result<(), Box<dyn Error>> { | ||
let locales_dir = Path::new("locales"); | ||
let zh_cn_dir = locales_dir.join("zh-CN"); | ||
let all_locales: [std::path::PathBuf; 12] = LANGS.map(|x| locales_dir.join(x)); | ||
let zh_cn_files = get_ftl_files(&zh_cn_dir)?; | ||
let mut inconsistent_languages = Vec::new(); | ||
let mut i = 0; | ||
while i < LANGS.len() { | ||
let path = all_locales[i].to_owned(); | ||
if path.is_dir() { | ||
let lang_code = LANGS[i]; | ||
i += 1; | ||
if lang_code == "zh-CN" { | ||
continue; | ||
} | ||
|
||
match get_ftl_files(&path) { | ||
Ok(files) => { | ||
if files != zh_cn_files { | ||
inconsistent_languages.push(lang_code); | ||
} | ||
} | ||
Err(_) => inconsistent_languages.push(lang_code), | ||
} | ||
} | ||
fn check_all() { | ||
match check_langfile(concat!(env!("CARGO_MANIFEST_DIR"), "/locales/")) { | ||
Ok(_) => {} | ||
Err(e) => panic!("Error: {}", e), | ||
} | ||
|
||
if !inconsistent_languages.is_empty() { | ||
return Err(Box::new(IllegalLanguages { | ||
languages: inconsistent_languages.iter().map(|x| x.to_string()).collect(), | ||
})); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use std::{collections::HashMap, sync::Mutex}; | ||
|
||
use tracing::warn; | ||
use unic_langid::LanguageIdentifier; | ||
|
||
use crate::{fallback_langid, FALLBACK_LANG, LANG_IDENTS}; | ||
|
||
pub struct L10nGlobal { | ||
pub lang_map: HashMap<LanguageIdentifier, usize>, | ||
pub order: Mutex<Vec<usize>>, | ||
} | ||
|
||
impl Default for L10nGlobal { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl L10nGlobal { | ||
pub fn new() -> Self { | ||
let mut lang_map = HashMap::new(); | ||
let mut order = Vec::new(); | ||
let locale_lang = sys_locale::get_locale().unwrap_or_else(|| String::from(FALLBACK_LANG)); | ||
let locale_lang: LanguageIdentifier = locale_lang.parse().unwrap_or_else(|_| { | ||
warn!("Invalid locale detected, defaulting to `{}`", FALLBACK_LANG); | ||
// Debug log: send lang tag to log | ||
warn!("Locale detected: {:?}", locale_lang); | ||
fallback_langid!() | ||
}); | ||
for (id, lang) in LANG_IDENTS.iter().enumerate() { | ||
lang_map.insert(lang.clone(), id); | ||
if *lang == locale_lang { | ||
order.push(id); | ||
} | ||
} | ||
order.push(*lang_map.get(&fallback_langid!()).unwrap()); | ||
Self { | ||
lang_map, | ||
order: order.into(), | ||
} | ||
} | ||
} |
Oops, something went wrong.