Skip to content

Commit 43a5053

Browse files
committed
Fix zh-Hant locale fallback
1 parent 3afb4f7 commit 43a5053

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

crates/edit/src/bin/edit/localization.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ static mut S_LANG: LangId = LangId::en;
1212
pub fn init() {
1313
let scratch = scratch_arena(None);
1414
let langs = sys::preferred_languages(&scratch);
15+
let lang = select_language(langs);
16+
17+
unsafe {
18+
S_LANG = lang;
19+
}
20+
}
21+
22+
fn select_language<'a>(langs: impl IntoIterator<Item = &'a str>) -> LangId {
1523
let mut lang = LangId::en;
1624

1725
'outer: for l in langs {
@@ -23,11 +31,30 @@ pub fn init() {
2331
}
2432
}
2533

26-
unsafe {
27-
S_LANG = lang;
28-
}
34+
lang
2935
}
3036

3137
pub fn loc(id: LocId) -> &'static str {
3238
TRANSLATIONS[unsafe { S_LANG as usize }][id as usize]
3339
}
40+
41+
#[cfg(test)]
42+
mod tests {
43+
use super::*;
44+
45+
fn assert_lang(langs: &[&str], expected: LangId) {
46+
assert!(select_language(langs.iter().copied()) == expected);
47+
}
48+
49+
// Regression test for https://github.com/microsoft/edit/issues/832.
50+
#[test]
51+
fn chinese_region_aliases_select_expected_script() {
52+
assert_lang(&["zh-CN.UTF-8"], LangId::zh_hans);
53+
assert_lang(&["zh-SG.UTF-8"], LangId::zh_hans);
54+
assert_lang(&["zh-TW.UTF-8"], LangId::zh_hant);
55+
assert_lang(&["zh-HK.UTF-8"], LangId::zh_hant);
56+
assert_lang(&["zh-MO.UTF-8"], LangId::zh_hant);
57+
assert_lang(&["zh-Hant.UTF-8"], LangId::zh_hant);
58+
assert_lang(&["zh"], LangId::zh_hans);
59+
}
60+
}

i18n/edit.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ __default__ = [
1515

1616
[__alias__]
1717
sr = "sr-cyrl"
18+
zh-cn = "zh-hans"
19+
zh-hk = "zh-hant"
20+
zh-mo = "zh-hant"
21+
zh-sg = "zh-hans"
22+
zh-tw = "zh-hant"
1823
zh = "zh-hans"
1924

2025
# The keyboard key

0 commit comments

Comments
 (0)