From 2e32de2fbb8d5042266a68f235e9505efd9ae2ca Mon Sep 17 00:00:00 2001 From: Slnanx Date: Sun, 17 Nov 2024 19:42:14 +0800 Subject: [PATCH] fix (#6099) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix * fix * 移除自述 * 改用单例模式,避免多次初始化。 * 小修正 * 修正 * 更正自述 * 应该完成了 --- v2rayN/AmazTool/LocalizationHelper.cs | 148 +++++++------------------- v2rayN/AmazTool/Program.cs | 5 +- v2rayN/AmazTool/UpgradeApp.cs | 9 +- v2rayN/AmazTool/en-US.json | 14 +++ v2rayN/AmazTool/zh-CN.json | 14 +++ 5 files changed, 72 insertions(+), 118 deletions(-) create mode 100644 v2rayN/AmazTool/en-US.json create mode 100644 v2rayN/AmazTool/zh-CN.json diff --git a/v2rayN/AmazTool/LocalizationHelper.cs b/v2rayN/AmazTool/LocalizationHelper.cs index 116fe465..3804564c 100644 --- a/v2rayN/AmazTool/LocalizationHelper.cs +++ b/v2rayN/AmazTool/LocalizationHelper.cs @@ -1,131 +1,63 @@ -using System.Collections.Generic; -using System.Globalization; +using System.Globalization; +using System.Text.Json; namespace AmazTool { public class LocalizationHelper { - /// - /// 获取系统当前语言的本地化字符串 - /// - /// 要翻译的关键字 - /// 对应语言的本地化字符串,如果没有找到则返回关键字 - public static string GetLocalizedValue(string key) - { - // 定义支持的语言 - HashSet supportedLanguages = ["zh", "en"]; - - // 获取当前系统语言的 ISO 两字母代码 - string currentLanguage = CultureInfo.CurrentCulture.TwoLetterISOLanguageName; - - // 如果当前语言不在支持的语言列表中,默认使用英文 - if (!supportedLanguages.Contains(currentLanguage)) - { - currentLanguage = "en"; - } - - // 尝试获取对应语言的翻译 - if (languageResources.TryGetValue(key, out var translations)) - { - if (translations.TryGetValue(currentLanguage, out var translation)) - { - return translation; - } - } + private static Dictionary languageResources = new(); - // 如果未找到翻译,返回关键字本身 - return key; + static LocalizationHelper() + { + // 加载语言资源 + LoadLanguageResources(); } /// - /// 存储不同语言的本地化资源 + /// 加载外部 JSON 文件中的语言资源 /// - public static Dictionary> languageResources = new() + private static void LoadLanguageResources() { + try { - "Guidelines", new Dictionary - { - { "en", "Please run it from the main application." }, - { "zh", "请从主应用运行!" } - } - }, - { - "Upgrade_File_Not_Found", new Dictionary - { - { "en", "Upgrade failed, file not found." }, - { "zh", "升级失败,文件不存在!" } - } - }, - { - "In_Progress", new Dictionary + string currentLanguage = CultureInfo.CurrentCulture.Name; + if (currentLanguage != "zh-CN" && currentLanguage != "en-US") { - { "en", "In progress, please wait..." }, - { "zh", "正在进行中,请等待..." } + currentLanguage = "en-US"; } - }, - { - "Try_Terminate_Process", new Dictionary - { - { "en", "Try to terminate the v2rayN process." }, - { "zh", "尝试结束 v2rayN 进程..." } - } - }, - { - "Failed_Terminate_Process", new Dictionary - { - { "en", "Failed to terminate the v2rayN.Close it manually,or the upgrade may fail." }, - { "zh", "请手动关闭正在运行的v2rayN,否则可能升级失败。" } - } - }, - { - "Start_Unzipping", new Dictionary - { - { "en", "Start extracting the update package." }, - { "zh", "开始解压缩更新包..." } - } - }, - { - "Success_Unzipping", new Dictionary - { - { "en", "Successfully extracted the update package!" }, - { "zh", "解压缩更新包成功!" } - } - }, - { - "Failed_Unzipping", new Dictionary - { - { "en", "Failed to extract the update package!" }, - { "zh", "解压缩更新包失败!" } - } - }, - { - "Failed_Upgrade", new Dictionary + + string jsonFilePath = $"{currentLanguage}.json"; + if (!File.Exists(jsonFilePath)) { - { "en", "Upgrade failed!" }, - { "zh", "升级失败!" } + jsonFilePath = "en-US.json"; } - }, - { - "Success_Upgrade", new Dictionary + + var json = File.ReadAllText(jsonFilePath); + if (!string.IsNullOrEmpty(json)) { - { "en", "Upgrade success!" }, - { "zh", "升级成功!" } + languageResources = JsonSerializer.Deserialize>(json) ?? new Dictionary(); } - }, + } + catch (Exception ex) { - "Information", new Dictionary - { - { "en", "Information" }, - { "zh", "提示" } - } - }, + Console.WriteLine($"Failed to load language resources: {ex.Message}"); + languageResources = []; // 初始化为空字典 + } + } + + /// + /// 获取系统当前语言的本地化字符串 + /// + /// 要翻译的关键字 + /// 对应语言的本地化字符串,如果没有找到则返回关键字 + public static string GetLocalizedValue(string key) + { + if (languageResources != null && languageResources.TryGetValue(key, out var translation)) { - "Restart_v2rayN", new Dictionary - { - { "en", "Start v2rayN, please wait..." }, - { "zh", "正在重启,请等待..." } - } + return translation; } - }; + + return key; + } } } diff --git a/v2rayN/AmazTool/Program.cs b/v2rayN/AmazTool/Program.cs index 74725033..11e6aefc 100644 --- a/v2rayN/AmazTool/Program.cs +++ b/v2rayN/AmazTool/Program.cs @@ -1,7 +1,4 @@ -using System; -using System.Threading; - -namespace AmazTool +namespace AmazTool { internal static class Program { diff --git a/v2rayN/AmazTool/UpgradeApp.cs b/v2rayN/AmazTool/UpgradeApp.cs index afab6cba..98239961 100644 --- a/v2rayN/AmazTool/UpgradeApp.cs +++ b/v2rayN/AmazTool/UpgradeApp.cs @@ -1,9 +1,6 @@ -using System; -using System.Diagnostics; -using System.IO; +using System.Diagnostics; using System.IO.Compression; using System.Text; -using System.Threading; namespace AmazTool { @@ -11,8 +8,8 @@ namespace AmazTool { public static void Upgrade(string fileName) { - Console.WriteLine(fileName); - + Console.WriteLine($"{LocalizationHelper.GetLocalizedValue("Start_Unzipping")}\n{fileName}"); + Thread.Sleep(9000); if (!File.Exists(fileName)) diff --git a/v2rayN/AmazTool/en-US.json b/v2rayN/AmazTool/en-US.json new file mode 100644 index 00000000..f4c0f600 --- /dev/null +++ b/v2rayN/AmazTool/en-US.json @@ -0,0 +1,14 @@ +{ + "Restart_v2rayN": "Start v2rayN, please wait...", + "Guidelines": "Please run it from the main application.", + "Upgrade_File_Not_Found": "Upgrade failed, file not found.", + "In_Progress": "In progress, please wait...", + "Try_Terminate_Process": "Try to terminate the v2rayN process.", + "Failed_Terminate_Process": "Failed to terminate the v2rayN.Close it manually,or the upgrade may fail.", + "Start_Unzipping": "Start extracting the update package.", + "Success_Unzipping": "Successfully extracted the update package!", + "Failed_Unzipping": "Failed to extract the update package!", + "Failed_Upgrade": "Upgrade failed!", + "Success_Upgrade": "Upgrade success!", + "Information": "Information" +} \ No newline at end of file diff --git a/v2rayN/AmazTool/zh-CN.json b/v2rayN/AmazTool/zh-CN.json new file mode 100644 index 00000000..a6b6a1b7 --- /dev/null +++ b/v2rayN/AmazTool/zh-CN.json @@ -0,0 +1,14 @@ +{ + "Restart_v2rayN": "正在重启,请等待...", + "Guidelines": "请从主应用运行!", + "Upgrade_File_Not_Found": "升级失败,文件不存在!", + "In_Progress": "正在进行中,请等待...", + "Try_Terminate_Process": "尝试结束 v2rayN 进程...", + "Failed_Terminate_Process": "请手动关闭正在运行的v2rayN,否则可能升级失败。", + "Start_Unzipping": "开始解压缩更新包...", + "Success_Unzipping": "解压缩更新包成功!", + "Failed_Unzipping": "解压缩更新包失败!", + "Failed_Upgrade": "升级失败!", + "Success_Upgrade": "升级成功!", + "Information": "提示" +} \ No newline at end of file