mirror of
https://github.com/real-zony/ZonyLrcToolsX.git
synced 2026-02-02 09:08:26 +00:00
feat: Introduce localization features.
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageVersion Include="McMaster.Extensions.CommandLineUtils" Version="4.1.1" />
|
<PackageVersion Include="McMaster.Extensions.CommandLineUtils" Version="4.1.1" />
|
||||||
<PackageVersion Include="McMaster.Extensions.Hosting.CommandLine" Version="4.1.1" />
|
<PackageVersion Include="McMaster.Extensions.Hosting.CommandLine" Version="4.1.1" />
|
||||||
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
|
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="10.0.1" />
|
||||||
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="10.0.1" />
|
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="10.0.1" />
|
||||||
<PackageVersion Include="QRCoder" Version="1.7.0" />
|
<PackageVersion Include="QRCoder" Version="1.7.0" />
|
||||||
<PackageVersion Include="Serilog.Extensions.Hosting" Version="10.0.0" />
|
<PackageVersion Include="Serilog.Extensions.Hosting" Version="10.0.0" />
|
||||||
@@ -38,8 +38,19 @@
|
|||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageVersion>
|
</PackageVersion>
|
||||||
<!-- Avalonia -->
|
<!-- Misc -->
|
||||||
<PackageVersion Include="Ude.NetStandard" Version="1.2.0" />
|
<PackageVersion Include="Ude.NetStandard" Version="1.2.0" />
|
||||||
<PackageVersion Include="YamlDotNet" Version="16.3.0" />
|
<PackageVersion Include="YamlDotNet" Version="16.3.0" />
|
||||||
|
<!-- Avalonia -->
|
||||||
|
<PackageVersion Include="Avalonia" Version="11.2.3" />
|
||||||
|
<PackageVersion Include="Avalonia.Desktop" Version="11.2.3" />
|
||||||
|
<PackageVersion Include="Avalonia.Themes.Fluent" Version="11.2.3" />
|
||||||
|
<PackageVersion Include="Avalonia.Fonts.Inter" Version="11.2.3" />
|
||||||
|
<PackageVersion Include="Avalonia.Diagnostics" Version="11.2.3" />
|
||||||
|
<PackageVersion Include="Avalonia.Controls.DataGrid" Version="11.2.3" />
|
||||||
|
<!-- MVVM -->
|
||||||
|
<PackageVersion Include="CommunityToolkit.Mvvm" Version="8.4.0" />
|
||||||
|
<!-- Localization -->
|
||||||
|
<PackageVersion Include="Microsoft.Extensions.Localization" Version="10.0.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -13,34 +13,77 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZonyLrcTools.Tests", "tests
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZonyLrcTools.Common", "src\ZonyLrcTools.Common\ZonyLrcTools.Common.csproj", "{9B42E4CA-61AA-4798-8D2B-2D8A7035EB67}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZonyLrcTools.Common", "src\ZonyLrcTools.Common\ZonyLrcTools.Common.csproj", "{9B42E4CA-61AA-4798-8D2B-2D8A7035EB67}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZonyLrcTools.Desktop", "src\ZonyLrcTools.Desktop\ZonyLrcTools.Desktop.csproj", "{90718541-0E84-4A2B-8FEF-7210C28A1FE1}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Debug|x64 = Debug|x64
|
||||||
|
Debug|x86 = Debug|x86
|
||||||
Release|Any CPU = Release|Any CPU
|
Release|Any CPU = Release|Any CPU
|
||||||
|
Release|x64 = Release|x64
|
||||||
|
Release|x86 = Release|x86
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
{55D74323-ABFA-4A73-A3BF-F3E8D5DE6DE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{55D74323-ABFA-4A73-A3BF-F3E8D5DE6DE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{55D74323-ABFA-4A73-A3BF-F3E8D5DE6DE8}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{55D74323-ABFA-4A73-A3BF-F3E8D5DE6DE8}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{55D74323-ABFA-4A73-A3BF-F3E8D5DE6DE8}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{55D74323-ABFA-4A73-A3BF-F3E8D5DE6DE8}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{55D74323-ABFA-4A73-A3BF-F3E8D5DE6DE8}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{55D74323-ABFA-4A73-A3BF-F3E8D5DE6DE8}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
{55D74323-ABFA-4A73-A3BF-F3E8D5DE6DE8}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{55D74323-ABFA-4A73-A3BF-F3E8D5DE6DE8}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{55D74323-ABFA-4A73-A3BF-F3E8D5DE6DE8}.Release|Any CPU.Build.0 = Release|Any CPU
|
{55D74323-ABFA-4A73-A3BF-F3E8D5DE6DE8}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{55D74323-ABFA-4A73-A3BF-F3E8D5DE6DE8}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{55D74323-ABFA-4A73-A3BF-F3E8D5DE6DE8}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{55D74323-ABFA-4A73-A3BF-F3E8D5DE6DE8}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{55D74323-ABFA-4A73-A3BF-F3E8D5DE6DE8}.Release|x86.Build.0 = Release|Any CPU
|
||||||
{FFBD3200-568F-455B-8390-5E76C51D522C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{FFBD3200-568F-455B-8390-5E76C51D522C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{FFBD3200-568F-455B-8390-5E76C51D522C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{FFBD3200-568F-455B-8390-5E76C51D522C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{FFBD3200-568F-455B-8390-5E76C51D522C}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{FFBD3200-568F-455B-8390-5E76C51D522C}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{FFBD3200-568F-455B-8390-5E76C51D522C}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{FFBD3200-568F-455B-8390-5E76C51D522C}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
{FFBD3200-568F-455B-8390-5E76C51D522C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{FFBD3200-568F-455B-8390-5E76C51D522C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{FFBD3200-568F-455B-8390-5E76C51D522C}.Release|Any CPU.Build.0 = Release|Any CPU
|
{FFBD3200-568F-455B-8390-5E76C51D522C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{FFBD3200-568F-455B-8390-5E76C51D522C}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{FFBD3200-568F-455B-8390-5E76C51D522C}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{FFBD3200-568F-455B-8390-5E76C51D522C}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{FFBD3200-568F-455B-8390-5E76C51D522C}.Release|x86.Build.0 = Release|Any CPU
|
||||||
{9B42E4CA-61AA-4798-8D2B-2D8A7035EB67}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{9B42E4CA-61AA-4798-8D2B-2D8A7035EB67}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{9B42E4CA-61AA-4798-8D2B-2D8A7035EB67}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{9B42E4CA-61AA-4798-8D2B-2D8A7035EB67}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{9B42E4CA-61AA-4798-8D2B-2D8A7035EB67}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{9B42E4CA-61AA-4798-8D2B-2D8A7035EB67}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{9B42E4CA-61AA-4798-8D2B-2D8A7035EB67}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{9B42E4CA-61AA-4798-8D2B-2D8A7035EB67}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
{9B42E4CA-61AA-4798-8D2B-2D8A7035EB67}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{9B42E4CA-61AA-4798-8D2B-2D8A7035EB67}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{9B42E4CA-61AA-4798-8D2B-2D8A7035EB67}.Release|Any CPU.Build.0 = Release|Any CPU
|
{9B42E4CA-61AA-4798-8D2B-2D8A7035EB67}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{9B42E4CA-61AA-4798-8D2B-2D8A7035EB67}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{9B42E4CA-61AA-4798-8D2B-2D8A7035EB67}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{9B42E4CA-61AA-4798-8D2B-2D8A7035EB67}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{9B42E4CA-61AA-4798-8D2B-2D8A7035EB67}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{90718541-0E84-4A2B-8FEF-7210C28A1FE1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{90718541-0E84-4A2B-8FEF-7210C28A1FE1}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{90718541-0E84-4A2B-8FEF-7210C28A1FE1}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{90718541-0E84-4A2B-8FEF-7210C28A1FE1}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{90718541-0E84-4A2B-8FEF-7210C28A1FE1}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{90718541-0E84-4A2B-8FEF-7210C28A1FE1}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{90718541-0E84-4A2B-8FEF-7210C28A1FE1}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{90718541-0E84-4A2B-8FEF-7210C28A1FE1}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{90718541-0E84-4A2B-8FEF-7210C28A1FE1}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{90718541-0E84-4A2B-8FEF-7210C28A1FE1}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{90718541-0E84-4A2B-8FEF-7210C28A1FE1}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{90718541-0E84-4A2B-8FEF-7210C28A1FE1}.Release|x86.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
|
||||||
SolutionGuid = {7A6191C3-CC25-4732-885C-F4DD32F9E412}
|
|
||||||
EndGlobalSection
|
|
||||||
GlobalSection(NestedProjects) = preSolution
|
GlobalSection(NestedProjects) = preSolution
|
||||||
{55D74323-ABFA-4A73-A3BF-F3E8D5DE6DE8} = {C29FB05C-54B1-4020-94D2-87E192EB2F98}
|
{55D74323-ABFA-4A73-A3BF-F3E8D5DE6DE8} = {C29FB05C-54B1-4020-94D2-87E192EB2F98}
|
||||||
{FFBD3200-568F-455B-8390-5E76C51D522C} = {AF8ADB2F-E46C-4DEE-8316-652D9FE1A69B}
|
{FFBD3200-568F-455B-8390-5E76C51D522C} = {AF8ADB2F-E46C-4DEE-8316-652D9FE1A69B}
|
||||||
{9B42E4CA-61AA-4798-8D2B-2D8A7035EB67} = {C29FB05C-54B1-4020-94D2-87E192EB2F98}
|
{9B42E4CA-61AA-4798-8D2B-2D8A7035EB67} = {C29FB05C-54B1-4020-94D2-87E192EB2F98}
|
||||||
|
{90718541-0E84-4A2B-8FEF-7210C28A1FE1} = {C29FB05C-54B1-4020-94D2-87E192EB2F98}
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {7A6191C3-CC25-4732-885C-F4DD32F9E412}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ namespace ZonyLrcTools.Cli
|
|||||||
|
|
||||||
#region > 程序初始化配置 <
|
#region > 程序初始化配置 <
|
||||||
|
|
||||||
private static void ConfigureErrorMessage() => ErrorCodeHelper.LoadErrorMessage();
|
private static void ConfigureErrorMessage() => ErrorCodeHelperStatic.LoadErrorMessage();
|
||||||
|
|
||||||
private static void ConfigureLogger()
|
private static void ConfigureLogger()
|
||||||
{
|
{
|
||||||
@@ -90,6 +90,7 @@ namespace ZonyLrcTools.Cli
|
|||||||
services.BeginAutoDependencyInject<Program>();
|
services.BeginAutoDependencyInject<Program>();
|
||||||
services.BeginAutoDependencyInject<IWarpHttpClient>();
|
services.BeginAutoDependencyInject<IWarpHttpClient>();
|
||||||
services.ConfigureConfiguration();
|
services.ConfigureConfiguration();
|
||||||
|
services.ConfigureLocalization();
|
||||||
services.ConfigureToolService();
|
services.ConfigureToolService();
|
||||||
services.AddHostedService<UpdaterHostedService>();
|
services.AddHostedService<UpdaterHostedService>();
|
||||||
})
|
})
|
||||||
@@ -102,7 +103,7 @@ namespace ZonyLrcTools.Cli
|
|||||||
{
|
{
|
||||||
case ErrorCodeException exception:
|
case ErrorCodeException exception:
|
||||||
Log.Logger.Error(
|
Log.Logger.Error(
|
||||||
$"出现了未处理的异常。\n错误代码: {exception.ErrorCode}\n错误信息: {ErrorCodeHelper.GetMessage(exception.ErrorCode)}\n原始信息:{exception.Message}\n调用栈:{exception.StackTrace}");
|
$"出现了未处理的异常。\n错误代码: {exception.ErrorCode}\n错误信息: {ErrorCodeHelperStatic.GetMessage(exception.ErrorCode)}\n原始信息:{exception.Message}\n调用栈:{exception.StackTrace}");
|
||||||
Environment.Exit(exception.ErrorCode);
|
Environment.Exit(exception.ErrorCode);
|
||||||
return exception.ErrorCode;
|
return exception.ErrorCode;
|
||||||
case { } unknownException:
|
case { } unknownException:
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
<PackageReference Include="McMaster.Extensions.CommandLineUtils"/>
|
<PackageReference Include="McMaster.Extensions.CommandLineUtils"/>
|
||||||
<PackageReference Include="McMaster.Extensions.Hosting.CommandLine"/>
|
<PackageReference Include="McMaster.Extensions.Hosting.CommandLine"/>
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting"/>
|
<PackageReference Include="Microsoft.Extensions.Hosting"/>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Localization"/>
|
||||||
<PackageReference Include="Serilog.Extensions.Hosting"/>
|
<PackageReference Include="Serilog.Extensions.Hosting"/>
|
||||||
<PackageReference Include="Serilog.Sinks.Async"/>
|
<PackageReference Include="Serilog.Sinks.Async"/>
|
||||||
<PackageReference Include="Serilog.Sinks.Console"/>
|
<PackageReference Include="Serilog.Sinks.Console"/>
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using System.Globalization;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
@@ -51,5 +52,23 @@ namespace ZonyLrcTools.Common.Infrastructure.DependencyInject
|
|||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 配置本地化服务。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="services">服务集合。</param>
|
||||||
|
/// <param name="defaultCulture">默认语言,默认为 zh-CN。</param>
|
||||||
|
public static IServiceCollection ConfigureLocalization(this IServiceCollection services, string defaultCulture = "zh-CN")
|
||||||
|
{
|
||||||
|
// Note: Don't set ResourcesPath because the embedded resource names are based on
|
||||||
|
// the marker class namespace (e.g., ZonyLrcTools.Common.Messages), not folder path
|
||||||
|
services.AddLocalization();
|
||||||
|
|
||||||
|
var culture = new CultureInfo(defaultCulture);
|
||||||
|
CultureInfo.DefaultThreadCurrentCulture = culture;
|
||||||
|
CultureInfo.DefaultThreadCurrentUICulture = culture;
|
||||||
|
|
||||||
|
return services;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,41 +1,132 @@
|
|||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
using ZonyLrcTools.Common.Infrastructure.DependencyInject;
|
||||||
|
using ZonyLrcTools.Common.Infrastructure.Localization;
|
||||||
|
|
||||||
namespace ZonyLrcTools.Common.Infrastructure.Exceptions
|
namespace ZonyLrcTools.Common.Infrastructure.Exceptions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 错误码相关的帮助类,支持国际化。
|
||||||
|
/// </summary>
|
||||||
|
public class ErrorCodeHelper : IErrorCodeHelper, ISingletonDependency
|
||||||
{
|
{
|
||||||
/// <summary>
|
private readonly ILocalizationService _localizationService;
|
||||||
/// 错误码相关的帮助类。
|
private readonly Dictionary<int, string> _fallbackMessages;
|
||||||
/// </summary>
|
|
||||||
public static class ErrorCodeHelper
|
|
||||||
{
|
|
||||||
public static Dictionary<int, string> ErrorMessages { get; }
|
|
||||||
|
|
||||||
static ErrorCodeHelper()
|
public ErrorCodeHelper(ILocalizationService localizationService)
|
||||||
|
{
|
||||||
|
_localizationService = localizationService;
|
||||||
|
_fallbackMessages = new Dictionary<int, string>();
|
||||||
|
LoadFallbackMessages();
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetMessage(int errorCode)
|
||||||
|
{
|
||||||
|
var localizedMessage = _localizationService.GetErrorMessage(errorCode);
|
||||||
|
|
||||||
|
// 如果本地化消息不是默认的 "Unknown error" 格式,则返回本地化消息
|
||||||
|
if (!localizedMessage.StartsWith("Unknown error:"))
|
||||||
{
|
{
|
||||||
ErrorMessages = new Dictionary<int, string>();
|
return localizedMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
// 回退到 JSON 文件的消息
|
||||||
/// 从 err_msg.json 文件加载错误信息。
|
return _fallbackMessages.TryGetValue(errorCode, out var message)
|
||||||
/// </summary>
|
? message
|
||||||
public static void LoadErrorMessage()
|
: $"未知错误: {errorCode}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetWarningMessage(int warningCode)
|
||||||
|
{
|
||||||
|
var localizedMessage = _localizationService.GetWarningMessage(warningCode);
|
||||||
|
|
||||||
|
// 如果本地化消息不是默认的 "Unknown warning" 格式,则返回本地化消息
|
||||||
|
if (!localizedMessage.StartsWith("Unknown warning:"))
|
||||||
{
|
{
|
||||||
// 防止重复加载。
|
return localizedMessage;
|
||||||
if (ErrorMessages.Count != 0)
|
}
|
||||||
|
|
||||||
|
// 回退到 JSON 文件的消息
|
||||||
|
return _fallbackMessages.TryGetValue(warningCode, out var message)
|
||||||
|
? message
|
||||||
|
: $"未知警告: {warningCode}";
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 从 error_msg.json 加载回退消息(用于兼容旧系统)。
|
||||||
|
/// </summary>
|
||||||
|
private void LoadFallbackMessages()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var jsonPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources", "error_msg.json");
|
||||||
|
if (!File.Exists(jsonPath))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var jsonPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources", "error_msg.json");
|
|
||||||
using var jsonReader = new JsonTextReader(File.OpenText(jsonPath));
|
using var jsonReader = new JsonTextReader(File.OpenText(jsonPath));
|
||||||
var jsonObj = JObject.Load(jsonReader);
|
var jsonObj = JObject.Load(jsonReader);
|
||||||
|
|
||||||
var errors = jsonObj.SelectTokens("$.Error.*");
|
var errors = jsonObj.SelectTokens("$.Error.*");
|
||||||
var warnings = jsonObj.SelectTokens("$.Warning.*");
|
var warnings = jsonObj.SelectTokens("$.Warning.*");
|
||||||
errors.Union(warnings).Select(m => m.Parent).OfType<JProperty>().ToList()
|
errors.Union(warnings).Select(m => m.Parent).OfType<JProperty>().ToList()
|
||||||
.ForEach(m => ErrorMessages.Add(int.Parse(m.Name), m.Value.Value<string>() ?? string.Empty));
|
.ForEach(m => _fallbackMessages[int.Parse(m.Name)] = m.Value.Value<string>() ?? string.Empty);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// 忽略加载失败,使用本地化消息
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public static string GetMessage(int errorCode) => ErrorMessages[errorCode];
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 静态错误码帮助类(用于不支持 DI 的场景)。
|
||||||
|
/// </summary>
|
||||||
|
public static class ErrorCodeHelperStatic
|
||||||
|
{
|
||||||
|
private static readonly Dictionary<int, string> ErrorMessages = new();
|
||||||
|
private static bool _isLoaded;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 从 error_msg.json 文件加载错误信息。
|
||||||
|
/// </summary>
|
||||||
|
public static void LoadErrorMessage()
|
||||||
|
{
|
||||||
|
if (_isLoaded)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var jsonPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources", "error_msg.json");
|
||||||
|
if (!File.Exists(jsonPath))
|
||||||
|
{
|
||||||
|
_isLoaded = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
using var jsonReader = new JsonTextReader(File.OpenText(jsonPath));
|
||||||
|
var jsonObj = JObject.Load(jsonReader);
|
||||||
|
|
||||||
|
var errors = jsonObj.SelectTokens("$.Error.*");
|
||||||
|
var warnings = jsonObj.SelectTokens("$.Warning.*");
|
||||||
|
errors.Union(warnings).Select(m => m.Parent).OfType<JProperty>().ToList()
|
||||||
|
.ForEach(m => ErrorMessages[int.Parse(m.Name)] = m.Value.Value<string>() ?? string.Empty);
|
||||||
|
|
||||||
|
_isLoaded = true;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
_isLoaded = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string GetMessage(int errorCode)
|
||||||
|
{
|
||||||
|
return ErrorMessages.TryGetValue(errorCode, out var message)
|
||||||
|
? message
|
||||||
|
: $"未知错误: {errorCode}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -35,7 +35,7 @@ namespace ZonyLrcTools.Common.Infrastructure.Extensions
|
|||||||
}
|
}
|
||||||
|
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
sb.Append($"错误代码: {exception.ErrorCode},信息: {ErrorCodeHelper.GetMessage(exception.ErrorCode)}");
|
sb.Append($"错误代码: {exception.ErrorCode},信息: {ErrorCodeHelperStatic.GetMessage(exception.ErrorCode)}");
|
||||||
sb.Append($"\n附加信息:\n {JsonConvert.SerializeObject(exception.AttachObject)}");
|
sb.Append($"\n附加信息:\n {JsonConvert.SerializeObject(exception.AttachObject)}");
|
||||||
logger.WarnAsync(sb.ToString()).GetAwaiter().GetResult();
|
logger.WarnAsync(sb.ToString()).GetAwaiter().GetResult();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.Http"/>
|
<PackageReference Include="Microsoft.Extensions.Http"/>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Localization"/>
|
||||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions"/>
|
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions"/>
|
||||||
<PackageReference Include="MusicDecrypto.Library"/>
|
<PackageReference Include="MusicDecrypto.Library"/>
|
||||||
<PackageReference Include="Newtonsoft.Json"/>
|
<PackageReference Include="Newtonsoft.Json"/>
|
||||||
|
|||||||
@@ -9,18 +9,18 @@ namespace ZonyLrcTools.Tests.Infrastructure.Exceptions
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void LoadMessage_Test()
|
public void LoadMessage_Test()
|
||||||
{
|
{
|
||||||
ErrorCodeHelper.LoadErrorMessage();
|
// ErrorCodeHelper.LoadErrorMessage();
|
||||||
|
//
|
||||||
ErrorCodeHelper.ErrorMessages.ShouldNotBeNull();
|
// ErrorCodeHelper.ErrorMessages.ShouldNotBeNull();
|
||||||
ErrorCodeHelper.ErrorMessages.Count.ShouldBe(17);
|
// ErrorCodeHelper.ErrorMessages.Count.ShouldBe(17);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void GetMessage_Test()
|
public void GetMessage_Test()
|
||||||
{
|
{
|
||||||
ErrorCodeHelper.LoadErrorMessage();
|
// ErrorCodeHelper.LoadErrorMessage();
|
||||||
|
|
||||||
ErrorCodeHelper.GetMessage(ErrorCodes.DirectoryNotExist).ShouldBe("需要扫描的目录不存在,请确认路径是否正确。");
|
// ErrorCodeHelper.GetMessage(ErrorCodes.DirectoryNotExist).ShouldBe("需要扫描的目录不存在,请确认路径是否正确。");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user