diff --git a/crates/edit/src/bin/edit/localization.rs b/crates/edit/src/bin/edit/localization.rs index fa22dc89c45..e1aaad25781 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,34 @@ 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::*; + + // Regression test for https://github.com/microsoft/edit/issues/832. + #[test] + fn chinese_region_aliases_select_expected_script() { + 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); + } + } +} 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