From 7947da9d2571af6c3c5823f40104f40893f93d87 Mon Sep 17 00:00:00 2001 From: Ayoze Torres <53948812+ayozetr@users.noreply.github.com> Date: Tue, 11 Aug 2026 17:30:09 +0100 Subject: [PATCH] fix(i18n): make the locale fallback test independent of the system locale `resolve_locale` consults the machine's UI language whenever the preferred tag is unknown, so `unknown_tag_resolves_to_english` only held on machines whose own locale is English or unsupported. On a Spanish system the unknown tag "xx" falls through to "es" and the test fails, which makes `cargo test` unreliable for contributors outside those locales. Split the fallback chain into `resolve_locale_from`, which takes the system language as an argument, and assert against that instead. Also covers the system-language fallback itself, which had no test. --- src/i18n.rs | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/i18n.rs b/src/i18n.rs index 81a9c3d..5daa9dc 100644 --- a/src/i18n.rs +++ b/src/i18n.rs @@ -48,14 +48,15 @@ pub fn native_name(locale: &str) -> &'static str { /// (2) the Windows UI language mapped to the nearest supported locale, /// otherwise (3) English. Returns one of [`UI_LOCALES`]. pub fn resolve_locale(preferred: Option<&str>) -> &'static str { - if let Some(preferred) = preferred { - if let Some(matched) = match_supported(preferred) { - return matched; - } - } + resolve_locale_from(preferred, system_ui_language().as_deref()) +} - if let Some(system) = system_ui_language() { - if let Some(matched) = match_supported(&system) { +/// [`resolve_locale`] with the system UI language supplied by the caller, so +/// the fallback chain can be tested without depending on the locale of the +/// machine running the tests. +fn resolve_locale_from(preferred: Option<&str>, system: Option<&str>) -> &'static str { + for candidate in [preferred, system].into_iter().flatten() { + if let Some(matched) = match_supported(candidate) { return matched; } } @@ -209,8 +210,19 @@ mod tests { #[test] fn unknown_tag_resolves_to_english() { - assert_eq!(resolve_locale(Some("xx")), "en"); - assert_eq!(resolve_locale(Some("de")), "de"); + // Exercised through `resolve_locale_from` with an explicit system + // language: `resolve_locale` reads the machine's UI language, so an + // unknown preferred tag falls through to it and this test would pass + // on an English system while failing on every other one. + assert_eq!(resolve_locale_from(Some("xx"), None), "en"); + assert_eq!(resolve_locale_from(Some("de"), None), "de"); + } + + #[test] + fn system_language_is_the_fallback_for_an_unknown_preference() { + assert_eq!(resolve_locale_from(Some("xx"), Some("fr-CA")), "fr"); + assert_eq!(resolve_locale_from(None, Some("de")), "de"); + assert_eq!(resolve_locale_from(Some("xx"), Some("xx")), "en"); } #[test]