Skip to content

Commit

Permalink
Merge pull request #4 from dotnet-campus/t/lvyi/multi-line
Browse files Browse the repository at this point in the history
修复多行语言项导致注释和值不符合语法的错误
  • Loading branch information
SeWZC authored Jun 7, 2024
2 parents ba50837 + 5e0dcfd commit efc9da4
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Immutable;
using dotnetCampus.Localizations.Assets.Templates;
using dotnetCampus.Localizations.Utils.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using static dotnetCampus.Localizations.Generators.ModelProviding.IetfLanguageTagExtensions;

namespace dotnetCampus.Localizations.Generators.CodeTransforming;
Expand Down Expand Up @@ -67,7 +68,7 @@ private string RecursiveConvertLocalizationTreeNodeToKeyInterfaceCode(Localizati
{
return $"""
/// <summary>
/// {x.Item.SampleValue}
/// {ConvertValueToComment(x.Item.SampleValue)}
/// </summary>
LocalizedString {x.IdentifierKey} => this.Get0("{x.Item.Key}");
""";
Expand All @@ -77,7 +78,7 @@ private string RecursiveConvertLocalizationTreeNodeToKeyInterfaceCode(Localizati
var genericTypes = string.Join(", ", x.Item.ValueArgumentTypes);
return $"""
/// <summary>
/// {x.Item.SampleValue}
/// {ConvertValueToComment(x.Item.SampleValue)}
/// </summary>
LocalizedString<{genericTypes}> {x.IdentifierKey} => this.Get{x.Item.ValueArgumentTypes.Length}<{genericTypes}>("{x.Item.Key}");
""";
Expand All @@ -99,6 +100,17 @@ public interface ILocalized_{{nodeTypeName}} : ILocalizedStringProvider
""";
}

private string ConvertValueToComment(string? value)
{
if (string.IsNullOrEmpty(value) || !value.Contains('\n'))
{
return value ?? "";
}

var lines = value!.Replace("\r", "").Split('\n');
return string.Join("<br/>\n /// ", lines);
}

#endregion

#region Language Value Implementations
Expand All @@ -121,7 +133,8 @@ public string ToImplementationCodeText(string rootNamespace, string ietfLanguage

private string ConvertKeyValueToValueCodeLine(string key, string value)
{
return $"\n {{ \"{key}\", \"{value}\" }},";
var escapedValue = SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(value)).ToFullString();
return $"\n {{ \"{key}\", {escapedValue} }},";
}

private IEnumerable<string> EnumerateConvertTreeNodeToInterfaceNames(IEnumerable<LocalizationTreeNode> nodes)
Expand Down

0 comments on commit efc9da4

Please sign in to comment.