Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions crates/edit/src/bin/edit/localization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = &'a str>) -> LangId {
let mut lang = LangId::en;

'outer: for l in langs {
Expand All @@ -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);
}
}
}
5 changes: 5 additions & 0 deletions i18n/edit.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading