@@ -12,6 +12,14 @@ static mut S_LANG: LangId = LangId::en;
1212pub 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
3137pub 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+ }
0 commit comments