Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

feat: localize the tray menu for Chinese systems

youxi798837e983a4e62
diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml
index c7b98f9e..54e56d96 100644
--- a/src-tauri/Cargo.toml
+++ b/src-tauri/Cargo.toml
@@ -42,6 +42,7 @@ objc2 = "0.6"
objc2-app-kit = { version = "0.3", features = ["NSApplication", "NSDockTile", "NSWindow"] }
objc2-foundation = { version = "0.3", features = [
"NSArray",
+ "NSLocale",
"NSString",
"NSUserNotification",
] }
@@ -63,6 +64,7 @@ zbus = "5.14"
[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.61.2", features = [
"Win32_Foundation",
+ "Win32_Globalization",
"Win32_UI_Shell",
"Win32_UI_WindowsAndMessaging",
] }
diff --git a/src-tauri/src/app/setup.rs b/src-tauri/src/app/setup.rs
index b2a4824c..c1b6138c 100644
--- a/src-tauri/src/app/setup.rs
+++ b/src-tauri/src/app/setup.rs
@@ -2,6 +2,7 @@ use crate::app::window::{
hide_all_app_windows, open_additional_window_safe, show_all_app_windows, toggle_all_app_windows,
};
use crate::cancel_startup_reveal;
+use crate::util::UiLanguage;
use std::str::FromStr;
use std::sync::{atomic::AtomicBool, Arc, Mutex};
use std::time::{Duration, Instant};
@@ -13,6 +14,38 @@ use tauri::{
use tauri_plugin_global_shortcut::{GlobalShortcutExt, Shortcut};
use tauri_plugin_window_state::{AppHandleExt, StateFlags};
+struct TrayLabels {
+ new_window: &'static str,
+ hide: &'static str,
+ show: &'static str,
+ quit: &'static str,
+}
+
+impl TrayLabels {
+ fn for_language(language: UiLanguage) -> Self {
+ match language {
+ UiLanguage::English => TrayLabels {
+ new_window: "New Window",
+ hide: "Hide",
+ show: "Show",
+ quit: "Quit",
+ },
+ UiLanguage::SimplifiedChinese => TrayLabels {
+ new_window: "新建窗口",
+ hide: "隐藏",
+ show: "显示",
+ quit: "退出",
+ },
+ UiLanguage::TraditionalChinese => TrayLabels {
+ new_window: "新增視窗",
+ hide: "隱藏",
+ show: "顯示",
+ quit: "結束",
+ },
+ }
+ }
+}
+
pub fn set_system_tray(
app: &AppHandle,
show_system_tray: bool,
@@ -29,10 +62,11 @@ pub fn set_system_tray(
// Menu events are broadcast to every handler in Tauri v2, so the tray item
// must not share the "new_window" id with the app menu accelerator
// (Cmd/Ctrl+N), or one click opens two windows.
- let new_window = MenuItemBuilder::with_id("tray_new_window", "New Window").build(app)?;
- let hide_app = MenuItemBuilder::with_id("hide_app", "Hide").build(app)?;
- let show_app = MenuItemBuilder::with_id("show_app", "Show").build(app)?;
- let quit = MenuItemBuilder::with_id("quit", "Quit").build(app)?;
+ let labels = TrayLabels::for_language(UiLanguage::system());
+ let new_window = MenuItemBuilder::with_id("tray_new_window", labels.new_window).build(app)?;
+ let hide_app = MenuItemBuilder::with_id("hide_app", labels.hide).build(app)?;
+ let show_app = MenuItemBuilder::with_id("show_app", labels.show).build(app)?;
+ let quit = MenuItemBuilder::with_id("quit", labels.quit).build(app)?;
let menu = if allow_multi_window {
MenuBuilder::new(app)
diff --git a/src-tauri/src/util.rs b/src-tauri/src/util.rs
index 858c0ecf..12fa478b 100644
--- a/src-tauri/src/util.rs
+++ b/src-tauri/src/util.rs
@@ -322,6 +322,71 @@ pub fn get_download_message_with_lang(
.to_string()
}
+/// Language of the user's system interface, for native strings such as the
+/// tray menu that the page's `navigator.language` cannot reach.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum UiLanguage {
+ English,
+ SimplifiedChinese,
+ TraditionalChinese,
+}
+
+impl UiLanguage {
+ /// Classifies a BCP 47 tag (`zh-Hant-TW`) or a POSIX locale (`zh_TW.UTF-8`).
+ pub fn from_tag(tag: &str) -> Self {
+ let tag = tag.to_ascii_lowercase();
+ let mut parts = tag.split(['-', '_', '.', '@']);
+ if parts.next() != Some("zh") {
+ return UiLanguage::English;
+ }
+ if parts.any(|part| matches!(part, "hant" | "tw" | "hk" | "mo")) {
+ UiLanguage::TraditionalChinese
+ } else {
+ UiLanguage::SimplifiedChinese
+ }
+ }
+
+ /// Classifies a Windows LANGID from `GetUserDefaultUILanguage`.
+ #[cfg(any(windows, test))]
+ fn from_windows_langid(langid: u16) -> Self {
+ const LANG_CHINESE: u16 = 0x04;
+ if langid & 0x3ff != LANG_CHINESE {
+ return UiLanguage::English;
+ }
+ // Sublanguages 1, 3 and 5 are Taiwan, Hong Kong and Macao.
+ match langid >> 10 {
+ 0x01 | 0x03 | 0x05 => UiLanguage::TraditionalChinese,
+ _ => UiLanguage::SimplifiedChinese,
+ }
+ }
+
+ /// Apps launched from Finder or Explorer carry no `LANG`, so macOS and
+ /// Windows ask the system for the preferred interface language.
+ pub fn system() -> Self {
+ #[cfg(target_os = "macos")]
+ {
+ objc2_foundation::NSLocale::preferredLanguages()
+ .firstObject()
+ .map(|tag| UiLanguage::from_tag(&tag.to_string()))
+ .unwrap_or(UiLanguage::English)
+ }
+ #[cfg(windows)]
+ {
+ use windows_sys::Win32::Globalization::GetUserDefaultUILanguage;
+ UiLanguage::from_windows_langid(unsafe { GetUserDefaultUILanguage() })
+ }
+ #[cfg(not(any(target_os = "macos", windows)))]
+ {
+ ["LC_ALL", "LC_MESSAGES", "LANG"]
+ .iter()
+ .filter_map(|var| env::var(var).ok())
+ .find(|value| !value.is_empty())
+ .map(|value| UiLanguage::from_tag(&value))
+ .unwrap_or(UiLanguage::English)
+ }
+ }
+}
+
pub fn sanitize_download_filename(filename: &str) -> String {
let Some(candidate) = filename
.rsplit(['/', '\\'])
@@ -601,6 +666,53 @@ mod tests {
}
}
+ #[test]
+ fn ui_language_reads_bcp47_and_posix_tags() {
+ for tag in ["en", "en-US", "fr_FR.UTF-8", "zhx", ""] {
+ assert_eq!(UiLanguage::from_tag(tag), UiLanguage::English, "{tag}");
+ }
+ for tag in ["zh", "zh-Hans", "zh-Hans-CN", "zh_CN.UTF-8", "zh-SG"] {
+ assert_eq!(
+ UiLanguage::from_tag(tag),
+ UiLanguage::SimplifiedChinese,
+ "{tag}"
+ );
+ }
+ for tag in [
+ "zh-Hant",
+ "zh-Hant-HK",
+ "zh-TW",
+ "zh_TW.UTF-8",
+ "zh_HK",
+ "zh-MO",
+ ] {
+ assert_eq!(
+ UiLanguage::from_tag(tag),
+ UiLanguage::TraditionalChinese,
+ "{tag}"
+ );
+ }
+ }
+
+ #[test]
+ fn ui_language_reads_windows_langids() {
+ assert_eq!(UiLanguage::from_windows_langid(0x0409), UiLanguage::English);
+ assert_eq!(
+ UiLanguage::from_windows_langid(0x0804),
+ UiLanguage::SimplifiedChinese
+ );
+ assert_eq!(
+ UiLanguage::from_windows_langid(0x1004),
+ UiLanguage::SimplifiedChinese
+ );
+ for traditional in [0x0404, 0x0C04, 0x1404] {
+ assert_eq!(
+ UiLanguage::from_windows_langid(traditional),
+ UiLanguage::TraditionalChinese
+ );
+ }
+ }
+
#[test]
fn download_message_failure_localized() {
let en = get_download_message_with_lang(MessageType::Failure, Some("en".into()));