From 5e6ec05da55f789de3568b060129da557d1270d0 Mon Sep 17 00:00:00 2001 From: qaijuang Date: Sat, 2 May 2026 05:01:51 -0400 Subject: [PATCH 1/2] Fix zh-Hant locale fallback --- crates/edit/src/bin/edit/localization.rs | 33 +++++++++++++++++++++--- i18n/edit.toml | 5 ++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/crates/edit/src/bin/edit/localization.rs b/crates/edit/src/bin/edit/localization.rs index fa22dc89c45..492481934e9 100644 --- a/crates/edit/src/bin/edit/localization.rs +++ b/crates/edit/src/bin/edit/localization.rs @@ -12,6 +12,14 @@ static mut S_LANG: LangId = LangId::en; pub fn init() { let scratch = scratch_arena(None); let langs = sys::preferred_languages(&scratch); + let lang = select_language(langs); + + unsafe { + S_LANG = lang; + } +} + +fn select_language<'a>(langs: impl IntoIterator) -> LangId { let mut lang = LangId::en; 'outer: for l in langs { @@ -23,11 +31,30 @@ pub fn init() { } } - unsafe { - S_LANG = lang; - } + lang } pub fn loc(id: LocId) -> &'static str { TRANSLATIONS[unsafe { S_LANG as usize }][id as usize] } + +#[cfg(test)] +mod tests { + use super::*; + + fn assert_lang(langs: &[&str], expected: LangId) { + assert!(select_language(langs.iter().copied()) == expected); + } + + // Regression test for https://github.com/microsoft/edit/issues/832. + #[test] + fn chinese_region_aliases_select_expected_script() { + assert_lang(&["zh-CN.UTF-8"], LangId::zh_hans); + assert_lang(&["zh-SG.UTF-8"], LangId::zh_hans); + assert_lang(&["zh-TW.UTF-8"], LangId::zh_hant); + assert_lang(&["zh-HK.UTF-8"], LangId::zh_hant); + assert_lang(&["zh-MO.UTF-8"], LangId::zh_hant); + assert_lang(&["zh-Hant.UTF-8"], LangId::zh_hant); + assert_lang(&["zh"], LangId::zh_hans); + } +} diff --git a/i18n/edit.toml b/i18n/edit.toml index 79693eb7d67..afafb3dd703 100644 --- a/i18n/edit.toml +++ b/i18n/edit.toml @@ -15,6 +15,11 @@ __default__ = [ [__alias__] sr = "sr-cyrl" +zh-cn = "zh-hans" +zh-hk = "zh-hant" +zh-mo = "zh-hant" +zh-sg = "zh-hans" +zh-tw = "zh-hant" zh = "zh-hans" # The keyboard key From 4ee39f2ea503192442b4e38f5793e10868da6852 Mon Sep 17 00:00:00 2001 From: Leonard Hecker Date: Mon, 4 May 2026 17:10:11 +0200 Subject: [PATCH 2/2] Make tests robust against EDIT_CFG_LANGUAGES --- crates/edit/src/bin/edit/localization.rs | 26 ++++++++++++++---------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/crates/edit/src/bin/edit/localization.rs b/crates/edit/src/bin/edit/localization.rs index 492481934e9..e1aaad25781 100644 --- a/crates/edit/src/bin/edit/localization.rs +++ b/crates/edit/src/bin/edit/localization.rs @@ -42,19 +42,23 @@ pub fn loc(id: LocId) -> &'static str { mod tests { use super::*; - fn assert_lang(langs: &[&str], expected: LangId) { - assert!(select_language(langs.iter().copied()) == expected); - } - // Regression test for https://github.com/microsoft/edit/issues/832. #[test] fn chinese_region_aliases_select_expected_script() { - assert_lang(&["zh-CN.UTF-8"], LangId::zh_hans); - assert_lang(&["zh-SG.UTF-8"], LangId::zh_hans); - assert_lang(&["zh-TW.UTF-8"], LangId::zh_hant); - assert_lang(&["zh-HK.UTF-8"], LangId::zh_hant); - assert_lang(&["zh-MO.UTF-8"], LangId::zh_hant); - assert_lang(&["zh-Hant.UTF-8"], LangId::zh_hant); - assert_lang(&["zh"], LangId::zh_hans); + for (actual, expected) in [ + ("zh-CN.UTF-8", "zh-hans"), + ("zh-SG.UTF-8", "zh-hans"), + ("zh-TW.UTF-8", "zh-hant"), + ("zh-HK.UTF-8", "zh-hant"), + ("zh-MO.UTF-8", "zh-hant"), + ("zh-Hant.UTF-8", "zh-hant"), + ("zh", "zh-hans"), + ] { + let Some(&(_, expected)) = LANGUAGES.iter().find(|(l, _)| expected == *l) else { + continue; // Disabled by EDIT_CFG_LANGUAGES + }; + + assert!(select_language(std::iter::once(actual)) == expected); + } } }