Skip to content

Commit cb2feb3

Browse files
committed
属性自动尝试从父类中获取json语言资源
1 parent 7d3ac79 commit cb2feb3

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using Microsoft.Extensions.Localization;
77
using Microsoft.Extensions.Logging;
8+
89
using System.Collections.Concurrent;
910
using System.Globalization;
1011
using System.Reflection;
@@ -119,7 +120,28 @@ public override LocalizedString this[string name]
119120
}
120121
else
121122
{
122-
HandleMissingResourceItem(name);
123+
// 如果没有找到资源信息则尝试从父类中查找
124+
var type = Assembly.GetType(typeName);
125+
var propertyInfo = type?.GetPropertyByName(name);
126+
if (propertyInfo != null)
127+
{
128+
var baseType = propertyInfo.DeclaringType!;
129+
// 如果是父类属性则尝试从父类中查找
130+
if (baseType != type)
131+
{
132+
var baseAssembly = baseType.Assembly!;
133+
var localizerStrings2 = MegerResolveLocalizers(CacheManager.GetAllStringsByTypeName(baseAssembly, baseType.FullName!));
134+
135+
var l2 = localizerStrings2.Find(i => i.Name == name);
136+
if (l2 is { ResourceNotFound: false })
137+
{
138+
ret = l2.Value;
139+
}
140+
}
141+
142+
}
143+
if (ret is null)
144+
HandleMissingResourceItem(name);
123145
}
124146
}
125147
return ret;

0 commit comments

Comments
 (0)