Browse Source

Fix LocalizationHelper for AmazTool

pull/6126/head
2dust 4 days ago
parent
commit
499a16feae
  1. 5
      v2rayN/AmazTool/AmazTool.csproj
  2. 0
      v2rayN/AmazTool/Assets/en-US.json
  3. 0
      v2rayN/AmazTool/Assets/zh-CN.json
  4. 24
      v2rayN/AmazTool/LocalizationHelper.cs

5
v2rayN/AmazTool/AmazTool.csproj

@ -9,4 +9,9 @@
<FileVersion>1.3.0</FileVersion> <FileVersion>1.3.0</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Assets\en-US.json" />
<EmbeddedResource Include="Assets\zh-CN.json" />
</ItemGroup>
</Project> </Project>

0
v2rayN/AmazTool/en-US.json → v2rayN/AmazTool/Assets/en-US.json

0
v2rayN/AmazTool/zh-CN.json → v2rayN/AmazTool/Assets/zh-CN.json

24
v2rayN/AmazTool/LocalizationHelper.cs

@ -6,7 +6,7 @@ namespace AmazTool
{ {
public class LocalizationHelper public class LocalizationHelper
{ {
private static Dictionary<string, string> languageResources = []; private static Dictionary<string, string> _languageResources = [];
static LocalizationHelper() static LocalizationHelper()
{ {
@ -18,26 +18,23 @@ namespace AmazTool
{ {
try try
{ {
string currentLanguage = CultureInfo.CurrentCulture.Name; var currentLanguage = CultureInfo.CurrentCulture.Name;
if (currentLanguage != "zh-CN" && currentLanguage != "en-US") if (currentLanguage != "zh-CN" && currentLanguage != "en-US")
{ {
currentLanguage = "en-US"; currentLanguage = "en-US";
} }
string resourceName = $"AmazTool.{currentLanguage}.json"; var resourceName = $"AmazTool.Assets.{currentLanguage}.json";
var assembly = Assembly.GetExecutingAssembly(); var assembly = Assembly.GetExecutingAssembly();
using Stream? stream = assembly.GetManifestResourceStream(resourceName); using var stream = assembly.GetManifestResourceStream(resourceName);
if (stream != null) if (stream == null) return;
{
using StreamReader reader = new(stream);
using StreamReader reader = new(stream);
var json = reader.ReadToEnd(); var json = reader.ReadToEnd();
if (!string.IsNullOrEmpty(json)) if (!string.IsNullOrEmpty(json))
{ {
languageResources = JsonSerializer.Deserialize<Dictionary<string, string>>(json) ?? new Dictionary<string, string>(); _languageResources = JsonSerializer.Deserialize<Dictionary<string, string>>(json) ?? new Dictionary<string, string>();
}
} }
} }
catch (IOException ex) catch (IOException ex)
@ -56,12 +53,7 @@ namespace AmazTool
public static string GetLocalizedValue(string key) public static string GetLocalizedValue(string key)
{ {
if (languageResources.TryGetValue(key, out var translation)) return _languageResources.TryGetValue(key, out var translation) ? translation : key;
{
return translation;
}
return key;
} }
} }
} }

Loading…
Cancel
Save