Skip to content

Commit d86aa37

Browse files
feat(Localizer): add GetStringFromBaseType logic support base type (#6006)
* 属性自动尝试从父类中获取json语言资源 * test: 更新单元测试 * refactor: 重构代码 * 增加单元测试 * revert: 撤销更改 * test: 更新单元测试 * refactor: 更新单元测试 * chore: bump version 9.6.2-beta02 Co-Authored-By: Diego2098 <[email protected]> --------- Co-Authored-By: Argo Zhang <[email protected]>
1 parent cd0284c commit d86aa37

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.6.2-beta01</Version>
4+
<Version>9.6.2-beta02</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,36 @@ public override LocalizedString this[string name]
119119
}
120120
else
121121
{
122-
HandleMissingResourceItem(name);
122+
// 如果没有找到资源信息则尝试从父类中查找
123+
ret ??= GetStringFromBaseType(name);
124+
125+
if (ret is null)
126+
{
127+
// 加入缺失资源信息缓存中
128+
HandleMissingResourceItem(name);
129+
}
130+
}
131+
}
132+
return ret;
133+
}
134+
135+
private string? GetStringFromBaseType(string name)
136+
{
137+
string? ret = null;
138+
var type = Assembly.GetType(typeName);
139+
var propertyInfo = type?.GetPropertyByName(name);
140+
if (propertyInfo is { DeclaringType: not null })
141+
{
142+
var baseType = propertyInfo.DeclaringType;
143+
if (baseType != type)
144+
{
145+
var baseAssembly = baseType.Assembly;
146+
var localizerStrings = MegerResolveLocalizers(CacheManager.GetAllStringsByTypeName(baseAssembly, baseType.FullName!));
147+
var l = localizerStrings.Find(i => i.Name == name);
148+
if (l is { ResourceNotFound: false })
149+
{
150+
ret = l.Value;
151+
}
123152
}
124153
}
125154
return ret;

test/UnitTest/Components/DisplayTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ public async Task LookupService_Ok()
5656
{
5757
pb.Add(a => a.LookupService, new MockLookupService());
5858
});
59-
await Task.Delay(20);
59+
await Task.Delay(50);
6060
Assert.Contains("Test1,Test2", cut.Markup);
6161

6262
cut.SetParametersAndRender(pb =>
6363
{
6464
pb.Add(a => a.LookupServiceKey, null);
6565
pb.Add(a => a.Lookup, new List<SelectedItem> { new("v1", "Test3"), new("v2", "Test4") });
6666
});
67-
await Task.Delay(20);
67+
await Task.Delay(50);
6868
Assert.Contains("Test3,Test4", cut.Markup);
6969
}
7070

test/UnitTest/Localization/JsonStringLocalizerTest.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,23 @@ public void GetAllStrings_FromJson()
237237
Assert.Empty(resolve.GetAllStringsByCulture(true));
238238
}
239239

240+
[Fact]
241+
public void GetString_FromBaseTypeJson()
242+
{
243+
var sc = new ServiceCollection();
244+
sc.AddConfiguration();
245+
sc.AddBootstrapBlazor();
246+
247+
var provider = sc.BuildServiceProvider();
248+
var localizer = provider.GetRequiredService<IStringLocalizer<SubFoo>>();
249+
Assert.Equal("姓名", localizer["Name"].Value);
250+
}
251+
252+
class SubFoo : Foo
253+
{
254+
255+
}
256+
240257
[Fact]
241258
public void GetAllStrings_FromResolver()
242259
{

test/UnitTest/Services/FullScreenServiceTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void FullScreenOption_Ok()
9191
var option = new FullScreenOption() { Element = new("test01", null), Id = "test", Selector = "test-selector" };
9292
Assert.NotNull(option.Id);
9393
Assert.Null(option.Element.Context);
94-
Assert.Null(option.Selector);
94+
Assert.NotNull(option.Selector);
9595
}
9696

9797
private class MockFullScreen : ComponentBase

0 commit comments

Comments
 (0)