diff --git a/src/BootstrapBlazor/Localization/Json/JsonLocalizationOptions.cs b/src/BootstrapBlazor/Localization/Json/JsonLocalizationOptions.cs index 2136e06fca1..1668284f1a2 100644 --- a/src/BootstrapBlazor/Localization/Json/JsonLocalizationOptions.cs +++ b/src/BootstrapBlazor/Localization/Json/JsonLocalizationOptions.cs @@ -43,6 +43,11 @@ public class JsonLocalizationOptions : LocalizationOptions /// public bool IgnoreLocalizerMissing { get; set; } + /// + /// 获得/设置 如果 Value 值为 null 时使用 Key 代替 默认 false 触发异常 + /// + public bool UseKeyWhenValueIsNull { get; set; } + /// /// 获得/设置 资源文件是否热加载 默认 false /// diff --git a/src/BootstrapBlazor/Services/CacheManager.cs b/src/BootstrapBlazor/Services/CacheManager.cs index 936a8b3302f..f8d7158a014 100644 --- a/src/BootstrapBlazor/Services/CacheManager.cs +++ b/src/BootstrapBlazor/Services/CacheManager.cs @@ -152,10 +152,7 @@ public static int ElementCount(object? value) entry.SetDynamicAssemblyPolicy(type); return LambdaExtensions.CountLambda(type).Compile(); }); - if (invoker != null) - { - ret = invoker(value); - } + ret = invoker(value); } return ret; } @@ -237,12 +234,20 @@ private static JsonLocalizationOptions GetJsonLocalizationOption() Instance.Cache.Remove(key); Instance.Cache.Remove(typeKey); } - return Instance.GetOrCreate(typeKey, entry => + return Instance.GetOrCreate(typeKey, _ => { - var sections = Instance.GetOrCreate(key, entry => option.GetJsonStringFromAssembly(assembly, cultureName)); + var sections = Instance.GetOrCreate(key, _ => option.GetJsonStringFromAssembly(assembly, cultureName)); var items = sections.FirstOrDefault(kv => typeName.Equals(kv.Key, StringComparison.OrdinalIgnoreCase))? .GetChildren() - .SelectMany(kv => new[] { new LocalizedString(kv.Key, kv.Value!, false, typeName) }); + .Select(kv => + { + var value = kv.Value; + if (value == null && option.UseKeyWhenValueIsNull == true) + { + value = kv.Key; + } + return new LocalizedString(kv.Key, value ?? "", false, typeName); + }); #if NET8_0_OR_GREATER return items?.ToFrozenSet(); #else diff --git a/test/UnitTest/Locales/en-US.json b/test/UnitTest/Locales/en-US.json index 92afa009eb6..9f650f7779b 100644 --- a/test/UnitTest/Locales/en-US.json +++ b/test/UnitTest/Locales/en-US.json @@ -25,5 +25,9 @@ "PlaceHolder": "Click to select ...", "Primary": "Primary", "Middle": "Middle" + }, + "UnitTest.Utils.UtilityTest": { + "Test-Null": null, + "Test-Key:": "" } } diff --git a/test/UnitTest/Utils/UtilityTest.cs b/test/UnitTest/Utils/UtilityTest.cs index d3d8759fd79..2d543cc0cc8 100644 --- a/test/UnitTest/Utils/UtilityTest.cs +++ b/test/UnitTest/Utils/UtilityTest.cs @@ -379,6 +379,34 @@ public void GetJsonStringByTypeName_Ok() Utility.GetJsonStringByTypeName(option, dynamicType!.Assembly, "Test"); } + [Fact] + public void GetJsonStringByTypeName_UseKeyWhenValueIsNull() + { + // improve code coverage + var option = Context.Services.GetRequiredService>().Value; + option.UseKeyWhenValueIsNull = true; + var items = Utility.GetJsonStringByTypeName(option, this.GetType().Assembly, "UnitTest.Utils.UtilityTest", "en-US", true); + + var test1 = items.FirstOrDefault(i => i.Name == "Test-Null"); + Assert.NotNull(test1); + Assert.Equal("", test1.Value); + + var test2 = items.FirstOrDefault(i => i.Name == "Test-Key"); + Assert.NotNull(test2); + Assert.Equal("Test-Key", test2.Value); + + option.UseKeyWhenValueIsNull = false; + items = Utility.GetJsonStringByTypeName(option, this.GetType().Assembly, "UnitTest.Utils.UtilityTest", "en-US", true); + + test1 = items.FirstOrDefault(i => i.Name == "Test-Null"); + Assert.NotNull(test1); + Assert.Equal("", test1.Value); + + test2 = items.FirstOrDefault(i => i.Name == "Test-Key"); + Assert.NotNull(test2); + Assert.Equal("", test2.Value); + } + private class MockDynamicObject : IDynamicObject { public Guid DynamicObjectPrimaryKey { get; set; }