Skip to content

Commit 428ef55

Browse files
qaijuanglhecker
andauthored
Fix zh-Hant locale fallback (#846)
Region-specific Chinese locales like `zh-TW` were falling through to the broad `zh` alias, which defaults to `zh-Hans`. This PR adds explicit Chinese region aliases so `zh-TW`, `zh-HK`, and `zh-MO` select `zh-Hant`, while `zh-CN` and `zh-SG` continue to select `zh-Hans`. Fixes #832 --------- Co-authored-by: Leonard Hecker <leonard@hecker.io>
1 parent 737450c commit 428ef55

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

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

Lines changed: 34 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,34 @@ 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+
// Regression test for https://github.com/microsoft/edit/issues/832.
46+
#[test]
47+
fn chinese_region_aliases_select_expected_script() {
48+
for (actual, expected) in [
49+
("zh-CN.UTF-8", "zh-hans"),
50+
("zh-SG.UTF-8", "zh-hans"),
51+
("zh-TW.UTF-8", "zh-hant"),
52+
("zh-HK.UTF-8", "zh-hant"),
53+
("zh-MO.UTF-8", "zh-hant"),
54+
("zh-Hant.UTF-8", "zh-hant"),
55+
("zh", "zh-hans"),
56+
] {
57+
let Some(&(_, expected)) = LANGUAGES.iter().find(|(l, _)| expected == *l) else {
58+
continue; // Disabled by EDIT_CFG_LANGUAGES
59+
};
60+
61+
assert!(select_language(std::iter::once(actual)) == expected);
62+
}
63+
}
64+
}

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)