Skip to content

Commit

Permalink
Merge pull request #3 from dotnet-campus/t/walterlv/interface
Browse files Browse the repository at this point in the history
修复生成的多语言类,缺少实现接口,导致运行时转换错误的问题
  • Loading branch information
SeWZC authored Jun 7, 2024
2 parents cf8a934 + 5e42c51 commit ba50837
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ public string ToImplementationCodeText(string rootNamespace, string ietfLanguage
var code = template.Content
.Replace($"namespace {template.Namespace};", $"namespace {rootNamespace}.Localizations;")
.Replace($"class {nameof(LocalizationValues)}", $"class {nameof(LocalizationValues)}_{typeName}")
.Replace(
$" : ILocalized_Root",
$" : ILocalized_Root{string.Concat(EnumerateConvertTreeNodeToInterfaceNames(Tree.Children).Select(x => $",\n ILocalized_Root_{x}"))}")
.Replace("""IetfLanguageTag => "default";""", $"""IetfLanguageTag => "{ietfLanguageTag}";""");
var lines = LocalizationItems.Select(x => ConvertKeyValueToValueCodeLine(x.Key, x.Value));
code = TemplateRegexes.FlagRegex.Replace(code, string.Concat(lines));
Expand All @@ -121,6 +124,26 @@ private string ConvertKeyValueToValueCodeLine(string key, string value)
return $"\n {{ \"{key}\", \"{value}\" }},";
}

private IEnumerable<string> EnumerateConvertTreeNodeToInterfaceNames(IEnumerable<LocalizationTreeNode> nodes)
{
foreach (var node in nodes)
{
if (node.Children.Count is 0)
{
// 叶子节点,不提供接口。
}
else
{
// 非叶子节点,提供接口。
yield return string.Join("_", node.FullIdentifierKey);
foreach (var child in EnumerateConvertTreeNodeToInterfaceNames(node.Children))
{
yield return child;
}
}
}
}

#endregion

#region Helpers
Expand Down

0 comments on commit ba50837

Please sign in to comment.