Skip to content

Commit

Permalink
refactor(i18n): change backend localization to rust-i18n (#425)
Browse files Browse the repository at this point in the history
* refactor(i18n): refactor i18n for tray menu

* feat(i18n): add i18n for dialog

* feat(i18n): update locale file type

* refactor(i18n): move locales to top level dir
  • Loading branch information
4o3F committed Feb 13, 2024
1 parent 83c166f commit f7296db
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 75 deletions.
114 changes: 112 additions & 2 deletions backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ rocksdb = "0.21"
thiserror = { workspace = true }
simd-json = "0.13.8"
runas = "=1.0.0" # blocked by https://github.com/mitsuhiko/rust-runas/issues/13
rust-i18n = "3"

[target.'cfg(windows)'.dependencies]
deelevate = "0.2.0"
Expand Down
91 changes: 23 additions & 68 deletions backend/tauri/src/core/tray/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{cmds, config::Config, feat, utils::resolve};
use anyhow::Result;
use rust_i18n::t;
use tauri::{
api, AppHandle, CustomMenuItem, Manager, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem,
SystemTraySubmenu,
Expand All @@ -17,89 +18,52 @@ impl Tray {

let version = env!("NYANPASU_VERSION");

macro_rules! t {
($en: expr, $zh: expr) => {
if zh {
$zh
} else {
$en
}
};
}

SystemTrayMenu::new()
.add_item(CustomMenuItem::new(
"open_window",
t!("Dashboard", "打开面板"),
))
.add_item(CustomMenuItem::new("open_window", t!("tray.dashboard")))
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(CustomMenuItem::new(
"rule_mode",
t!("Rule Mode", "规则模式"),
))
.add_item(CustomMenuItem::new(
"global_mode",
t!("Global Mode", "全局模式"),
))
.add_item(CustomMenuItem::new(
"direct_mode",
t!("Direct Mode", "直连模式"),
))
.add_item(CustomMenuItem::new(
"script_mode",
t!("Script Mode", "脚本模式"),
))
.add_item(CustomMenuItem::new("rule_mode", t!("tray.rule_mode")))
.add_item(CustomMenuItem::new("global_mode", t!("tray.global_mode")))
.add_item(CustomMenuItem::new("direct_mode", t!("tray.direct_mode")))
.add_item(CustomMenuItem::new("script_mode", t!("tray.script_mode")))
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(CustomMenuItem::new(
"system_proxy",
t!("System Proxy", "系统代理"),
))
.add_item(CustomMenuItem::new("tun_mode", t!("TUN Mode", "Tun 模式")))
.add_item(CustomMenuItem::new(
"copy_env_sh",
t!("Copy Env (sh)", "复制环境变量(sh)"),
))
.add_item(CustomMenuItem::new(
"copy_env_cmd",
t!("Copy Env (CMD)", "复制环境变量(CMD)"),
))
.add_item(CustomMenuItem::new(
"copy_env_ps",
t!("Copy Env (PS)", "复制环境变量(PS)"),
))
.add_item(CustomMenuItem::new("system_proxy", t!("tray.system_proxy")))
.add_item(CustomMenuItem::new("tun_mode", t!("tray.tun_mode")))
.add_item(CustomMenuItem::new("copy_env_sh", t!("tray.copy_env.sh")))
.add_item(CustomMenuItem::new("copy_env_cmd", t!("tray.copy_env.cmd")))
.add_item(CustomMenuItem::new("copy_env_ps", t!("tray.copy_env.ps")))
.add_submenu(SystemTraySubmenu::new(
t!("Open Dir", "打开目录"),
t!("tray.open_dir.menu"),
SystemTrayMenu::new()
.add_item(CustomMenuItem::new(
"open_app_dir",
t!("App Dir", "应用目录"),
t!("tray.open_dir.app_dir"),
))
.add_item(CustomMenuItem::new(
"open_core_dir",
t!("Core Dir", "内核目录"),
t!("tray.open_dir.core_dir"),
))
.add_item(CustomMenuItem::new(
"open_logs_dir",
t!("Logs Dir", "日志目录"),
t!("tray.open_dir.log_dir"),
)),
))
.add_submenu(SystemTraySubmenu::new(
t!("More", "更多"),
t!("tray.more.menu"),
SystemTrayMenu::new()
.add_item(CustomMenuItem::new(
"restart_clash",
t!("Restart Clash", "重启 Clash"),
t!("tray.more.restart_clash"),
))
.add_item(CustomMenuItem::new(
"restart_app",
t!("Restart App", "重启应用"),
t!("tray.more.restart_app"),
))
.add_item(
CustomMenuItem::new("app_version", format!("Version {version}")).disabled(),
),
))
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(CustomMenuItem::new("quit", t!("Quit", "退出")).accelerator("CmdOrControl+Q"))
.add_item(CustomMenuItem::new("quit", t!("tray.quit")).accelerator("CmdOrControl+Q"))
}

pub fn update_systray(app_handle: &AppHandle) -> Result<()> {
Expand Down Expand Up @@ -152,28 +116,19 @@ impl Tray {
#[cfg(not(target_os = "linux"))]
{
let zh = { verge.language == Some("zh".into()) };
macro_rules! t {
($en: expr, $zh: expr) => {
if zh {
$zh
} else {
$en
}
};
}

let switch_map = {
let mut map = std::collections::HashMap::new();
map.insert(true, t!("On", "开"));
map.insert(false, t!("Off", "关"));
map.insert(true, t!("tray.proxy_action.on"));
map.insert(false, t!("tray.proxy_action.off"));
map
};

let _ = tray.set_tooltip(&format!(
"{}: {}\n{}: {}",
t!("System Proxy", "系统代理"),
t!("tray.system_proxy"),
switch_map[system_proxy],
t!("TUN Mode", "Tun 模式"),
t!("tray.tun_mode"),
switch_map[tun_mode]
));
}
Expand Down
1 change: 1 addition & 0 deletions backend/tauri/src/feat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ pub async fn patch_verge(patch: IVerge) -> Result<()> {
}

if language.is_some() {
rust_i18n::set_locale(language.unwrap().as_str());
handle::Handle::update_systray()?;
} else if system_proxy.or(tun_mode).is_some() {
handle::Handle::update_systray_part()?;
Expand Down
6 changes: 6 additions & 0 deletions backend/tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ mod enhance;
mod feat;
mod utils;

use crate::config::Config;
use crate::utils::{init, resolve, server};
use tauri::{api, SystemTray};

rust_i18n::i18n!("../../locales");

fn main() -> std::io::Result<()> {
// 单例检测
if server::check_singleton().is_err() {
Expand All @@ -22,6 +25,9 @@ fn main() -> std::io::Result<()> {

crate::log_err!(init::init_config());

let verge = { Config::verge().latest().language.clone().unwrap() };
rust_i18n::set_locale(verge.as_str());

// Panic Hook to show a panic dialog and save logs
let default_panic = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
Expand Down
8 changes: 3 additions & 5 deletions backend/tauri/src/utils/dialog.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use rust_i18n::t;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Barrier};
use tauri::api::dialog::{MessageDialogBuilder, MessageDialogButtons, MessageDialogKind};

pub fn panic_dialog(msg: &str) {
let msg = format!(
"{}\n\nPlease report this issue to Github issue tracker.",
msg
);
let msg = format!("{}\n\n{}", msg, t!("dialog.panic"));
let barrier = Arc::new(Barrier::new(2));
let barrier_ref = barrier.clone();
MessageDialogBuilder::new("Error", msg)
Expand All @@ -19,7 +17,7 @@ pub fn panic_dialog(msg: &str) {
}

pub fn migrate_dialog() -> bool {
let msg = "Old version config file detected\nMigrate to new version or not?\n WARNING: This will override your current config if exists".to_string();
let msg = format!("{}", t!("dialog.migrate"));
let barrier = Arc::new(Barrier::new(2));
let barrier_ref = barrier.clone();
let migrate = Arc::new(AtomicBool::new(false));
Expand Down

0 comments on commit f7296db

Please sign in to comment.