Skip to content

Commit 527ad94

Browse files
authored
refactor(oma-refresh): use sys-locale to get locales from system (#336)
1 parent 8b7c3e6 commit 527ad94

File tree

5 files changed

+39
-18
lines changed

5 files changed

+39
-18
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ i18n-embed = { version = "0.15.0", features = ["fluent-system", "desktop-request
6464
i18n-embed-fl = "0.9.1"
6565
rust-embed = "8.5.0"
6666
unic-langid = "0.9.5"
67+
sys-locale = "0.3"
6768

6869
[features]
6970
aosc = ["dep:oma-topics", "dep:oma-mirror", "oma-refresh/aosc", "oma-pm/aosc", "oma-contents/aosc", "reqwest/blocking"]

oma-refresh/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ bon = "3"
3232
once_cell = "1.19"
3333
apt-auth-config = { version = "0.2.0", path = "../apt-auth-config" }
3434
deb822-lossless = { version = "0.2", features = ["derive"] }
35+
sys-locale = "0.3"
3536

3637
[features]
3738
aosc = ["dep:oma-topics"]

oma-refresh/src/config.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::{
22
borrow::Cow,
33
collections::{HashMap, VecDeque},
4-
env,
54
path::Path,
65
};
76

@@ -71,16 +70,13 @@ pub struct IndexTargetConfig<'a> {
7170
deb_src: Vec<(String, HashMap<String, String>)>,
7271
replacer: AhoCorasick,
7372
native_arch: &'a str,
74-
langs: Vec<Box<str>>,
73+
langs: Vec<String>,
7574
}
7675

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

8581
Self {
8682
deb: get_index_target_tree(config, "Acquire::IndexTargets::deb"),
@@ -252,17 +248,23 @@ pub struct ChecksumDownloadEntry {
252248
pub msg: String,
253249
}
254250

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

259-
let lang = if env_lang == "C" { "en" } else { env_lang };
254+
for locale in locales {
255+
if locale.eq_ignore_ascii_case("c") {
256+
langs.push("en".to_string());
257+
continue;
258+
}
259+
260+
// apt 数据库使用下划线来设置 translation 文件名
261+
let locale = locale.replace("-", "_");
260262

261-
langs.push(lang);
263+
if let Some((lang, _)) = locale.split_once("_") {
264+
langs.push(lang.to_lowercase());
265+
}
262266

263-
// en_US.UTF-8 => en
264-
if let Some((a, _)) = lang.split_once('_') {
265-
langs.push(a);
267+
langs.push(locale);
266268
}
267269

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

282284
#[test]
283285
fn test_get_matches_language() {
284-
assert_eq!(get_matches_language("C"), vec!["en"]);
285-
assert_eq!(get_matches_language("zh_CN.UTF-8"), vec!["zh_CN", "zh"]);
286-
assert_eq!(get_matches_language("en_US.UTF-8"), vec!["en_US", "en"]);
286+
assert_eq!(get_matches_language(vec!["C".to_string()]), vec!["en"]);
287+
assert_eq!(
288+
get_matches_language(vec!["zh-CN".to_string()]),
289+
vec!["zh", "zh_CN"]
290+
);
291+
assert_eq!(
292+
get_matches_language(vec!["en-US".to_string()]),
293+
vec!["en", "en_US"]
294+
);
287295
}
288296

289297
#[test]

src/args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub fn print_version() {
141141
}
142142

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

0 commit comments

Comments
 (0)