Skip to content

Commit 36fefa6

Browse files
authored
Merge pull request #491 from Inxton/fix-control-labels-and-values-not-translated-into-given-culture
fix control labels and values not translated into given culture
2 parents 0206923 + 45ad5e3 commit 36fefa6

File tree

10 files changed

+72
-23
lines changed

10 files changed

+72
-23
lines changed

src/AXSharp.blazor/src/AXSharp.Presentation.Blazor.Controls/Templates/TemplateBase.razor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ protected override Task OnInitializedAsync()
120120
/// <returns></returns>
121121
protected string GetLabel()
122122
{
123-
return Onliner.GetAttributeName(CultureInfo.CurrentUICulture) + (string.IsNullOrWhiteSpace(Onliner.AttributeUnits) ? null : $" [{Onliner.AttributeUnits}]");
123+
var retVal = Onliner.GetAttributeName(CultureInfo.CurrentUICulture) + (string.IsNullOrWhiteSpace(Onliner.AttributeUnits) ? null : $" [{Onliner.AttributeUnits}]");
124+
return retVal;
124125
}
125126
}
126127
}

src/AXSharp.compiler/src/AXSharp.Cs.Compiler/CsProject.cs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class CsProject : ITargetProject
3535
public CsProject(AXSharpProject AXSharpProject)
3636
{
3737
AxSharpProject = AXSharpProject;
38-
ProjectRootNamespace = MakeValidIdentifier(AXSharpProject.AxProject.ProjectInfo.Name);
38+
ProjectRootNamespace = GetProjectRootNamespace();
3939
}
4040

4141
private AXSharpProject AxSharpProject { get; }
@@ -97,6 +97,35 @@ private static string GetExecutingAssemblyPath()
9797
return assemblyPath!;
9898
}
9999

100+
/// <summary>
101+
/// Gets the project root namespace from the csproj file if defined, otherwise uses the project name.
102+
/// </summary>
103+
/// <returns>The root namespace for the project</returns>
104+
private string GetProjectRootNamespace()
105+
{
106+
try
107+
{
108+
if (File.Exists(CsProjectFile))
109+
{
110+
var xDocument = XDocument.Load(CsProjectFile);
111+
var rootNamespaceElement = xDocument.Descendants("RootNamespace").FirstOrDefault();
112+
113+
if (rootNamespaceElement != null && !string.IsNullOrWhiteSpace(rootNamespaceElement.Value))
114+
{
115+
return rootNamespaceElement.Value;
116+
}
117+
}
118+
}
119+
catch (Exception ex)
120+
{
121+
// Log the error but continue with fallback
122+
Log.Logger.Warning($"Failed to read RootNamespace from {CsProjectFile}: {ex.Message}");
123+
}
124+
125+
// Fallback to csproj filename (without extension) if RootNamespace is not defined
126+
return MakeValidIdentifier(Path.GetFileNameWithoutExtension(CsProjectFile));
127+
}
128+
100129

101130
private string EnsureOutputFolder()
102131
{
@@ -209,9 +238,9 @@ public static PlcTranslator Instance
209238
210239
private PlcTranslator()
211240
{{
212-
var defaultResourceType = Assembly.GetAssembly(typeof({this.ProjectRootNamespace}.PlcTranslator))
213-
.GetType(""{this.ProjectRootNamespace}.Resources.PlcStringResources"");
214-
this.SetLocalizationResource(defaultResourceType);
241+
var assembly = Assembly.GetAssembly(typeof({this.ProjectRootNamespace}.PlcTranslator));
242+
var resource = assembly.GetType(""{this.ProjectRootNamespace}.Resources.PlcStringResources"");
243+
this.SetLocalizationResource(resource, assembly);
215244
}}
216245
}}
217246
}}";

src/AXSharp.compiler/src/ixc/Properties/launchSettings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@
6565
"commandName": "Project",
6666
"commandLineArgs": "--skip-deps",
6767
"workingDirectory": "c:\\W\\Develop\\gh\\inxton\\simatic-ax\\ppppppsjsjsjs\\MTS258585\\ax\\"
68+
},
69+
"axopen-traversal-in-tepmplates": {
70+
"commandName": "Project",
71+
"workingDirectory": "C:\\W\\Develop\\gh\\inxton\\simatic-ax\\axopen.templates\\axopen\\src\\traversals\\apax"
6872
}
6973
}
7074
}

src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/Integration.Cs/IxProjectTests.IntegrationCs.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
namespace AXSharp.CompilerTests.Integration.Cs;
1717

18+
/*
19+
* C:\W\Develop\gh\inxton\simatic-ax\axsharp\src\AXSharp.compiler\tests\AXSharp.Compiler.CsTests\bin\Debug\net10.0\samples\units\samples\units\ix
20+
* C:\W\Develop\gh\inxton\simatic-ax\axsharp\src\AXSharp.compiler\tests\AXSharp.Compiler.CsTests\samples\units\expected
21+
*/
1822
public class IxProjectTestsAx : IxProjectTests
1923
{
2024

@@ -226,6 +230,10 @@ public void should_generate_all_even_when_fails_somewhere()
226230
[Fact]
227231
public void should_retrieve_dependencies_and_use_types_from_referenced_project()
228232
{
233+
/*
234+
* C:\W\Develop\gh\inxton\simatic-ax\axsharp\src\AXSharp.compiler\tests\integration\actual\app\samples\units\ix\ax\.g
235+
* C:\W\Develop\gh\inxton\simatic-ax\axsharp\src\AXSharp.compiler\tests\integration\expected\app\ix\.g
236+
*/
229237
var integrationProjectsPaths = new string[]
230238
{
231239
Path.GetFullPath(Path.Combine(testFolder, @"..\..\..\..\integration\actual\lib1")),

src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/ax/.g/PlcResources.g.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public static PlcTranslator Instance
1818

1919
private PlcTranslator()
2020
{
21-
var defaultResourceType = Assembly.GetAssembly(typeof(units.PlcTranslator))
22-
.GetType("units.Resources.PlcStringResources");
23-
this.SetLocalizationResource(defaultResourceType);
21+
var assembly = Assembly.GetAssembly(typeof(units.PlcTranslator));
22+
var resource = assembly.GetType("units.Resources.PlcStringResources");
23+
this.SetLocalizationResource(resource, assembly);
2424
}
2525
}
2626
}

src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/tia/.g/PlcResources.g.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public static PlcTranslator Instance
1818

1919
private PlcTranslator()
2020
{
21-
var defaultResourceType = Assembly.GetAssembly(typeof(units.PlcTranslator))
22-
.GetType("units.Resources.PlcStringResources");
23-
this.SetLocalizationResource(defaultResourceType);
21+
var assembly = Assembly.GetAssembly(typeof(units.PlcTranslator));
22+
var resource = assembly.GetType("units.Resources.PlcStringResources");
23+
this.SetLocalizationResource(resource, assembly);
2424
}
2525
}
2626
}

src/AXSharp.compiler/tests/integration/expected/app/ix/.g/PlcResources.g.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public static PlcTranslator Instance
1818

1919
private PlcTranslator()
2020
{
21-
var defaultResourceType = Assembly.GetAssembly(typeof(app.PlcTranslator))
22-
.GetType("app.Resources.PlcStringResources");
23-
this.SetLocalizationResource(defaultResourceType);
21+
var assembly = Assembly.GetAssembly(typeof(app.PlcTranslator));
22+
var resource = assembly.GetType("app.Resources.PlcStringResources");
23+
this.SetLocalizationResource(resource, assembly);
2424
}
2525
}
2626
}

src/AXSharp.connectors/src/AXSharp.Connector/Localizations/Translator.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public string Translate(string originalString, ITwinElement twin, CultureInfo cu
5252
/// Sets the localization resource for this translator.
5353
/// </summary>
5454
/// <param name="resourceType">Type of resource to be used.</param>
55-
public void SetLocalizationResource(Type resourceType)
55+
/// <param name="originAssembly"></param>
56+
public void SetLocalizationResource(Type resourceType, Assembly originAssembly = null)
5657
{
5758
if (resourceType != null)
5859
{
@@ -61,6 +62,10 @@ public void SetLocalizationResource(Type resourceType)
6162
IgnoreCase = true
6263
};
6364
}
65+
else
66+
{
67+
Console.WriteLine($"No resource type provided for `{originAssembly?.FullName}`");
68+
}
6469
}
6570

6671
internal static IEnumerable<string> GetTranslatable(string input,

src/AXSharp.connectors/tests/AXSharp.ConnectorTests/AXSharp.ConnectorTests/Localizations/TranslatorExtensionTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace AXSharp.ConnectorTests.Localizations
99
using Xunit;
1010
using NSubstitute;
1111
using AXSharp.Connector;
12+
using System.Reflection;
1213

1314
public static class TranslatorExtensionTests
1415
{
@@ -33,7 +34,7 @@ public static void Translate_localized_string_keep_default_translation()
3334
// Arrange
3435
var twin = Substitute.For<ITwinElement>();
3536
var interpreter = new Translator();
36-
interpreter.SetLocalizationResource(typeof(AXSharp.ConnectorTests.Localizations.Resources.Dictionary));
37+
interpreter.SetLocalizationResource(typeof(AXSharp.ConnectorTests.Localizations.Resources.Dictionary), Assembly.GetExecutingAssembly());
3738
twin.Interpreter.Returns(interpreter);
3839
var originalString = "<#In the middle of the night#>";
3940

@@ -70,7 +71,7 @@ public static void Translate_partially_localized_string_translate_to_sk()
7071
// Arrange
7172
var twin = Substitute.For<ITwinElement>();
7273
var interpreter = new Translator();
73-
interpreter.SetLocalizationResource(typeof(AXSharp.ConnectorTests.Localizations.Resources.Dictionary));
74+
interpreter.SetLocalizationResource(typeof(AXSharp.ConnectorTests.Localizations.Resources.Dictionary), Assembly.GetExecutingAssembly());
7475
twin.Interpreter.Returns(interpreter);
7576
var originalString = "(A4)<#In the middle of the night#> 1.5";
7677
var expected = "(A4)Uprostred noci 1.5";
@@ -110,7 +111,7 @@ public static void Translate_partially_localized_inexisting_translation()
110111
// Arrange
111112
var twin = Substitute.For<ITwinElement>();
112113
var interpreter = new Translator();
113-
interpreter.SetLocalizationResource(typeof(AXSharp.ConnectorTests.Localizations.Resources.Dictionary));
114+
interpreter.SetLocalizationResource(typeof(AXSharp.ConnectorTests.Localizations.Resources.Dictionary), Assembly.GetExecutingAssembly());
114115
twin.Interpreter.Returns(interpreter);
115116
var originalString = "(A4)<#In the middle of the night does not exist#> 1.5";
116117

src/AXSharp.connectors/tests/AXSharp.ConnectorTests/AXSharp.ConnectorTests/Localizations/TranslatorTests.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
namespace AXSharp.ConnectorTests.Localizations
22
{
3+
using AXSharp.Connector;
34
using AXSharp.Connector.Localizations;
5+
using AXSharp.ConnectorTests.Localizations.Resources;
6+
using NSubstitute;
47
using System;
58
using System.Globalization;
9+
using System.Reflection;
610
using Xunit;
7-
using NSubstitute;
8-
using AXSharp.Connector;
9-
using AXSharp.ConnectorTests.Localizations.Resources;
1011

1112
public class TranslatorTests
1213
{
@@ -54,7 +55,7 @@ public void CanCallSetLocalizationResource()
5455
var resourceType = typeof(AXSharp.ConnectorTests.Localizations.Resources.Dictionary);
5556

5657
// Act
57-
_testClass.SetLocalizationResource(resourceType);
58+
_testClass.SetLocalizationResource(resourceType, Assembly.GetExecutingAssembly());
5859
}
5960

6061
[Fact]
@@ -80,7 +81,7 @@ public void Translate_falls_back_to_library_resource_when_primary_does_not_have_
8081
// Arrange
8182
Translator.SetPrimaryTranslatorResource(typeof(OverrideApplication));
8283
var translator = new Translator();
83-
translator.SetLocalizationResource(typeof(OverrideLibrary));
84+
translator.SetLocalizationResource(typeof(OverrideLibrary), Assembly.GetExecutingAssembly());
8485
var twin = Substitute.For<ITwinElement>();
8586
var originalString = "<#Library only#>";
8687

0 commit comments

Comments
 (0)