Skip to content

Commit

Permalink
[NEW-FEATURE] We will need a way to provide information about culture…
Browse files Browse the repository at this point in the history
… for localized PLC strings (#248)

* Create draft PR for #247

* add methods and modifies to provide localized tranlsation usingmethods

* mend

---------

Co-authored-by: PTKu <PTKu@users.noreply.github.com>
Co-authored-by: Peter <61538034+PTKu@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 13, 2023
1 parent ad72d25 commit 4225ad2
Show file tree
Hide file tree
Showing 57 changed files with 1,192 additions and 208 deletions.
9 changes: 9 additions & 0 deletions docfx/articles/ixr/IXR.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ Ixr support all these special characters:
``!"#$'()*+,-.:;<=>?@[\]^_`{|}~€``
No other characters can be used.

## Localization in a client in server-side application

In order to get translation in given culture for a client in a server side application you should use methods `Get{ProperyName}(CultureInfo culture)` to get translation for client current culture. The properties will returned original string where localization tokens are removed.

~~~csharp
var notTranlsated = obj.AttributeName;
var translated = obj.GetAttribute(new CultureInfo("sk-SK"));
~~~

## Notes

Ixr is still in early development, so some features may be missing and bugs may occur.
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,11 @@ private void CreateITwinObjectImplementation()
$"public {typeof(ITwinObject).n()} GetParent() {{ return this.@Parent; }}" +
"public string Symbol { get; protected set; }" +
"private string _attributeName;" +
"public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : this.Translate(_attributeName).Interpolate(this); set => _attributeName = value; }" +
"public string HumanReadable { get; set; }" +
"public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : _attributeName.Interpolate(this).CleanUpLocalizationTokens(); set => _attributeName = value; }" +
"public System.String GetAttributeName(System.Globalization.CultureInfo culture) { return this.Translate(_attributeName, culture).Interpolate(this); }" +
"private string _humanReadable;" +
"public string HumanReadable { get => string.IsNullOrEmpty(_humanReadable) ? SymbolTail : _humanReadable.Interpolate(this).CleanUpLocalizationTokens(); set => _humanReadable = value; }" +
"public System.String GetHumanReadable(System.Globalization.CultureInfo culture) { return this.Translate(_humanReadable, culture); }" +
"protected System.String @SymbolTail { get; set;}" +
$"protected {typeof(ITwinObject).n()} @Parent {{ get; set; }}"+
$"public AXSharp.Connector.Localizations.Translator Interpreter => global::{Project.TargetProject.ProjectRootNamespace}.PlcTranslator.Instance;"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public override void AcceptVisitor(IAstVisitor visitor)
v.Product.Product = $"private {Type} _{Identifier};" +
$"\n{AccessQualifier} {Type} {Identifier} " +
$"{{ " +
$"get => string.IsNullOrEmpty(_{Identifier}) ? SymbolTail : this.Translate(_{Identifier}).Interpolate(this); set => _{Identifier} = value; " +
$"get => string.IsNullOrEmpty(_{Identifier}) ? SymbolTail : _{Identifier}.Interpolate(this).CleanUpLocalizationTokens(); set => _{Identifier} = value; " +
$"}}"+
$"{AccessQualifier} {Type} Get{Identifier}(System.Globalization.CultureInfo culture)" +
$"{{"+
$"return this.Translate(_{Identifier}, culture).Interpolate(this);" +
$"}}";
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void should_get_attribute_source()
[Fact]
public void should_declare_property()
{
var expected = "private string _SomeField;\npublic string SomeField { get => string.IsNullOrEmpty(_SomeField) ? SymbolTail : this.Translate(_SomeField).Interpolate(this); set => _SomeField = value; }";
var expected = "private string _SomeField;\npublic string SomeField { get => string.IsNullOrEmpty(_SomeField) ? SymbolTail : _SomeField.Interpolate(this).CleanUpLocalizationTokens(); set => _SomeField = value; }public string GetSomeField(System.Globalization.CultureInfo culture){return this.Translate(_SomeField, culture).Interpolate(this);}";
var field = NSubstitute.Substitute.For<ITypeDeclaration>();
field.Name.Returns("someField");
field.Pragmas.Returns(new ReadOnlyCollection<IPragma>(new IPragma[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using AXSharp.Connector;
using AXSharp.Connector.ValueTypes;
using System.Collections.Generic;
using System.Globalization;
using AXSharp.Connector.Localizations;


Expand Down Expand Up @@ -55,6 +56,16 @@ public void Poll()
throw new NotImplementedException();
}

public string GetAttributeName(CultureInfo culture)
{
return this.Translate(this.AttributeName, culture);
}

public string GetHumanReadable(CultureInfo culture)
{
return this.Translate(this.HumanReadable, culture);
}

public Translator Interpreter { get; }
public IEnumerable<ITwinObject> GetChildren()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,20 @@ public AXSharp.Connector.ITwinObject GetParent()
public string Symbol { get; protected set; }

private string _attributeName;
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : this.Translate(_attributeName).Interpolate(this); set => _attributeName = value; }
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : _attributeName.Interpolate(this).CleanUpLocalizationTokens(); set => _attributeName = value; }

public string HumanReadable { get; set; }
public System.String GetAttributeName(System.Globalization.CultureInfo culture)
{
return this.Translate(_attributeName, culture).Interpolate(this);
}

private string _humanReadable;
public string HumanReadable { get => string.IsNullOrEmpty(_humanReadable) ? SymbolTail : _humanReadable.Interpolate(this).CleanUpLocalizationTokens(); set => _humanReadable = value; }

public System.String GetHumanReadable(System.Globalization.CultureInfo culture)
{
return this.Translate(_humanReadable, culture);
}

protected System.String @SymbolTail { get; set; }

Expand Down Expand Up @@ -355,9 +366,20 @@ public AXSharp.Connector.ITwinObject GetParent()
public string Symbol { get; protected set; }

private string _attributeName;
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : this.Translate(_attributeName).Interpolate(this); set => _attributeName = value; }
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : _attributeName.Interpolate(this).CleanUpLocalizationTokens(); set => _attributeName = value; }

public string HumanReadable { get; set; }
public System.String GetAttributeName(System.Globalization.CultureInfo culture)
{
return this.Translate(_attributeName, culture).Interpolate(this);
}

private string _humanReadable;
public string HumanReadable { get => string.IsNullOrEmpty(_humanReadable) ? SymbolTail : _humanReadable.Interpolate(this).CleanUpLocalizationTokens(); set => _humanReadable = value; }

public System.String GetHumanReadable(System.Globalization.CultureInfo culture)
{
return this.Translate(_humanReadable, culture);
}

protected System.String @SymbolTail { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,20 @@ public AXSharp.Connector.ITwinObject GetParent()
public string Symbol { get; protected set; }

private string _attributeName;
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : this.Translate(_attributeName).Interpolate(this); set => _attributeName = value; }
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : _attributeName.Interpolate(this).CleanUpLocalizationTokens(); set => _attributeName = value; }

public string HumanReadable { get; set; }
public System.String GetAttributeName(System.Globalization.CultureInfo culture)
{
return this.Translate(_attributeName, culture).Interpolate(this);
}

private string _humanReadable;
public string HumanReadable { get => string.IsNullOrEmpty(_humanReadable) ? SymbolTail : _humanReadable.Interpolate(this).CleanUpLocalizationTokens(); set => _humanReadable = value; }

public System.String GetHumanReadable(System.Globalization.CultureInfo culture)
{
return this.Translate(_humanReadable, culture);
}

protected System.String @SymbolTail { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,20 @@ public AXSharp.Connector.ITwinObject GetParent()
public string Symbol { get; protected set; }

private string _attributeName;
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : this.Translate(_attributeName).Interpolate(this); set => _attributeName = value; }
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : _attributeName.Interpolate(this).CleanUpLocalizationTokens(); set => _attributeName = value; }

public string HumanReadable { get; set; }
public System.String GetAttributeName(System.Globalization.CultureInfo culture)
{
return this.Translate(_attributeName, culture).Interpolate(this);
}

private string _humanReadable;
public string HumanReadable { get => string.IsNullOrEmpty(_humanReadable) ? SymbolTail : _humanReadable.Interpolate(this).CleanUpLocalizationTokens(); set => _humanReadable = value; }

public System.String GetHumanReadable(System.Globalization.CultureInfo culture)
{
return this.Translate(_humanReadable, culture);
}

protected System.String @SymbolTail { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,20 @@ public AXSharp.Connector.ITwinObject GetParent()
public string Symbol { get; protected set; }

private string _attributeName;
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : this.Translate(_attributeName).Interpolate(this); set => _attributeName = value; }
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : _attributeName.Interpolate(this).CleanUpLocalizationTokens(); set => _attributeName = value; }

public string HumanReadable { get; set; }
public System.String GetAttributeName(System.Globalization.CultureInfo culture)
{
return this.Translate(_attributeName, culture).Interpolate(this);
}

private string _humanReadable;
public string HumanReadable { get => string.IsNullOrEmpty(_humanReadable) ? SymbolTail : _humanReadable.Interpolate(this).CleanUpLocalizationTokens(); set => _humanReadable = value; }

public System.String GetHumanReadable(System.Globalization.CultureInfo culture)
{
return this.Translate(_humanReadable, culture);
}

protected System.String @SymbolTail { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,20 @@ public AXSharp.Connector.ITwinObject GetParent()
public string Symbol { get; protected set; }

private string _attributeName;
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : this.Translate(_attributeName).Interpolate(this); set => _attributeName = value; }
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : _attributeName.Interpolate(this).CleanUpLocalizationTokens(); set => _attributeName = value; }

public string HumanReadable { get; set; }
public System.String GetAttributeName(System.Globalization.CultureInfo culture)
{
return this.Translate(_attributeName, culture).Interpolate(this);
}

private string _humanReadable;
public string HumanReadable { get => string.IsNullOrEmpty(_humanReadable) ? SymbolTail : _humanReadable.Interpolate(this).CleanUpLocalizationTokens(); set => _humanReadable = value; }

public System.String GetHumanReadable(System.Globalization.CultureInfo culture)
{
return this.Translate(_humanReadable, culture);
}

protected System.String @SymbolTail { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,20 @@ public AXSharp.Connector.ITwinObject GetParent()
public string Symbol { get; protected set; }

private string _attributeName;
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : this.Translate(_attributeName).Interpolate(this); set => _attributeName = value; }
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : _attributeName.Interpolate(this).CleanUpLocalizationTokens(); set => _attributeName = value; }

public string HumanReadable { get; set; }
public System.String GetAttributeName(System.Globalization.CultureInfo culture)
{
return this.Translate(_attributeName, culture).Interpolate(this);
}

private string _humanReadable;
public string HumanReadable { get => string.IsNullOrEmpty(_humanReadable) ? SymbolTail : _humanReadable.Interpolate(this).CleanUpLocalizationTokens(); set => _humanReadable = value; }

public System.String GetHumanReadable(System.Globalization.CultureInfo culture)
{
return this.Translate(_humanReadable, culture);
}

protected System.String @SymbolTail { get; set; }

Expand Down Expand Up @@ -588,9 +599,20 @@ public AXSharp.Connector.ITwinObject GetParent()
public string Symbol { get; protected set; }

private string _attributeName;
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : this.Translate(_attributeName).Interpolate(this); set => _attributeName = value; }
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : _attributeName.Interpolate(this).CleanUpLocalizationTokens(); set => _attributeName = value; }

public string HumanReadable { get; set; }
public System.String GetAttributeName(System.Globalization.CultureInfo culture)
{
return this.Translate(_attributeName, culture).Interpolate(this);
}

private string _humanReadable;
public string HumanReadable { get => string.IsNullOrEmpty(_humanReadable) ? SymbolTail : _humanReadable.Interpolate(this).CleanUpLocalizationTokens(); set => _humanReadable = value; }

public System.String GetHumanReadable(System.Globalization.CultureInfo culture)
{
return this.Translate(_humanReadable, culture);
}

protected System.String @SymbolTail { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,20 @@ public AXSharp.Connector.ITwinObject GetParent()
public string Symbol { get; protected set; }

private string _attributeName;
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : this.Translate(_attributeName).Interpolate(this); set => _attributeName = value; }
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : _attributeName.Interpolate(this).CleanUpLocalizationTokens(); set => _attributeName = value; }

public string HumanReadable { get; set; }
public System.String GetAttributeName(System.Globalization.CultureInfo culture)
{
return this.Translate(_attributeName, culture).Interpolate(this);
}

private string _humanReadable;
public string HumanReadable { get => string.IsNullOrEmpty(_humanReadable) ? SymbolTail : _humanReadable.Interpolate(this).CleanUpLocalizationTokens(); set => _humanReadable = value; }

public System.String GetHumanReadable(System.Globalization.CultureInfo culture)
{
return this.Translate(_humanReadable, culture);
}

protected System.String @SymbolTail { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,20 @@ public AXSharp.Connector.ITwinObject GetParent()
public string Symbol { get; protected set; }

private string _attributeName;
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : this.Translate(_attributeName).Interpolate(this); set => _attributeName = value; }
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : _attributeName.Interpolate(this).CleanUpLocalizationTokens(); set => _attributeName = value; }

public string HumanReadable { get; set; }
public System.String GetAttributeName(System.Globalization.CultureInfo culture)
{
return this.Translate(_attributeName, culture).Interpolate(this);
}

private string _humanReadable;
public string HumanReadable { get => string.IsNullOrEmpty(_humanReadable) ? SymbolTail : _humanReadable.Interpolate(this).CleanUpLocalizationTokens(); set => _humanReadable = value; }

public System.String GetHumanReadable(System.Globalization.CultureInfo culture)
{
return this.Translate(_humanReadable, culture);
}

protected System.String @SymbolTail { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,20 @@ public AXSharp.Connector.ITwinObject GetParent()
public string Symbol { get; protected set; }

private string _attributeName;
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : this.Translate(_attributeName).Interpolate(this); set => _attributeName = value; }
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : _attributeName.Interpolate(this).CleanUpLocalizationTokens(); set => _attributeName = value; }

public string HumanReadable { get; set; }
public System.String GetAttributeName(System.Globalization.CultureInfo culture)
{
return this.Translate(_attributeName, culture).Interpolate(this);
}

private string _humanReadable;
public string HumanReadable { get => string.IsNullOrEmpty(_humanReadable) ? SymbolTail : _humanReadable.Interpolate(this).CleanUpLocalizationTokens(); set => _humanReadable = value; }

public System.String GetHumanReadable(System.Globalization.CultureInfo culture)
{
return this.Translate(_humanReadable, culture);
}

protected System.String @SymbolTail { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,20 @@ public AXSharp.Connector.ITwinObject GetParent()
public string Symbol { get; protected set; }

private string _attributeName;
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : this.Translate(_attributeName).Interpolate(this); set => _attributeName = value; }
public System.String AttributeName { get => string.IsNullOrEmpty(_attributeName) ? SymbolTail : _attributeName.Interpolate(this).CleanUpLocalizationTokens(); set => _attributeName = value; }

public string HumanReadable { get; set; }
public System.String GetAttributeName(System.Globalization.CultureInfo culture)
{
return this.Translate(_attributeName, culture).Interpolate(this);
}

private string _humanReadable;
public string HumanReadable { get => string.IsNullOrEmpty(_humanReadable) ? SymbolTail : _humanReadable.Interpolate(this).CleanUpLocalizationTokens(); set => _humanReadable = value; }

public System.String GetHumanReadable(System.Globalization.CultureInfo culture)
{
return this.Translate(_humanReadable, culture);
}

protected System.String @SymbolTail { get; set; }

Expand Down
Loading

0 comments on commit 4225ad2

Please sign in to comment.