Skip to content

armgit / Handy

Public Imported from GitHub
0

A free, open source, and extensible speech-to-text application that works completely offline.

fix(i18n): resolve zh-Hant-* system locales to Traditional Chinese (zh-TW) (#1798)

Fred Chuea3c20a3a67c
diff --git a/src-tauri/src/tray_i18n.rs b/src-tauri/src/tray_i18n.rs
index 1f896dbe..2bdc84ca 100644
--- a/src-tauri/src/tray_i18n.rs
+++ b/src-tauri/src/tray_i18n.rs
@@ -20,15 +20,63 @@ include!(concat!(env!("OUT_DIR"), "/tray_translations.rs"));
/// Get localized tray menu strings based on the system locale.
///
-/// Lookup order: full locale (e.g. "zh-TW") → language code ("zh") → English.
+/// Lookup order: exact locale → Chinese script/region fallback → language code → English.
pub fn get_tray_translations(locale: Option<String>) -> TrayStrings {
- let locale_str = locale.as_deref().unwrap_or("en");
- let lang_code = locale_str.split(['-', '_']).next().unwrap_or("en");
+ let normalized = locale
+ .as_deref()
+ .unwrap_or("en")
+ .to_lowercase()
+ .replace('_', "-");
+ let subtags: Vec<_> = normalized.split('-').collect();
+ let language = subtags.first().copied().unwrap_or("en");
+ let is_hant = subtags.contains(&"hant");
+ let is_hans = subtags.contains(&"hans");
+ let is_traditional_region = ["tw", "hk", "mo"]
+ .iter()
+ .any(|region| subtags.contains(region));
- TRANSLATIONS
- .get(locale_str)
- .or_else(|| TRANSLATIONS.get(lang_code))
+ let exact_match = TRANSLATIONS
+ .iter()
+ .find_map(|(code, strings)| code.eq_ignore_ascii_case(&normalized).then_some(strings));
+ let fallback = match language {
+ "zh" if is_hant || (!is_hans && is_traditional_region) => "zh-TW",
+ // Cantonese uses Traditional Chinese unless explicitly tagged as Hans.
+ "yue" if is_hans => "zh",
+ "yue" => "zh-TW",
+ _ => language,
+ };
+
+ exact_match
+ .or_else(|| TRANSLATIONS.get(fallback))
.or_else(|| TRANSLATIONS.get("en"))
.cloned()
.expect("English translations must exist")
}
+
+#[cfg(test)]
+mod tests {
+ use super::{get_tray_translations, TRANSLATIONS};
+
+ #[test]
+ fn resolves_locale_fallbacks() {
+ for (locale, expected) in [
+ ("zh-Hant-TW", "zh-TW"),
+ ("zh-Hant-HK", "zh-TW"),
+ ("zh-HK", "zh-TW"),
+ ("zh-MO", "zh-TW"),
+ ("ZH-TW", "zh-TW"),
+ ("zh_Hant_TW", "zh-TW"),
+ ("zh-Hans-CN", "zh"),
+ ("yue-Hant-HK", "zh-TW"),
+ ("yue-Hans-CN", "zh"),
+ ("fr-FR", "fr"),
+ ("xx-YY", "en"),
+ ] {
+ assert_eq!(
+ format!("{:?}", get_tray_translations(Some(locale.into()))),
+ format!("{:?}", TRANSLATIONS[expected]),
+ "{locale} should resolve to {expected}"
+ );
+ }
+ }
+}
diff --git a/src/components/settings/AppLanguageSelector.tsx b/src/components/settings/AppLanguageSelector.tsx
index 3c41aa1a..69be8631 100644
--- a/src/components/settings/AppLanguageSelector.tsx
+++ b/src/components/settings/AppLanguageSelector.tsx
@@ -2,7 +2,11 @@ import React from "react";
import { useTranslation } from "react-i18next";
import { Dropdown } from "../ui/Dropdown";
import { SettingContainer } from "../ui/SettingContainer";
-import { SUPPORTED_LANGUAGES, type SupportedLanguageCode } from "../../i18n";
+import {
+ SUPPORTED_LANGUAGES,
+ getSupportedLanguage,
+ type SupportedLanguageCode,
+} from "../../i18n";
import { useSettings } from "@/hooks/useSettings";
interface AppLanguageSelectorProps {
@@ -15,7 +19,7 @@ export const AppLanguageSelector: React.FC<AppLanguageSelectorProps> =
const { t, i18n } = useTranslation();
const { settings, updateSetting } = useSettings();
- const currentLanguage = (settings?.app_language ||
+ const currentLanguage = (getSupportedLanguage(settings?.app_language) ||
i18n.language) as SupportedLanguageCode;
const languageOptions = SUPPORTED_LANGUAGES.map((lang) => ({
diff --git a/src/i18n/index.ts b/src/i18n/index.ts
index cf5fd3f2..107e7ce0 100644
--- a/src/i18n/index.ts
+++ b/src/i18n/index.ts
@@ -52,20 +52,34 @@ export const SUPPORTED_LANGUAGES = Object.keys(resources)
export type SupportedLanguageCode = string;
// Check if a language code is supported
-const getSupportedLanguage = (
+export const getSupportedLanguage = (
langCode: string | null | undefined,
): SupportedLanguageCode | null => {
if (!langCode) return null;
- const normalized = langCode.toLowerCase();
+
+ const normalized = langCode.toLowerCase().replace(/_/g, "-");
+ const subtags = normalized.split("-");
+ const language = subtags[0];
+ const isHant = subtags.includes("hant");
+ const isHans = subtags.includes("hans");
+ const isTraditionalRegion = ["tw", "hk", "mo"].some((region) =>
+ subtags.includes(region),
+ );
+
// Try exact match first
let supported = SUPPORTED_LANGUAGES.find(
(lang) => lang.code.toLowerCase() === normalized,
);
if (!supported) {
- // Fall back to prefix match (language only, without region)
- const prefix = normalized.split("-")[0];
+ let fallback = language;
+ if (language === "zh" && (isHant || (!isHans && isTraditionalRegion))) {
+ fallback = "zh-tw";
+ } else if (language === "yue") {
+ // Cantonese uses Traditional Chinese unless explicitly tagged as Hans.
+ fallback = isHans ? "zh" : "zh-tw";
+ }
supported = SUPPORTED_LANGUAGES.find(
- (lang) => lang.code.toLowerCase() === prefix,
+ (lang) => lang.code.toLowerCase() === fallback,
);
}
return supported ? supported.code : null;