Skip to content

Commit ba50837

Browse files
authored
Merge pull request #3 from dotnet-campus/t/walterlv/interface
修复生成的多语言类,缺少实现接口,导致运行时转换错误的问题
2 parents cf8a934 + 5e42c51 commit ba50837

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/dotnetCampus.Localizations.Analyzer/Generators/CodeTransforming/LocalizationCodeTransformer.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ public string ToImplementationCodeText(string rootNamespace, string ietfLanguage
110110
var code = template.Content
111111
.Replace($"namespace {template.Namespace};", $"namespace {rootNamespace}.Localizations;")
112112
.Replace($"class {nameof(LocalizationValues)}", $"class {nameof(LocalizationValues)}_{typeName}")
113+
.Replace(
114+
$" : ILocalized_Root",
115+
$" : ILocalized_Root{string.Concat(EnumerateConvertTreeNodeToInterfaceNames(Tree.Children).Select(x => $",\n ILocalized_Root_{x}"))}")
113116
.Replace("""IetfLanguageTag => "default";""", $"""IetfLanguageTag => "{ietfLanguageTag}";""");
114117
var lines = LocalizationItems.Select(x => ConvertKeyValueToValueCodeLine(x.Key, x.Value));
115118
code = TemplateRegexes.FlagRegex.Replace(code, string.Concat(lines));
@@ -121,6 +124,26 @@ private string ConvertKeyValueToValueCodeLine(string key, string value)
121124
return $"\n {{ \"{key}\", \"{value}\" }},";
122125
}
123126

127+
private IEnumerable<string> EnumerateConvertTreeNodeToInterfaceNames(IEnumerable<LocalizationTreeNode> nodes)
128+
{
129+
foreach (var node in nodes)
130+
{
131+
if (node.Children.Count is 0)
132+
{
133+
// 叶子节点,不提供接口。
134+
}
135+
else
136+
{
137+
// 非叶子节点,提供接口。
138+
yield return string.Join("_", node.FullIdentifierKey);
139+
foreach (var child in EnumerateConvertTreeNodeToInterfaceNames(node.Children))
140+
{
141+
yield return child;
142+
}
143+
}
144+
}
145+
}
146+
124147
#endregion
125148

126149
#region Helpers

0 commit comments

Comments
 (0)