mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-05 06:43:44 +08:00
fix: fix: try to fix the language pack issue(2)
This commit is contained in:
parent
a39696151d
commit
41a27641df
@ -398,6 +398,34 @@ const resolveServicePermission = async () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 在 resolveResource 函数后添加新函数
|
||||||
|
async function resolveLocales() {
|
||||||
|
const srcLocalesDir = path.join(cwd, "src/locales");
|
||||||
|
const targetLocalesDir = path.join(cwd, "src-tauri/resources/locales");
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 确保目标目录存在
|
||||||
|
await fsp.mkdir(targetLocalesDir, { recursive: true });
|
||||||
|
|
||||||
|
// 读取所有语言文件
|
||||||
|
const files = await fsp.readdir(srcLocalesDir);
|
||||||
|
|
||||||
|
// 复制每个文件
|
||||||
|
for (const file of files) {
|
||||||
|
const srcPath = path.join(srcLocalesDir, file);
|
||||||
|
const targetPath = path.join(targetLocalesDir, file);
|
||||||
|
|
||||||
|
await fsp.copyFile(srcPath, targetPath);
|
||||||
|
log_success(`Copied locale file: ${file}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
log_success("All locale files copied successfully");
|
||||||
|
} catch (err) {
|
||||||
|
log_error("Error copying locale files:", err.message);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* main
|
* main
|
||||||
*/
|
*/
|
||||||
@ -509,6 +537,11 @@ const tasks = [
|
|||||||
retry: 5,
|
retry: 5,
|
||||||
macosOnly: true,
|
macosOnly: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "locales",
|
||||||
|
func: resolveLocales,
|
||||||
|
retry: 2,
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
async function runTask() {
|
async function runTask() {
|
||||||
|
@ -1,81 +1,88 @@
|
|||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
|
use crate::utils::dirs;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use std::{collections::HashMap, fs, path::Path};
|
use std::{collections::HashMap, fs, path::PathBuf};
|
||||||
use sys_locale;
|
use sys_locale;
|
||||||
|
|
||||||
|
const DEFAULT_LANGUAGE: &str = "zh";
|
||||||
|
|
||||||
|
fn get_locales_dir() -> Option<PathBuf> {
|
||||||
|
dirs::app_resources_dir()
|
||||||
|
.map(|resource_path| resource_path.join("locales"))
|
||||||
|
.ok()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_supported_languages() -> Vec<String> {
|
pub fn get_supported_languages() -> Vec<String> {
|
||||||
let project_dir = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
|
|
||||||
let locales_dir = project_dir.join("src/locales");
|
|
||||||
let mut languages = Vec::new();
|
let mut languages = Vec::new();
|
||||||
|
|
||||||
if let Ok(entries) = fs::read_dir(locales_dir) {
|
if let Some(locales_dir) = get_locales_dir() {
|
||||||
for entry in entries.flatten() {
|
if let Ok(entries) = fs::read_dir(locales_dir) {
|
||||||
if let Some(file_name) = entry.file_name().to_str() {
|
for entry in entries.flatten() {
|
||||||
if let Some(lang) = file_name.strip_suffix(".json") {
|
if let Some(file_name) = entry.file_name().to_str() {
|
||||||
languages.push(lang.to_string());
|
if let Some(lang) = file_name.strip_suffix(".json") {
|
||||||
|
languages.push(lang.to_string());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if languages.is_empty() {
|
||||||
|
languages.push(DEFAULT_LANGUAGE.to_string());
|
||||||
|
}
|
||||||
languages
|
languages
|
||||||
}
|
}
|
||||||
|
|
||||||
static TRANSLATIONS: Lazy<HashMap<String, Value>> = Lazy::new(|| {
|
static TRANSLATIONS: Lazy<HashMap<String, Value>> = Lazy::new(|| {
|
||||||
let project_dir = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
|
|
||||||
let locales_dir = project_dir.join("src/locales");
|
|
||||||
let mut translations = HashMap::new();
|
let mut translations = HashMap::new();
|
||||||
|
|
||||||
for lang in get_supported_languages() {
|
if let Some(locales_dir) = get_locales_dir() {
|
||||||
let file_path = locales_dir.join(format!("{}.json", lang));
|
for lang in get_supported_languages() {
|
||||||
if let Ok(content) = fs::read_to_string(file_path) {
|
let file_path = locales_dir.join(format!("{}.json", lang));
|
||||||
if let Ok(json) = serde_json::from_str(&content) {
|
if let Ok(content) = fs::read_to_string(file_path) {
|
||||||
translations.insert(lang.to_string(), json);
|
if let Ok(json) = serde_json::from_str(&content) {
|
||||||
|
translations.insert(lang.to_string(), json);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
translations
|
translations
|
||||||
});
|
});
|
||||||
|
|
||||||
pub fn t(key: &str) -> String {
|
fn get_system_language() -> String {
|
||||||
let config = Config::verge();
|
sys_locale::get_locale()
|
||||||
let verge = config.latest();
|
.map(|locale| locale.to_lowercase())
|
||||||
let current_lang = verge
|
.and_then(|locale| locale.split(['_', '-']).next().map(String::from))
|
||||||
.language
|
.filter(|lang| get_supported_languages().contains(lang))
|
||||||
.as_ref()
|
.unwrap_or_else(|| DEFAULT_LANGUAGE.to_string())
|
||||||
.map_or_else(|| get_system_language(), |lang| lang.to_string());
|
}
|
||||||
|
|
||||||
if let Some(translations) = TRANSLATIONS.get(¤t_lang) {
|
pub fn t(key: &str) -> String {
|
||||||
if let Some(text) = translations.get(key) {
|
let current_lang = Config::verge()
|
||||||
if let Some(text) = text.as_str() {
|
.latest()
|
||||||
return text.to_string();
|
.language
|
||||||
}
|
.as_deref()
|
||||||
}
|
.map(String::from)
|
||||||
|
.unwrap_or_else(get_system_language);
|
||||||
|
|
||||||
|
if let Some(text) = TRANSLATIONS
|
||||||
|
.get(¤t_lang)
|
||||||
|
.and_then(|trans| trans.get(key))
|
||||||
|
.and_then(|val| val.as_str())
|
||||||
|
{
|
||||||
|
return text.to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback to Chinese
|
if current_lang != DEFAULT_LANGUAGE {
|
||||||
if let Some(translations) = TRANSLATIONS.get("zh") {
|
if let Some(text) = TRANSLATIONS
|
||||||
if let Some(text) = translations.get(key) {
|
.get(DEFAULT_LANGUAGE)
|
||||||
if let Some(text) = text.as_str() {
|
.and_then(|trans| trans.get(key))
|
||||||
return text.to_string();
|
.and_then(|val| val.as_str())
|
||||||
}
|
{
|
||||||
|
return text.to_string();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
key.to_string()
|
key.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_system_language() -> String {
|
|
||||||
let sys_lang = sys_locale::get_locale()
|
|
||||||
.unwrap_or_else(|| String::from("zh"))
|
|
||||||
.to_lowercase();
|
|
||||||
|
|
||||||
let lang_code = sys_lang.split(['_', '-']).next().unwrap_or("zh");
|
|
||||||
let supported_languages = get_supported_languages();
|
|
||||||
|
|
||||||
if supported_languages.contains(&lang_code.to_string()) {
|
|
||||||
lang_code.to_string()
|
|
||||||
} else {
|
|
||||||
String::from("zh")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
"icons/icon.icns",
|
"icons/icon.icns",
|
||||||
"icons/icon.ico"
|
"icons/icon.ico"
|
||||||
],
|
],
|
||||||
"resources": ["resources"],
|
"resources": ["resources", "resources/locales/*"],
|
||||||
"publisher": "Clash Verge Rev",
|
"publisher": "Clash Verge Rev",
|
||||||
"externalBin": ["sidecar/verge-mihomo", "sidecar/verge-mihomo-alpha"],
|
"externalBin": ["sidecar/verge-mihomo", "sidecar/verge-mihomo-alpha"],
|
||||||
"copyright": "GNU General Public License v3.0",
|
"copyright": "GNU General Public License v3.0",
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
"identifier": "io.github.clash-verge-rev.clash-verge-rev",
|
"identifier": "io.github.clash-verge-rev.clash-verge-rev",
|
||||||
"bundle": {
|
"bundle": {
|
||||||
"targets": ["app", "dmg"],
|
"targets": ["app", "dmg"],
|
||||||
"resources": ["resources"],
|
|
||||||
"macOS": {
|
"macOS": {
|
||||||
"frameworks": [],
|
"frameworks": [],
|
||||||
"minimumSystemVersion": "10.15",
|
"minimumSystemVersion": "10.15",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user