diff --git a/src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs b/src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs index fb7386858d7..9bfcdfc3e01 100644 --- a/src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs +++ b/src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs @@ -5,7 +5,6 @@ using Microsoft.Extensions.Localization; using Microsoft.Extensions.Logging; -using System.Collections.Concurrent; using System.Globalization; using System.Reflection; using System.Resources; @@ -105,33 +104,28 @@ public override LocalizedString this[string name] return ret; } - private readonly ConcurrentDictionary _missingManifestCache = []; private string? GetStringFromJson(string name) { - // 从 json 本地化文件中获取字符串 - // get string from json localization file var localizerStrings = MergeResolveLocalizers(CacheManager.GetAllStringsByTypeName(Assembly, typeName)); var cacheKey = $"name={name}&culture={CultureInfo.CurrentUICulture.Name}"; string? ret = null; - if (!_missingManifestCache.ContainsKey(cacheKey)) + + var l = localizerStrings.Find(i => i.Name == name); + if (l is { ResourceNotFound: false }) { - var l = localizerStrings.Find(i => i.Name == name); - if (l is { ResourceNotFound: false }) - { - ret = l.Value; - } - else - { - // 如果没有找到资源信息则尝试从父类中查找 - // If resource info not found, try to find from base class - ret ??= GetStringFromBaseType(name); + ret = l.Value; + } + else + { + // 如果没有找到资源信息则尝试从父类中查找 + // If resource info not found, try to find from base class + ret ??= GetStringFromBaseType(name); - if (ret is null) - { - // 加入缺失资源信息缓存中 - // Add to missing resource info cache - HandleMissingResourceItem(name); - } + if (ret is null) + { + // 加入缺失资源信息缓存中 + // Add to missing resource info cache + HandleMissingResourceItem(name); } } return ret; @@ -172,11 +166,15 @@ private List MergeResolveLocalizers(IEnumerable? _allLocalizedStrings; diff --git a/test/UnitTest/Localization/JsonStringLocalizerTest.cs b/test/UnitTest/Localization/JsonStringLocalizerTest.cs index 1d482e2b28b..2bc5c3ab7a8 100644 --- a/test/UnitTest/Localization/JsonStringLocalizerTest.cs +++ b/test/UnitTest/Localization/JsonStringLocalizerTest.cs @@ -215,7 +215,8 @@ public void GetAllStrings_FromResource() Assert.NotEmpty(items); Assert.Equal("test-name", items.First(i => i.Name == "Name").Value); - var name = Utility.GetDisplayName(typeof(Dummy), "DummyName"); + var type = typeof(Dummy); + var name = Utility.GetDisplayName(type, "DummyName"); Assert.Equal("test-name", name); } @@ -498,5 +499,8 @@ public void GetAllStrings_FromInject() var localizer = provider.GetRequiredService>(); var item = localizer["Foo.Name"]; Assert.NotEqual("Foo.Name", item); + + item = localizer["missing-item"]; + Assert.True(item.ResourceNotFound); } }