Skip to content

Commit

Permalink
[NEW-FEATURE] When an added property of string contains localization …
Browse files Browse the repository at this point in the history
…attributes it should use translator (#97)

* Create draft PR for #96

* [connector] temporarily removes localization tags from string attributes

---------

Co-authored-by: PTKu <[email protected]>
  • Loading branch information
PTKu and PTKu authored Feb 25, 2023
1 parent 4eca14f commit ab88f3b
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ public override void Init(AstContext context, ParseTreeNode treeNode)

public override void AcceptVisitor(IAstVisitor visitor)
{
if (visitor is PragmaVisitor v) v.Product = $"{AccessQualifier} {Type} {Identifier} {{ get; set; }}";
if (visitor is PragmaVisitor v)
{
if (Type.ToUpperInvariant() == "STRING")
{
v.Product = $"private {Type} _{Identifier};" +
$"\n{AccessQualifier} {Type} {Identifier} " +
$"{{ " +
$"get" +
$"{{ " +
$"return Ix.Localizations.LocalizationHelper.CleanUpLocalizationTokens(_{Identifier}); " +
$"}} " +
$"set; " +
$"{{_{Identifier} = value;" +
$"}} " +
$"}}";
}
else
{
v.Product = $"{AccessQualifier} {Type} {Identifier} {{ get; set; }}";
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public override void Init(AstContext context, ParseTreeNode treeNode)

public override void AcceptVisitor(IAstVisitor visitor)
{

if (visitor is PragmaVisitor v)
v.Product = MemberName != null
? $"{MemberName}.{PropertyName} = {InitValue};"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Ix.Compiler.Cs.Pragmas.PragmaParser;

internal class DeclarationAttributeAstNode : AstNode
internal class AttributeDeclarationAstNode : AstNode
{
public string? AttributeLiteral { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal class PragmaGrammar : Grammar
public readonly NonTerminal AddedPropertySetter = new(nameof(AddedPropertySetter), typeof(AddedPropertySetterAstNode));
public readonly Terminal assing = new(nameof(assing));

public readonly NonTerminal DeclarationAttribute = new(nameof(DeclarationAttribute), typeof(DeclarationAttributeAstNode));
public readonly NonTerminal DeclarationAttribute = new(nameof(DeclarationAttribute), typeof(AttributeDeclarationAstNode));
public readonly NonTerminal ClrAttribute = new(nameof(ClrAttribute));

public readonly FreeTextLiteral ClrAttributeContent =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void should_get_attribute_source()
[Fact]
public void should_declare_property()
{
var expected = "public string AttributeName { get; set; }";
var expected = "private string _AttributeName;\npublic string AttributeName { get{ return Ix.Localizations.LocalizationHelper.CleanUpLocalizationTokens(_AttributeName); } set; {_AttributeName = value;} }";
var field = new TypeMock("someField",
new ReadOnlyCollection<IPragma>(new IPragma[]
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,19 @@ public Ix.Connector.ITwinObject GetParent()

public partial class NoAccessModifierClass : Ix.Connector.ITwinObject
{
public string AttributeName { get; set; }
private string _AttributeName;
public string AttributeName
{
get
{
return Ix.Localizations.LocalizationHelper.CleanUpLocalizationTokens(_AttributeName);
}

set;
{
_AttributeName = value;
}
}

public OnlinerString SomeClassVariable { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@ namespace TypesWithPropertyAttributes
{
public partial class SomeAddedProperties : Ix.Connector.ITwinObject
{
public string Description { get; set; }
private string _Description;
public string Description
{
get
{
return Ix.Localizations.LocalizationHelper.CleanUpLocalizationTokens(_Description);
}

set;
{
_Description = value;
}
}

public OnlinerInt Counter { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ internal static IEnumerable<LocalizableItem> GetTranslatable(string input,
return localizables;
}

internal static string CleanUpLocalizationTokens(string localized)
public static string CleanUpLocalizationTokens(this string localized)
{
if (localized != null) return localized.Replace("<#", string.Empty).Replace("#>", string.Empty);

Expand Down

0 comments on commit ab88f3b

Please sign in to comment.