Skip to content

Commit

Permalink
refactor: finish l10n
Browse files Browse the repository at this point in the history
  • Loading branch information
sjfhsjfh committed Feb 6, 2025
1 parent 72a859f commit 27f242f
Show file tree
Hide file tree
Showing 13 changed files with 328 additions and 350 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cargo_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ jobs:

- name: Run tests
run: |
cargo test -p phira --no-default-features -- --skip test_parse_chart
cargo test --workspace --exclude prpr-avc --exclude phira-monitor --no-default-features -- --skip test_parse_chart
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 5 additions & 76 deletions phira/tests/integrated_test.rs
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(())
}
1 change: 1 addition & 0 deletions prpr-l10n/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ once_cell = "1.16.0"
sys-locale = "0.3.1"
tracing = "0.1.37"
unic-langid = { version = "0.9.1", features = ["macros"] }
walkdir = "2.3.3"
42 changes: 42 additions & 0 deletions prpr-l10n/src/global.rs
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(),
}
}
}
Loading

0 comments on commit 27f242f

Please sign in to comment.