From 5e0c28438b8aeba78cbf484c54a6140ccde1d4c9 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sat, 23 Nov 2024 14:41:19 +0800 Subject: [PATCH] Refactor Localization for AmazTool --- v2rayN/AmazTool/AmazTool.csproj | 14 +- v2rayN/AmazTool/Assets/en-US.json | 14 -- v2rayN/AmazTool/Assets/zh-CN.json | 14 -- v2rayN/AmazTool/LocalizationHelper.cs | 59 ------- v2rayN/AmazTool/Program.cs | 2 +- v2rayN/AmazTool/Resx/Resource.Designer.cs | 171 +++++++++++++++++++++ v2rayN/AmazTool/Resx/Resource.resx | 156 +++++++++++++++++++ v2rayN/AmazTool/Resx/Resource.zh-Hans.resx | 156 +++++++++++++++++++ v2rayN/AmazTool/UpgradeApp.cs | 16 +- 9 files changed, 504 insertions(+), 98 deletions(-) delete mode 100644 v2rayN/AmazTool/Assets/en-US.json delete mode 100644 v2rayN/AmazTool/Assets/zh-CN.json delete mode 100644 v2rayN/AmazTool/LocalizationHelper.cs create mode 100644 v2rayN/AmazTool/Resx/Resource.Designer.cs create mode 100644 v2rayN/AmazTool/Resx/Resource.resx create mode 100644 v2rayN/AmazTool/Resx/Resource.zh-Hans.resx diff --git a/v2rayN/AmazTool/AmazTool.csproj b/v2rayN/AmazTool/AmazTool.csproj index 05874af3..74fbff7f 100644 --- a/v2rayN/AmazTool/AmazTool.csproj +++ b/v2rayN/AmazTool/AmazTool.csproj @@ -10,8 +10,18 @@ - - + + True + True + Resource.resx + + + + + + ResXFileCodeGenerator + Resource.Designer.cs + \ No newline at end of file diff --git a/v2rayN/AmazTool/Assets/en-US.json b/v2rayN/AmazTool/Assets/en-US.json deleted file mode 100644 index 8b94e7cb..00000000 --- a/v2rayN/AmazTool/Assets/en-US.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "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/Assets/zh-CN.json b/v2rayN/AmazTool/Assets/zh-CN.json deleted file mode 100644 index dd57bcdf..00000000 --- a/v2rayN/AmazTool/Assets/zh-CN.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "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 diff --git a/v2rayN/AmazTool/LocalizationHelper.cs b/v2rayN/AmazTool/LocalizationHelper.cs deleted file mode 100644 index f10b8e33..00000000 --- a/v2rayN/AmazTool/LocalizationHelper.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System.Globalization; -using System.Reflection; -using System.Text.Json; - -namespace AmazTool -{ - public class LocalizationHelper - { - private static Dictionary _languageResources = []; - - static LocalizationHelper() - { - // 加载语言资源 - LoadLanguageResources(); - } - - private static void LoadLanguageResources() - { - try - { - var currentLanguage = CultureInfo.CurrentCulture.Name; - if (currentLanguage != "zh-CN" && currentLanguage != "en-US") - { - currentLanguage = "en-US"; - } - - var resourceName = $"AmazTool.Assets.{currentLanguage}.json"; - var assembly = Assembly.GetExecutingAssembly(); - - using var stream = assembly.GetManifestResourceStream(resourceName); - if (stream == null) return; - - using StreamReader reader = new(stream); - var json = reader.ReadToEnd(); - if (!string.IsNullOrEmpty(json)) - { - _languageResources = JsonSerializer.Deserialize>(json) ?? new Dictionary(); - } - } - catch (IOException ex) - { - Console.WriteLine($"Failed to read language resource file: {ex.Message}"); - } - catch (JsonException ex) - { - Console.WriteLine($"Failed to parse JSON data: {ex.Message}"); - } - catch (Exception ex) - { - Console.WriteLine($"Unexpected error occurred: {ex.Message}"); - } - } - - public static string GetLocalizedValue(string key) - { - return _languageResources.TryGetValue(key, out var translation) ? translation : key; - } - } -} \ No newline at end of file diff --git a/v2rayN/AmazTool/Program.cs b/v2rayN/AmazTool/Program.cs index 11e6aefc..f81f24be 100644 --- a/v2rayN/AmazTool/Program.cs +++ b/v2rayN/AmazTool/Program.cs @@ -10,7 +10,7 @@ { if (args.Length == 0) { - Console.WriteLine(LocalizationHelper.GetLocalizedValue("Guidelines")); + Console.WriteLine(Resx.Resource.Guidelines); Thread.Sleep(5000); return; } diff --git a/v2rayN/AmazTool/Resx/Resource.Designer.cs b/v2rayN/AmazTool/Resx/Resource.Designer.cs new file mode 100644 index 00000000..e13c70b6 --- /dev/null +++ b/v2rayN/AmazTool/Resx/Resource.Designer.cs @@ -0,0 +1,171 @@ +//------------------------------------------------------------------------------ +// +// 此代码由工具生成。 +// 运行时版本:4.0.30319.42000 +// +// 对此文件的更改可能会导致不正确的行为,并且如果 +// 重新生成代码,这些更改将会丢失。 +// +//------------------------------------------------------------------------------ + +namespace AmazTool.Resx { + using System; + + + /// + /// 一个强类型的资源类,用于查找本地化的字符串等。 + /// + // 此类是由 StronglyTypedResourceBuilder + // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 + // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen + // (以 /str 作为命令选项),或重新生成 VS 项目。 + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resource { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resource() { + } + + /// + /// 返回此类使用的缓存的 ResourceManager 实例。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("AmazTool.Resx.Resource", typeof(Resource).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// 重写当前线程的 CurrentUICulture 属性,对 + /// 使用此强类型资源类的所有资源查找执行重写。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// 查找类似 Failed to terminate the v2rayN.Close it manually,or the upgrade may fail. 的本地化字符串。 + /// + internal static string FailedTerminateProcess { + get { + return ResourceManager.GetString("FailedTerminateProcess", resourceCulture); + } + } + + /// + /// 查找类似 Failed to extract the update package. 的本地化字符串。 + /// + internal static string FailedUnzipping { + get { + return ResourceManager.GetString("FailedUnzipping", resourceCulture); + } + } + + /// + /// 查找类似 Upgrade failed. 的本地化字符串。 + /// + internal static string FailedUpgrade { + get { + return ResourceManager.GetString("FailedUpgrade", resourceCulture); + } + } + + /// + /// 查找类似 Please run it from the main application. 的本地化字符串。 + /// + internal static string Guidelines { + get { + return ResourceManager.GetString("Guidelines", resourceCulture); + } + } + + /// + /// 查找类似 Information 的本地化字符串。 + /// + internal static string Information { + get { + return ResourceManager.GetString("Information", resourceCulture); + } + } + + /// + /// 查找类似 In progress, please wait... 的本地化字符串。 + /// + internal static string InProgress { + get { + return ResourceManager.GetString("InProgress", resourceCulture); + } + } + + /// + /// 查找类似 Start v2rayN, please wait... 的本地化字符串。 + /// + internal static string Restartv2rayN { + get { + return ResourceManager.GetString("Restartv2rayN", resourceCulture); + } + } + + /// + /// 查找类似 Start extracting the update package... 的本地化字符串。 + /// + internal static string StartUnzipping { + get { + return ResourceManager.GetString("StartUnzipping", resourceCulture); + } + } + + /// + /// 查找类似 Successfully extracted the update package. 的本地化字符串。 + /// + internal static string SuccessUnzipping { + get { + return ResourceManager.GetString("SuccessUnzipping", resourceCulture); + } + } + + /// + /// 查找类似 Upgrade success. 的本地化字符串。 + /// + internal static string SuccessUpgrade { + get { + return ResourceManager.GetString("SuccessUpgrade", resourceCulture); + } + } + + /// + /// 查找类似 Try to terminate the v2rayN process... 的本地化字符串。 + /// + internal static string TryTerminateProcess { + get { + return ResourceManager.GetString("TryTerminateProcess", resourceCulture); + } + } + + /// + /// 查找类似 Upgrade failed, file not found. 的本地化字符串。 + /// + internal static string UpgradeFileNotFound { + get { + return ResourceManager.GetString("UpgradeFileNotFound", resourceCulture); + } + } + } +} diff --git a/v2rayN/AmazTool/Resx/Resource.resx b/v2rayN/AmazTool/Resx/Resource.resx new file mode 100644 index 00000000..b2cfc113 --- /dev/null +++ b/v2rayN/AmazTool/Resx/Resource.resx @@ -0,0 +1,156 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Start v2rayN, please wait... + + + Please run it from the main application. + + + Upgrade failed, file not found. + + + In progress, please wait... + + + Try to terminate the v2rayN process... + + + Failed to terminate the v2rayN.Close it manually,or the upgrade may fail. + + + Start extracting the update package... + + + Successfully extracted the update package. + + + Failed to extract the update package. + + + Upgrade failed. + + + Upgrade success. + + + Information + + \ No newline at end of file diff --git a/v2rayN/AmazTool/Resx/Resource.zh-Hans.resx b/v2rayN/AmazTool/Resx/Resource.zh-Hans.resx new file mode 100644 index 00000000..062cab7a --- /dev/null +++ b/v2rayN/AmazTool/Resx/Resource.zh-Hans.resx @@ -0,0 +1,156 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 正在重启,请等待... + + + 请从主应用运行。 + + + 升级失败,文件不存在。 + + + 正在进行中,请等待... + + + 尝试结束 v2rayN 进程... + + + 请手动关闭正在运行的v2rayN,否则可能升级失败。 + + + 开始解压缩更新包... + + + 解压缩更新包成功。 + + + 解压缩更新包失败。 + + + 升级失败。 + + + 升级成功。 + + + 提示 + + \ No newline at end of file diff --git a/v2rayN/AmazTool/UpgradeApp.cs b/v2rayN/AmazTool/UpgradeApp.cs index 06484950..dc517ee9 100644 --- a/v2rayN/AmazTool/UpgradeApp.cs +++ b/v2rayN/AmazTool/UpgradeApp.cs @@ -8,17 +8,17 @@ namespace AmazTool { public static void Upgrade(string fileName) { - Console.WriteLine($"{LocalizationHelper.GetLocalizedValue("Start_Unzipping")}\n{fileName}"); + Console.WriteLine($"{Resx.Resource.StartUnzipping}\n{fileName}"); Waiting(9); if (!File.Exists(fileName)) { - Console.WriteLine(LocalizationHelper.GetLocalizedValue("Upgrade_File_Not_Found")); + Console.WriteLine(Resx.Resource.UpgradeFileNotFound); return; } - Console.WriteLine(LocalizationHelper.GetLocalizedValue("Try_Terminate_Process")); + Console.WriteLine(Resx.Resource.TryTerminateProcess); try { var existing = Process.GetProcessesByName(V2rayN); @@ -35,10 +35,10 @@ namespace AmazTool catch (Exception ex) { // Access may be denied without admin right. The user may not be an administrator. - Console.WriteLine(LocalizationHelper.GetLocalizedValue("Failed_Terminate_Process") + ex.StackTrace); + Console.WriteLine(Resx.Resource.FailedTerminateProcess + ex.StackTrace); } - Console.WriteLine(LocalizationHelper.GetLocalizedValue("Start_Unzipping")); + Console.WriteLine(Resx.Resource.StartUnzipping); StringBuilder sb = new(); try { @@ -81,16 +81,16 @@ namespace AmazTool } catch (Exception ex) { - Console.WriteLine(LocalizationHelper.GetLocalizedValue("Failed_Upgrade") + ex.StackTrace); + Console.WriteLine(Resx.Resource.FailedUpgrade + ex.StackTrace); //return; } if (sb.Length > 0) { - Console.WriteLine(LocalizationHelper.GetLocalizedValue("Failed_Upgrade") + sb.ToString()); + Console.WriteLine(Resx.Resource.FailedUpgrade + sb.ToString()); //return; } - Console.WriteLine(LocalizationHelper.GetLocalizedValue("Restart_v2rayN")); + Console.WriteLine(Resx.Resource.Restartv2rayN); Waiting(9); Process process = new() {