Skip to content
31 changes: 30 additions & 1 deletion src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,36 @@ public override LocalizedString this[string name]
}
else
{
Comment thread
ArgoZhang marked this conversation as resolved.
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;
Expand Down
4 changes: 2 additions & 2 deletions test/UnitTest/Components/DisplayTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ 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 =>
{
pb.Add(a => a.LookupServiceKey, null);
pb.Add(a => a.Lookup, new List<SelectedItem> { new("v1", "Test3"), new("v2", "Test4") });
});
await Task.Delay(20);
await Task.Delay(50);
Assert.Contains("Test3,Test4", cut.Markup);
}

Expand Down
17 changes: 17 additions & 0 deletions test/UnitTest/Localization/JsonStringLocalizerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IStringLocalizer<SubFoo>>();
Assert.Equal("姓名", localizer["Name"].Value);
}

class SubFoo : Foo
{

}

[Fact]
public void GetAllStrings_FromResolver()
{
Expand Down
2 changes: 1 addition & 1 deletion test/UnitTest/Services/FullScreenServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down