mirror of
https://github.com/2dust/v2rayN.git
synced 2025-08-31 07:16:20 +00:00
修正
This commit is contained in:
parent
887a5734ac
commit
8236fe1840
2 changed files with 95 additions and 18 deletions
|
@ -7,48 +7,63 @@ 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";
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加载相应语言的JSON文件
|
// 加载相应语言的 JSON 文件
|
||||||
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);
|
||||||
return false; // 读取或解析JSON文件失败
|
return false; // 读取或解析 JSON 文件失败
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 根据键值返回相应的翻译,如果不存在则返回键值本身
|
||||||
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;
|
||||||
|
|
|
@ -1,21 +1,26 @@
|
||||||
/**
|
/**
|
||||||
* 使用JSON文件对C#应用程序进行本地化。
|
* 该程序使用JSON文件对C#应用程序进行本地化。
|
||||||
* 程序根据系统当前的语言加载相应的语言文件。
|
* 程序根据系统当前的语言加载相应的语言文件。
|
||||||
* 如果当前语言不被支持,则默认使用英语。
|
* 如果当前语言不被支持,则默认使用英语。
|
||||||
*
|
*
|
||||||
|
* 库:
|
||||||
|
* - System.Collections.Generic
|
||||||
|
* - System.Globalization
|
||||||
|
* - System.IO
|
||||||
|
* - System.Text.Json
|
||||||
|
*
|
||||||
* 用法:
|
* 用法:
|
||||||
* - 为每种支持的语言创建JSON文件(例如,en-US.json,zh-CN.json)。
|
* - 为每种支持的语言创建JSON文件(例如,en.json,zh.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)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue