forked from 2dust/v2rayN
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
12 changed files
with
84 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,59 @@ | ||
using System.Globalization; | ||
using System.Globalization; | ||
using System.Reflection; | ||
using System.Text.Json; | ||
|
||
namespace AmazTool | ||
{ | ||
public class LocalizationHelper | ||
{ | ||
private static Dictionary<string, string> languageResources = new(); | ||
private static Dictionary<string, string> _languageResources = []; | ||
|
||
static LocalizationHelper() | ||
{ | ||
// 加载语言资源 | ||
LoadLanguageResources(); | ||
} | ||
|
||
/// <summary> | ||
/// 加载外部 JSON 文件中的语言资源 | ||
/// </summary> | ||
private static void LoadLanguageResources() | ||
{ | ||
try | ||
{ | ||
string currentLanguage = CultureInfo.CurrentCulture.Name; | ||
var 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 resourceName = $"AmazTool.Assets.{currentLanguage}.json"; | ||
var assembly = Assembly.GetExecutingAssembly(); | ||
|
||
var json = File.ReadAllText(jsonFilePath); | ||
using var stream = assembly.GetManifestResourceStream(resourceName); | ||
if (stream == null) return; | ||
|
||
using StreamReader reader = new(stream); | ||
var json = reader.ReadToEnd(); | ||
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) | ||
{ | ||
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($"Failed to load language resources: {ex.Message}"); | ||
languageResources = []; // 初始化为空字典 | ||
Console.WriteLine($"Unexpected error occurred: {ex.Message}"); | ||
} | ||
} | ||
|
||
/// <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; | ||
return _languageResources.TryGetValue(key, out var translation) ? translation : key; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters