Skip to content

Commit

Permalink
refactor(oma-refresh): use sys-locale to get locales from system (#336
Browse files Browse the repository at this point in the history
)
  • Loading branch information
eatradish authored Jan 23, 2025
1 parent 8b7c3e6 commit 527ad94
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 18 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ i18n-embed = { version = "0.15.0", features = ["fluent-system", "desktop-request
i18n-embed-fl = "0.9.1"
rust-embed = "8.5.0"
unic-langid = "0.9.5"
sys-locale = "0.3"

[features]
aosc = ["dep:oma-topics", "dep:oma-mirror", "oma-refresh/aosc", "oma-pm/aosc", "oma-contents/aosc", "reqwest/blocking"]
Expand Down
1 change: 1 addition & 0 deletions oma-refresh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ bon = "3"
once_cell = "1.19"
apt-auth-config = { version = "0.2.0", path = "../apt-auth-config" }
deb822-lossless = { version = "0.2", features = ["derive"] }
sys-locale = "0.3"

[features]
aosc = ["dep:oma-topics"]
Expand Down
42 changes: 25 additions & 17 deletions oma-refresh/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{
borrow::Cow,
collections::{HashMap, VecDeque},
env,
path::Path,
};

Expand Down Expand Up @@ -71,16 +70,13 @@ pub struct IndexTargetConfig<'a> {
deb_src: Vec<(String, HashMap<String, String>)>,
replacer: AhoCorasick,
native_arch: &'a str,
langs: Vec<Box<str>>,
langs: Vec<String>,
}

impl<'a> IndexTargetConfig<'a> {
pub fn new(config: &Config, native_arch: &'a str) -> Self {
let lang = env::var("LANG").map(Cow::Owned).unwrap_or("C".into());
let langs = get_matches_language(&lang)
.into_iter()
.map(Box::from)
.collect::<Vec<_>>();
let locales = sys_locale::get_locales();
let langs = get_matches_language(locales);

Self {
deb: get_index_target_tree(config, "Acquire::IndexTargets::deb"),
Expand Down Expand Up @@ -252,17 +248,23 @@ pub struct ChecksumDownloadEntry {
pub msg: String,
}

fn get_matches_language(env_lang: &str) -> Vec<&str> {
fn get_matches_language(locales: impl IntoIterator<Item = String>) -> Vec<String> {
let mut langs = vec![];
let env_lang = env_lang.split_once('.').map(|x| x.0).unwrap_or(env_lang);

let lang = if env_lang == "C" { "en" } else { env_lang };
for locale in locales {
if locale.eq_ignore_ascii_case("c") {
langs.push("en".to_string());
continue;
}

// apt 数据库使用下划线来设置 translation 文件名
let locale = locale.replace("-", "_");

langs.push(lang);
if let Some((lang, _)) = locale.split_once("_") {
langs.push(lang.to_lowercase());
}

// en_US.UTF-8 => en
if let Some((a, _)) = lang.split_once('_') {
langs.push(a);
langs.push(locale);
}

langs
Expand All @@ -281,9 +283,15 @@ fn compress_file(name: &str) -> CompressFile {

#[test]
fn test_get_matches_language() {
assert_eq!(get_matches_language("C"), vec!["en"]);
assert_eq!(get_matches_language("zh_CN.UTF-8"), vec!["zh_CN", "zh"]);
assert_eq!(get_matches_language("en_US.UTF-8"), vec!["en_US", "en"]);
assert_eq!(get_matches_language(vec!["C".to_string()]), vec!["en"]);
assert_eq!(
get_matches_language(vec!["zh-CN".to_string()]),
vec!["zh", "zh_CN"]
);
assert_eq!(
get_matches_language(vec!["en-US".to_string()]),
vec!["en", "en_US"]
);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub fn print_version() {
}

fn after_help() -> &'static str {
let Ok(lang) = env::var("LANG") else {
let Some(lang) = sys_locale::get_locale() else {
return "";
};

Expand Down

0 comments on commit 527ad94

Please sign in to comment.