mirror of
https://github.com/2dust/v2rayN.git
synced 2026-10-05 05:08:21 +03:00
* fix * fix * 移除自述 * 改用单例模式,避免多次初始化。 * 小修正 * 修正 * 更正自述 * 应该完成了 * 内嵌资源 * Update LocalizationHelper.cs
68 lines
2.0 KiB
C#
68 lines
2.0 KiB
C#
using System.Globalization;
|
|
using System.Reflection;
|
|
using System.Text.Json;
|
|
|
|
namespace AmazTool
|
|
{
|
|
public class LocalizationHelper
|
|
{
|
|
private static Dictionary<string, string> languageResources = [];
|
|
|
|
static LocalizationHelper()
|
|
{
|
|
// 加载语言资源
|
|
LoadLanguageResources();
|
|
}
|
|
|
|
private static void LoadLanguageResources()
|
|
{
|
|
try
|
|
{
|
|
string currentLanguage = CultureInfo.CurrentCulture.Name;
|
|
if (currentLanguage != "zh-CN" && currentLanguage != "en-US")
|
|
{
|
|
currentLanguage = "en-US";
|
|
}
|
|
|
|
string resourceName = $"AmazTool.{currentLanguage}.json";
|
|
var assembly = Assembly.GetExecutingAssembly();
|
|
|
|
using Stream? stream = assembly.GetManifestResourceStream(resourceName);
|
|
if (stream != null)
|
|
{
|
|
using StreamReader reader = new(stream);
|
|
|
|
var json = reader.ReadToEnd();
|
|
|
|
if (!string.IsNullOrEmpty(json))
|
|
{
|
|
languageResources = JsonSerializer.Deserialize<Dictionary<string, string>>(json) ?? new Dictionary<string, string>();
|
|
}
|
|
}
|
|
}
|
|
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)
|
|
{
|
|
if (languageResources.TryGetValue(key, out var translation))
|
|
{
|
|
return translation;
|
|
}
|
|
|
|
return key;
|
|
}
|
|
}
|
|
}
|