diff --git a/src/BootstrapBlazor/BootstrapBlazor.csproj b/src/BootstrapBlazor/BootstrapBlazor.csproj index ee2fdce1f9c..fe343632130 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -1,7 +1,7 @@  - 9.6.2-beta01 + 9.6.2-beta02 diff --git a/src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs b/src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs index 2aa63f483d5..90b642d3ded 100644 --- a/src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs +++ b/src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs @@ -119,7 +119,36 @@ public override LocalizedString this[string name] } else { - HandleMissingResourceItem(name); + // 如果没有找到资源信息则尝试从父类中查找 + ret ??= GetStringFromBaseType(name); + + if (ret is null) + { + // 加入缺失资源信息缓存中 + HandleMissingResourceItem(name); + } + } + } + return ret; + } + + private string? GetStringFromBaseType(string name) + { + string? ret = null; + var type = Assembly.GetType(typeName); + var propertyInfo = type?.GetPropertyByName(name); + if (propertyInfo is { DeclaringType: not null }) + { + var baseType = propertyInfo.DeclaringType; + if (baseType != type) + { + var baseAssembly = baseType.Assembly; + var localizerStrings = MegerResolveLocalizers(CacheManager.GetAllStringsByTypeName(baseAssembly, baseType.FullName!)); + var l = localizerStrings.Find(i => i.Name == name); + if (l is { ResourceNotFound: false }) + { + ret = l.Value; + } } } return ret; diff --git a/test/UnitTest/Components/DisplayTest.cs b/test/UnitTest/Components/DisplayTest.cs index 5f3ecd2b8f2..9cac91323d1 100644 --- a/test/UnitTest/Components/DisplayTest.cs +++ b/test/UnitTest/Components/DisplayTest.cs @@ -56,7 +56,7 @@ public async Task LookupService_Ok() { pb.Add(a => a.LookupService, new MockLookupService()); }); - await Task.Delay(20); + await Task.Delay(50); Assert.Contains("Test1,Test2", cut.Markup); cut.SetParametersAndRender(pb => @@ -64,7 +64,7 @@ public async Task LookupService_Ok() pb.Add(a => a.LookupServiceKey, null); pb.Add(a => a.Lookup, new List { new("v1", "Test3"), new("v2", "Test4") }); }); - await Task.Delay(20); + await Task.Delay(50); Assert.Contains("Test3,Test4", cut.Markup); } diff --git a/test/UnitTest/Localization/JsonStringLocalizerTest.cs b/test/UnitTest/Localization/JsonStringLocalizerTest.cs index a485d761748..5affbb80faf 100644 --- a/test/UnitTest/Localization/JsonStringLocalizerTest.cs +++ b/test/UnitTest/Localization/JsonStringLocalizerTest.cs @@ -237,6 +237,23 @@ public void GetAllStrings_FromJson() Assert.Empty(resolve.GetAllStringsByCulture(true)); } + [Fact] + public void GetString_FromBaseTypeJson() + { + var sc = new ServiceCollection(); + sc.AddConfiguration(); + sc.AddBootstrapBlazor(); + + var provider = sc.BuildServiceProvider(); + var localizer = provider.GetRequiredService>(); + Assert.Equal("姓名", localizer["Name"].Value); + } + + class SubFoo : Foo + { + + } + [Fact] public void GetAllStrings_FromResolver() { diff --git a/test/UnitTest/Services/FullScreenServiceTest.cs b/test/UnitTest/Services/FullScreenServiceTest.cs index 121d04e1678..1d412d1c846 100644 --- a/test/UnitTest/Services/FullScreenServiceTest.cs +++ b/test/UnitTest/Services/FullScreenServiceTest.cs @@ -91,7 +91,7 @@ public void FullScreenOption_Ok() var option = new FullScreenOption() { Element = new("test01", null), Id = "test", Selector = "test-selector" }; Assert.NotNull(option.Id); Assert.Null(option.Element.Context); - Assert.Null(option.Selector); + Assert.NotNull(option.Selector); } private class MockFullScreen : ComponentBase