Language: a Traditional Chinese system opens in Traditional Chinese, macOS's zh-Hant-TW included (#10306) (#10307)

* claim: #10306

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019FAWPuFoh9qxn2uS2j53QZ

* Language: a Chinese system tag goes by its script, then its region (#10306)

macOS lists its languages with a script (zh-Hant-TW), which no catalog
matches exactly, so the base zh (Simplified) was picked for a Traditional
Chinese system. Hong Kong and Macao tags got Simplified the same way.

A Chinese tag now picks zh-TW for the Hant script, zh for Hans, and
without a script zh-TW for the TW, HK and MO regions, zh otherwise.
Other languages keep the exact tag, then the base language.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
austin62jakeandClaude Opus 5.5 authored and GitHub committed 2026-11-02 00:15:53 +00:00
1 parent 91a19548d3
commit 1fb9dadcd9
1 file changed
+44 -1
+44 -1
View File
@@ -136,10 +136,14 @@ pub const SYSTEM: &str = "system";
/// Match the system's preferred locale tags against the catalogs: the exact tag first
/// (`zh-TW`, `pt-BR`, `en-GB`), then the tag's base language (`zh-CN` → `zh`, `de-AT` → `de`),
/// then English. Tags come in the system's order of preference; the first one that matches wins.
/// then English. A Chinese tag goes by its script or region instead ([`chinese_catalog`]).
/// Tags come in the system's order of preference; the first one that matches wins.
pub fn match_system_tags(tags: &[String]) -> &'static str {
for tag in tags {
let tag = tag.replace('_', "-");
if let Some(catalog) = chinese_catalog(&tag) {
return catalog;
}
if let Some(catalog) = LOCALES.iter().find(|catalog| catalog.id.eq_ignore_ascii_case(&tag)) {
return catalog.id;
}
@@ -151,6 +155,24 @@ pub fn match_system_tags(tags: &[String]) -> &'static str {
ENGLISH.id
}
/// The catalog for a Chinese tag (#10306): by its script when it has one (`Hant` → `zh-TW`,
/// `Hans` → `zh`), else by its region (`TW`, `HK` and `MO` use Traditional characters, so
/// `zh-TW`; any other region, or none, `zh`). macOS lists its languages with a script
/// (`zh-Hant-TW`), which no catalog matches exactly, and the base `zh` is Simplified.
fn chinese_catalog(tag: &str) -> Option<&'static str> {
let mut parts = tag.split('-');
if !parts.next().is_some_and(|base| base.eq_ignore_ascii_case("zh")) {
return None;
}
let rest: Vec<&str> = parts.collect();
let traditional = match rest.iter().find(|part| part.len() == 4) {
Some(script) => script.eq_ignore_ascii_case("Hant"),
None => rest.iter().any(|part| ["TW", "HK", "MO"].iter().any(|region| part.eq_ignore_ascii_case(region))),
};
let id = if traditional { "zh-TW" } else { "zh" };
LOCALES.iter().find(|catalog| catalog.id == id).map(|catalog| catalog.id)
}
/// The catalog id the system asks for, read once per process: the OS locale on desktop
/// (Windows, macOS, Linux/BSD) and `navigator.languages` in the browser.
pub fn system_language() -> &'static str {
@@ -417,6 +439,27 @@ mod tests {
assert_eq!(super::match_system_tags(&tags(&["xx-YY"])), "en", "unknown: English");
assert_eq!(super::match_system_tags(&tags(&[])), "en", "nothing: English");
assert_eq!(super::match_system_tags(&tags(&["xx-YY", "de-DE"])), "de", "the first tag that matches wins");
// #10306: a Chinese tag goes by its script, else by its region.
for (tag, want) in [
("zh-Hant-TW", "zh-TW"),
("zh-Hant-HK", "zh-TW"),
("zh-Hant", "zh-TW"),
("zh_Hant_MO", "zh-TW"),
("ZH-HANT-tw", "zh-TW"),
("zh-Hant-CN", "zh-TW"),
("zh-HK", "zh-TW"),
("zh-MO", "zh-TW"),
("zh-Hans-CN", "zh"),
("zh-Hans", "zh"),
("zh-Hans-TW", "zh"),
("zh-SG", "zh"),
("zh", "zh"),
] {
assert_eq!(super::match_system_tags(&tags(&[tag])), want, "{tag}");
}
// Other languages keep the exact tag, then the base language.
assert_eq!(super::match_system_tags(&tags(&["sr-Latn-RS"])), "sr");
assert_eq!(super::match_system_tags(&tags(&["zhx-TW", "de"])), "de", "only zh itself is Chinese");
}
/// Tests name English words, so the PC's own language never reaches them (#10297).