fix: add system language sup

This commit is contained in:
blagodaren
2024-12-28 08:12:46 +03:00
parent ae5b2cfb79
commit fc925ea032
9 changed files with 554 additions and 55 deletions

12
src-tauri/Cargo.lock generated
View File

@@ -1,6 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
version = 4
[[package]]
name = "addr2line"
@@ -1074,6 +1074,7 @@ dependencies = [
"serde",
"serde_json",
"serde_yaml",
"sys-locale",
"sysinfo",
"sysproxy",
"tauri",
@@ -6373,6 +6374,15 @@ dependencies = [
"syn 2.0.87",
]
[[package]]
name = "sys-locale"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8eab9a99a024a169fe8a903cf9d4a3b3601109bcc13bd9e3c6fff259138626c4"
dependencies = [
"libc",
]
[[package]]
name = "sysinfo"
version = "0.32.0"

View File

@@ -59,6 +59,7 @@ reqwest_dav = "0.1.14"
aes-gcm = { version = "0.10.3", features = ["std"] }
base64 = "0.22.1"
getrandom = "0.2"
sys-locale = "0.3.1"
[target.'cfg(windows)'.dependencies]
runas = "=1.2.0"

View File

@@ -1,6 +1,7 @@
use crate::config::DEFAULT_PAC;
use crate::config::{deserialize_encrypted, serialize_encrypted};
use crate::utils::{dirs, help};
use crate::utils::i18n;
use anyhow::Result;
use log::LevelFilter;
use serde::{Deserialize, Serialize};
@@ -202,6 +203,21 @@ pub struct IVergeTheme {
}
impl IVerge {
fn get_system_language() -> String {
let sys_lang = sys_locale::get_locale()
.unwrap_or_else(|| String::from("en"))
.to_lowercase();
let lang_code = sys_lang.split(['_', '-']).next().unwrap_or("en");
let supported_languages = i18n::get_supported_languages();
if supported_languages.contains(&lang_code.to_string()) {
lang_code.to_string()
} else {
String::from("en")
}
}
pub fn new() -> Self {
match dirs::verge_path().and_then(|path| help::read_yaml::<IVerge>(&path)) {
Ok(config) => config,
@@ -215,7 +231,7 @@ impl IVerge {
pub fn template() -> Self {
Self {
clash_core: Some("verge-mihomo".into()),
language: Some("zh".into()),
language: Some(Self::get_system_language()),
theme_mode: Some("system".into()),
#[cfg(not(target_os = "windows"))]
env_type: Some("bash".into()),

View File

@@ -1,12 +1,14 @@
use crate::{
cmds,
config::Config,
feat, t,
feat,
utils::{
dirs,
resolve::{self, VERSION},
i18n::t,
},
};
use anyhow::Result;
use tauri::AppHandle;
use tauri::{
@@ -67,7 +69,6 @@ impl Tray {
pub fn update_part() -> Result<()> {
let app_handle = handle::Handle::global().app_handle().unwrap();
let use_zh = { Config::verge().latest().language == Some("zh".into()) };
let version = VERSION.get().unwrap();
let mode = {
Config::clash()
@@ -190,11 +191,11 @@ impl Tray {
let _ = tray.set_tooltip(Some(&format!(
"Clash Verge {version}\n{}: {}\n{}: {}\n{}: {}",
t!("SysProxy", "系统代理", use_zh),
t("SysProxy"),
switch_map[system_proxy],
t!("TUN", "Tun模式", use_zh),
t("TUN"),
switch_map[tun_mode],
t!("Profile", "当前订阅", use_zh),
t("Profile"),
current_profile_name
)));
Ok(())
@@ -208,129 +209,116 @@ fn create_tray_menu(
tun_mode_enabled: bool,
) -> Result<tauri::menu::Menu<Wry>> {
let mode = mode.unwrap_or("");
let use_zh = { Config::verge().latest().language == Some("zh".into()) };
let version = VERSION.get().unwrap();
let open_window = &MenuItem::with_id(
app_handle,
"open_window",
t!("Dashboard", "打开面板", use_zh),
t("Dashboard"),
true,
None::<&str>,
)
.unwrap();
).unwrap();
let rule_mode = &CheckMenuItem::with_id(
app_handle,
"rule_mode",
t!("Rule Mode", "规则模式", use_zh),
t("Rule Mode"),
true,
mode == "rule",
None::<&str>,
)
.unwrap();
).unwrap();
let global_mode = &CheckMenuItem::with_id(
app_handle,
"global_mode",
t!("Global Mode", "全局模式", use_zh),
t("Global Mode"),
true,
mode == "global",
None::<&str>,
)
.unwrap();
).unwrap();
let direct_mode = &CheckMenuItem::with_id(
app_handle,
"direct_mode",
t!("Direct Mode", "直连模式", use_zh),
t("Direct Mode"),
true,
mode == "direct",
None::<&str>,
)
.unwrap();
).unwrap();
let system_proxy = &CheckMenuItem::with_id(
app_handle,
"system_proxy",
t!("System Proxy", "系统代理", use_zh),
t("System Proxy"),
true,
system_proxy_enabled,
None::<&str>,
)
.unwrap();
).unwrap();
let tun_mode = &CheckMenuItem::with_id(
app_handle,
"tun_mode",
t!("TUN Mode", "Tun模式", use_zh),
t("TUN Mode"),
true,
tun_mode_enabled,
None::<&str>,
)
.unwrap();
).unwrap();
let copy_env = &MenuItem::with_id(
app_handle,
"copy_env",
t!("Copy Env", "复制环境变量", use_zh),
t("Copy Env"),
true,
None::<&str>,
)
.unwrap();
).unwrap();
let open_app_dir = &MenuItem::with_id(
app_handle,
"open_app_dir",
t!("Conf Dir", "配置目录", use_zh),
t("Conf Dir"),
true,
None::<&str>,
)
.unwrap();
).unwrap();
let open_core_dir = &MenuItem::with_id(
app_handle,
"open_core_dir",
t!("Core Dir", "内核目录", use_zh),
t("Core Dir"),
true,
None::<&str>,
)
.unwrap();
).unwrap();
let open_logs_dir = &MenuItem::with_id(
app_handle,
"open_logs_dir",
t!("Logs Dir", "日志目录", use_zh),
t("Logs Dir"),
true,
None::<&str>,
)
.unwrap();
).unwrap();
let open_dir = &Submenu::with_id_and_items(
app_handle,
"open_dir",
t!("Open Dir", "打开目录", use_zh),
t("Open Dir"),
true,
&[open_app_dir, open_core_dir, open_logs_dir],
)
.unwrap();
).unwrap();
let restart_clash = &MenuItem::with_id(
app_handle,
"restart_clash",
t!("Restart Clash Core", "重启Clash内核", use_zh),
t("Restart Clash Core"),
true,
None::<&str>,
)
.unwrap();
).unwrap();
let restart_app = &MenuItem::with_id(
app_handle,
"restart_app",
t!("Restart App", "重启App", use_zh),
t("Restart App"),
true,
None::<&str>,
)
.unwrap();
).unwrap();
let app_version = &MenuItem::with_id(
app_handle,
@@ -338,26 +326,23 @@ fn create_tray_menu(
format!("Version {version}"),
true,
None::<&str>,
)
.unwrap();
).unwrap();
let more = &Submenu::with_id_and_items(
app_handle,
"more",
t!("More", "更多", use_zh),
t("More"),
true,
&[restart_clash, restart_app, app_version],
)
.unwrap();
).unwrap();
let quit = &MenuItem::with_id(
app_handle,
"quit",
t!("Quit", "退出", use_zh),
t("Quit"),
true,
Some("CmdOrControl+Q"),
)
.unwrap();
).unwrap();
let separator = &PredefinedMenuItem::separator(app_handle).unwrap();

View File

@@ -0,0 +1,59 @@
use std::{fs, path::Path};
use crate::config::Config;
use sys_locale;
pub fn get_supported_languages() -> Vec<String> {
let project_dir = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
let i18n_path = project_dir.join("src/services/i18n.ts");
if let Ok(content) = fs::read_to_string(i18n_path) {
let mut languages = Vec::new();
for line in content.lines() {
if line.contains("resources = {") {
for line in content.lines() {
if let Some(lang) = line.trim().strip_suffix(": { translation:") {
let lang = lang.trim().trim_matches('"');
if !lang.is_empty() {
languages.push(lang.to_string());
}
}
if line.contains("};") {
break;
}
}
break;
}
}
if !languages.is_empty() {
return languages;
}
}
vec!["en".to_string(), "ru".to_string(), "zh".to_string(), "fa".to_string()]
}
pub fn t(text: &str) -> String {
let config = Config::verge();
let verge = config.latest();
let current_lang = verge.language.as_ref().map_or_else(
|| get_system_language(),
|lang| lang.to_string()
);
text.to_string()
}
fn get_system_language() -> String {
let sys_lang = sys_locale::get_locale()
.unwrap_or_else(|| String::from("en"))
.to_lowercase();
let lang_code = sys_lang.split(['_', '-']).next().unwrap_or("en");
let supported_languages = get_supported_languages();
if supported_languages.contains(&lang_code.to_string()) {
lang_code.to_string()
} else {
String::from("en")
}
}

View File

@@ -5,3 +5,4 @@ pub mod init;
pub mod resolve;
pub mod server;
pub mod tmpl;
pub mod i18n;