Files
2dust_v2rayN/v2rayN/AmazTool/LocalizationHelper.cs
T
Slnanx 2e32de2fbb fix (#6099)
* fix

* fix

* 移除自述

* 改用单例模式,避免多次初始化。

* 小修正

* 修正

* 更正自述

* 应该完成了
2024-11-17 19:42:14 +08:00

64 lines
2.0 KiB
C#

using System.Globalization;
using System.Text.Json;
namespace AmazTool
{
public class LocalizationHelper
{
private static Dictionary<string, string> languageResources = new();
static LocalizationHelper()
{
// 加载语言资源
LoadLanguageResources();
}
/// <summary>
/// 加载外部 JSON 文件中的语言资源
/// </summary>
private static void LoadLanguageResources()
{
try
{
string currentLanguage = CultureInfo.CurrentCulture.Name;
if (currentLanguage != "zh-CN" && currentLanguage != "en-US")
{
currentLanguage = "en-US";
}
string jsonFilePath = $"{currentLanguage}.json";
if (!File.Exists(jsonFilePath))
{
jsonFilePath = "en-US.json";
}
var json = File.ReadAllText(jsonFilePath);
if (!string.IsNullOrEmpty(json))
{
languageResources = JsonSerializer.Deserialize<Dictionary<string, string>>(json) ?? new Dictionary<string, string>();
}
}
catch (Exception ex)
{
Console.WriteLine($"Failed to load language resources: {ex.Message}");
languageResources = []; // 初始化为空字典
}
}
/// <summary>
/// 获取系统当前语言的本地化字符串
/// </summary>
/// <param name="key">要翻译的关键字</param>
/// <returns>对应语言的本地化字符串,如果没有找到则返回关键字</returns>
public static string GetLocalizedValue(string key)
{
if (languageResources != null && languageResources.TryGetValue(key, out var translation))
{
return translation;
}
return key;
}
}
}