diff --git a/GFramework.Core/Functional/ResultExtensions.cs b/GFramework.Core/Functional/ResultExtensions.cs index 6be7870d..3d9d7d2d 100644 --- a/GFramework.Core/Functional/ResultExtensions.cs +++ b/GFramework.Core/Functional/ResultExtensions.cs @@ -177,7 +177,7 @@ public static class ResultExtensions return result.Match( succ: value => predicate(value) ? result - : Result.Fail(new ArgumentException(errorMessage, nameof(errorMessage))), + : Result.Fail(new ArgumentException(errorMessage)), fail: _ => result ); } diff --git a/GFramework.Core/Ioc/MicrosoftDiContainer.cs b/GFramework.Core/Ioc/MicrosoftDiContainer.cs index 4dd7034b..9192e289 100644 --- a/GFramework.Core/Ioc/MicrosoftDiContainer.cs +++ b/GFramework.Core/Ioc/MicrosoftDiContainer.cs @@ -394,7 +394,7 @@ public class MicrosoftDiContainer(IServiceCollection? serviceCollection = null) { if (assembly is null) { - throw new ArgumentException("Assemblies collection cannot contain null items.", nameof(assemblies)); + throw new ArgumentNullException(nameof(assemblies), "Assemblies collection cannot contain null items."); } } diff --git a/GFramework.Core/Localization/LocalizationString.cs b/GFramework.Core/Localization/LocalizationString.cs index c925cf9f..c926d73b 100644 --- a/GFramework.Core/Localization/LocalizationString.cs +++ b/GFramework.Core/Localization/LocalizationString.cs @@ -8,11 +8,26 @@ namespace GFramework.Core.Localization; /// public class LocalizationString : ILocalizationString { + /// + /// 正则分组名:变量名。 + /// + private const string VariableGroupName = "variable"; + + /// + /// 正则分组名:格式化器名。 + /// + private const string FormatterGroupName = "formatter"; + + /// + /// 正则分组名:格式化器参数。 + /// + private const string FormatterArgsGroupName = "args"; + /// /// 匹配 {variableName} 或 {variableName:formatter:args} 的正则表达式模式 /// private const string FormatVariablePattern = - @"\{([a-zA-Z_][a-zA-Z0-9_]*)(?::([a-zA-Z_][a-zA-Z0-9_]*)(?::([^}]+))?)?\}"; + @"\{(?[a-zA-Z_][a-zA-Z0-9_]*)(?::(?[a-zA-Z_][a-zA-Z0-9_]*)(?::(?[^}]+))?)?\}"; /// /// 预编译的静态正则表达式,用于格式化字符串中的变量替换 @@ -157,13 +172,13 @@ public class LocalizationString : ILocalizationString Dictionary variables, ILocalizationManager manager) { - var variableName = match.Groups[1].Value; + var variableName = match.Groups[VariableGroupName].Value; if (!variables.TryGetValue(variableName, out var value)) { return match.Value; } - var formatterName = GetOptionalGroupValue(match, 2); + var formatterName = GetOptionalGroupValue(match, FormatterGroupName); if (string.IsNullOrEmpty(formatterName)) { return FormatValue(value, manager); @@ -190,7 +205,7 @@ public class LocalizationString : ILocalizationString ILocalizationManager manager, out string result) { - var formatterArgs = GetOptionalGroupValue(match, 3) ?? string.Empty; + var formatterArgs = GetOptionalGroupValue(match, FormatterArgsGroupName) ?? string.Empty; if (GetFormatter(manager, formatterName) is { } formatter && formatter.TryFormat(formatterArgs, value, manager.CurrentCulture, out result)) { @@ -220,11 +235,11 @@ public class LocalizationString : ILocalizationString /// 获取正则表达式匹配组中的可选值 /// /// 正则表达式匹配结果 - /// 要获取的组索引 + /// 要获取的命名组 /// 如果该组匹配成功则返回其值;否则返回 null - private static string? GetOptionalGroupValue(Match match, int groupIndex) + private static string? GetOptionalGroupValue(Match match, string groupName) { - return match.Groups[groupIndex].Success ? match.Groups[groupIndex].Value : null; + return match.Groups[groupName].Success ? match.Groups[groupName].Value : null; } ///