This commit is contained in:
Slnanx 2024-11-17 10:03:27 +00:00
parent 887a5734ac
commit 8236fe1840
2 changed files with 95 additions and 18 deletions

View file

@ -7,14 +7,17 @@ using System.Threading;
public class Localization public class Localization
{ {
// 使用 Lazy<T> 实现单例模式,确保 Localization 实例在需要时才会创建
private static readonly Lazy<Localization> _instance = new(() => new Localization());
private Dictionary<string, string> translations; private Dictionary<string, string> translations;
// 私有构造函数,防止外部实例化
private Localization() private Localization()
{ {
// 获取当前系统的完整文化名称zh-CN en-US // 获取当前系统的完整文化名称例如zh-CN, en-US
string currentLanguage = CultureInfo.CurrentCulture.Name; string currentLanguage = CultureInfo.CurrentCulture.Name;
// 如果当前语言不是"zh-CN"或"en-US",默认使用英文 // 如果当前语言不是 "zh-CN" 或 "en-US",默认使用英语
if (currentLanguage != "zh-CN" && currentLanguage != "en-US") if (currentLanguage != "zh-CN" && currentLanguage != "en-US")
{ {
currentLanguage = "en-US"; currentLanguage = "en-US";
@ -24,24 +27,35 @@ public class Localization
string jsonFilePath = $"{currentLanguage}.json"; string jsonFilePath = $"{currentLanguage}.json";
if (!LoadTranslations(jsonFilePath)) if (!LoadTranslations(jsonFilePath))
{ {
// 如果加载失败,则使用默认语言 // 如果加载失败,则使用默认语言的 JSON 文件
jsonFilePath = "en-US.json"; jsonFilePath = "en-US.json";
LoadTranslations(jsonFilePath); LoadTranslations(jsonFilePath);
} }
} }
// 公有静态属性用于获取单例实例
public static Localization Instance
{
get
{
return _instance.Value;
}
}
// 加载翻译文件,并将内容解析为字典
private bool LoadTranslations(string jsonFilePath) private bool LoadTranslations(string jsonFilePath)
{ {
try try
{ {
// 读取 JSON 文件内容 // 读取 JSON 文件内容
var json = File.ReadAllText(jsonFilePath); var json = File.ReadAllText(jsonFilePath);
// 解析JSON内容 // 解析 JSON 内容并转换为字典
translations = JsonSerializer.Deserialize<Dictionary<string, string>>(json); translations = JsonSerializer.Deserialize<Dictionary<string, string>>(json);
return true; // 成功读取和解析JSON文件 return true; // 成功读取并解析 JSON 文件
} }
catch (Exception ex) catch (Exception ex)
{ {
// 处理读取或解析 JSON 文件时的异常
Console.WriteLine($"Failed to load JSON file: {ex.Message}"); Console.WriteLine($"Failed to load JSON file: {ex.Message}");
Thread.Sleep(5000); Thread.Sleep(5000);
Environment.Exit(1); Environment.Exit(1);
@ -49,6 +63,7 @@ public class Localization
} }
} }
// 根据键值返回相应的翻译,如果不存在则返回键值本身
public string Translate(string key) public string Translate(string key)
{ {
return translations != null && translations.TryGetValue(key, out string value) ? value : key; return translations != null && translations.TryGetValue(key, out string value) ? value : key;

View file

@ -1,21 +1,26 @@
/** /**
* 使JSON文件对C# * 使JSON文件对C#
* *
* 使 * 使
* *
* :
* - System.Collections.Generic
* - System.Globalization
* - System.IO
* - System.Text.Json
*
* : * :
* - JSON文件en-US.jsonzh-CN.json * - JSON文件en.jsonzh.json
* - JSON文件放置程序同目录中 * - JSON文件放置程序同目录中
* - * -
* - localization.Translate("Try_Terminate_Process") //返回一个 string 字符串 * - localization.Translate("Try_Terminate_Process") //返回一个 string 字符串
* * JSON文件en.json
* JSON文件en-US.json
* { * {
* "Restart_v2rayN": "Start v2rayN, please wait...", * "Restart_v2rayN": "Start v2rayN, please wait...",
* "Guidelines": "Please run it from the main application." * "Guidelines": "Please run it from the main application."
* } * }
* *
* JSON文件zh-CN.json * JSON文件zh.json
* { * {
* "Restart_v2rayN": "正在重启,请等待...", * "Restart_v2rayN": "正在重启,请等待...",
* "Guidelines": "请从主应用运行!" * "Guidelines": "请从主应用运行!"
@ -23,19 +28,76 @@
*/ */
using System; using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text.Json;
using System.Threading; using System.Threading;
namespace AmazTool namespace AmazTool
{ {
public class Localization
{
private static readonly Lazy<Localization> _instance = new Lazy<Localization>(() => new Localization());
private Dictionary<string, string> translations;
// 私有构造函数,防止外部实例化
private Localization()
{
string currentLanguage = CultureInfo.CurrentCulture.Name;
if (currentLanguage != "zh-CN" && currentLanguage != "en-US")
{
currentLanguage = "en-US";
}
string jsonFilePath = $"{currentLanguage}.json";
if (!LoadTranslations(jsonFilePath))
{
jsonFilePath = "en-US.json";
LoadTranslations(jsonFilePath);
}
}
// 公有静态属性用于获取单例实例
public static Localization Instance
{
get
{
return _instance.Value;
}
}
private bool LoadTranslations(string jsonFilePath)
{
try
{
var json = File.ReadAllText(jsonFilePath);
translations = JsonSerializer.Deserialize<Dictionary<string, string>>(json);
return true;
}
catch (Exception ex)
{
Console.WriteLine($"Failed to load JSON file: {ex.Message}");
Thread.Sleep(5000);
Environment.Exit(1);
return false;
}
}
public string Translate(string key)
{
return translations != null && translations.TryGetValue(key, out string value) ? value : key;
}
}
internal static class Program internal static class Program
{ {
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread] [STAThread]
private static void Main(string[] args) public static void Main(string[] args)
{ {
var localization = new Localization(); Localization localization = Localization.Instance;
if (args.Length == 0) if (args.Length == 0)
{ {