Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ protected override Task OnInitializedAsync()
/// <returns></returns>
protected string GetLabel()
{
return Onliner.GetAttributeName(CultureInfo.CurrentUICulture) + (string.IsNullOrWhiteSpace(Onliner.AttributeUnits) ? null : $" [{Onliner.AttributeUnits}]");
var retVal = Onliner.GetAttributeName(CultureInfo.CurrentUICulture) + (string.IsNullOrWhiteSpace(Onliner.AttributeUnits) ? null : $" [{Onliner.AttributeUnits}]");
return retVal;
}
}
}
37 changes: 33 additions & 4 deletions src/AXSharp.compiler/src/AXSharp.Cs.Compiler/CsProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class CsProject : ITargetProject
public CsProject(AXSharpProject AXSharpProject)
{
AxSharpProject = AXSharpProject;
ProjectRootNamespace = MakeValidIdentifier(AXSharpProject.AxProject.ProjectInfo.Name);
ProjectRootNamespace = GetProjectRootNamespace();
}

private AXSharpProject AxSharpProject { get; }
Expand Down Expand Up @@ -97,6 +97,35 @@ private static string GetExecutingAssemblyPath()
return assemblyPath!;
}

/// <summary>
/// Gets the project root namespace from the csproj file if defined, otherwise uses the project name.
/// </summary>
/// <returns>The root namespace for the project</returns>
private string GetProjectRootNamespace()
{
try
{
if (File.Exists(CsProjectFile))
{
var xDocument = XDocument.Load(CsProjectFile);
var rootNamespaceElement = xDocument.Descendants("RootNamespace").FirstOrDefault();

if (rootNamespaceElement != null && !string.IsNullOrWhiteSpace(rootNamespaceElement.Value))
{
return rootNamespaceElement.Value;
}
}
}
catch (Exception ex)
{
// Log the error but continue with fallback
Log.Logger.Warning($"Failed to read RootNamespace from {CsProjectFile}: {ex.Message}");
}

// Fallback to csproj filename (without extension) if RootNamespace is not defined
return MakeValidIdentifier(Path.GetFileNameWithoutExtension(CsProjectFile));
}


private string EnsureOutputFolder()
{
Expand Down Expand Up @@ -209,9 +238,9 @@ public static PlcTranslator Instance

private PlcTranslator()
{{
var defaultResourceType = Assembly.GetAssembly(typeof({this.ProjectRootNamespace}.PlcTranslator))
.GetType(""{this.ProjectRootNamespace}.Resources.PlcStringResources"");
this.SetLocalizationResource(defaultResourceType);
var assembly = Assembly.GetAssembly(typeof({this.ProjectRootNamespace}.PlcTranslator));
var resource = assembly.GetType(""{this.ProjectRootNamespace}.Resources.PlcStringResources"");
this.SetLocalizationResource(resource, assembly);
}}
}}
}}";
Expand Down
4 changes: 4 additions & 0 deletions src/AXSharp.compiler/src/ixc/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
"commandName": "Project",
"commandLineArgs": "--skip-deps",
"workingDirectory": "c:\\W\\Develop\\gh\\inxton\\simatic-ax\\ppppppsjsjsjs\\MTS258585\\ax\\"
},
"axopen-traversal-in-tepmplates": {
"commandName": "Project",
"workingDirectory": "C:\\W\\Develop\\gh\\inxton\\simatic-ax\\axopen.templates\\axopen\\src\\traversals\\apax"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

namespace AXSharp.CompilerTests.Integration.Cs;

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

Expand Down Expand Up @@ -226,6 +230,10 @@ public void should_generate_all_even_when_fails_somewhere()
[Fact]
public void should_retrieve_dependencies_and_use_types_from_referenced_project()
{
/*
* C:\W\Develop\gh\inxton\simatic-ax\axsharp\src\AXSharp.compiler\tests\integration\actual\app\samples\units\ix\ax\.g
* C:\W\Develop\gh\inxton\simatic-ax\axsharp\src\AXSharp.compiler\tests\integration\expected\app\ix\.g
*/
var integrationProjectsPaths = new string[]
{
Path.GetFullPath(Path.Combine(testFolder, @"..\..\..\..\integration\actual\lib1")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public static PlcTranslator Instance

private PlcTranslator()
{
var defaultResourceType = Assembly.GetAssembly(typeof(units.PlcTranslator))
.GetType("units.Resources.PlcStringResources");
this.SetLocalizationResource(defaultResourceType);
var assembly = Assembly.GetAssembly(typeof(units.PlcTranslator));
var resource = assembly.GetType("units.Resources.PlcStringResources");
this.SetLocalizationResource(resource, assembly);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public static PlcTranslator Instance

private PlcTranslator()
{
var defaultResourceType = Assembly.GetAssembly(typeof(units.PlcTranslator))
.GetType("units.Resources.PlcStringResources");
this.SetLocalizationResource(defaultResourceType);
var assembly = Assembly.GetAssembly(typeof(units.PlcTranslator));
var resource = assembly.GetType("units.Resources.PlcStringResources");
this.SetLocalizationResource(resource, assembly);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public static PlcTranslator Instance

private PlcTranslator()
{
var defaultResourceType = Assembly.GetAssembly(typeof(app.PlcTranslator))
.GetType("app.Resources.PlcStringResources");
this.SetLocalizationResource(defaultResourceType);
var assembly = Assembly.GetAssembly(typeof(app.PlcTranslator));
var resource = assembly.GetType("app.Resources.PlcStringResources");
this.SetLocalizationResource(resource, assembly);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public string Translate(string originalString, ITwinElement twin, CultureInfo cu
/// Sets the localization resource for this translator.
/// </summary>
/// <param name="resourceType">Type of resource to be used.</param>
public void SetLocalizationResource(Type resourceType)
/// <param name="originAssembly"></param>
public void SetLocalizationResource(Type resourceType, Assembly originAssembly = null)
{
if (resourceType != null)
{
Expand All @@ -61,6 +62,10 @@ public void SetLocalizationResource(Type resourceType)
IgnoreCase = true
};
}
else
{
Console.WriteLine($"No resource type provided for `{originAssembly?.FullName}`");
}
}

internal static IEnumerable<string> GetTranslatable(string input,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace AXSharp.ConnectorTests.Localizations
using Xunit;
using NSubstitute;
using AXSharp.Connector;
using System.Reflection;

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

Expand Down Expand Up @@ -70,7 +71,7 @@ public static void Translate_partially_localized_string_translate_to_sk()
// Arrange
var twin = Substitute.For<ITwinElement>();
var interpreter = new Translator();
interpreter.SetLocalizationResource(typeof(AXSharp.ConnectorTests.Localizations.Resources.Dictionary));
interpreter.SetLocalizationResource(typeof(AXSharp.ConnectorTests.Localizations.Resources.Dictionary), Assembly.GetExecutingAssembly());
twin.Interpreter.Returns(interpreter);
var originalString = "(A4)<#In the middle of the night#> 1.5";
var expected = "(A4)Uprostred noci 1.5";
Expand Down Expand Up @@ -110,7 +111,7 @@ public static void Translate_partially_localized_inexisting_translation()
// Arrange
var twin = Substitute.For<ITwinElement>();
var interpreter = new Translator();
interpreter.SetLocalizationResource(typeof(AXSharp.ConnectorTests.Localizations.Resources.Dictionary));
interpreter.SetLocalizationResource(typeof(AXSharp.ConnectorTests.Localizations.Resources.Dictionary), Assembly.GetExecutingAssembly());
twin.Interpreter.Returns(interpreter);
var originalString = "(A4)<#In the middle of the night does not exist#> 1.5";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
namespace AXSharp.ConnectorTests.Localizations
{
using AXSharp.Connector;
using AXSharp.Connector.Localizations;
using AXSharp.ConnectorTests.Localizations.Resources;
using NSubstitute;
using System;
using System.Globalization;
using System.Reflection;
using Xunit;
using NSubstitute;
using AXSharp.Connector;
using AXSharp.ConnectorTests.Localizations.Resources;

public class TranslatorTests
{
Expand Down Expand Up @@ -54,7 +55,7 @@ public void CanCallSetLocalizationResource()
var resourceType = typeof(AXSharp.ConnectorTests.Localizations.Resources.Dictionary);

// Act
_testClass.SetLocalizationResource(resourceType);
_testClass.SetLocalizationResource(resourceType, Assembly.GetExecutingAssembly());
}

[Fact]
Expand All @@ -80,7 +81,7 @@ public void Translate_falls_back_to_library_resource_when_primary_does_not_have_
// Arrange
Translator.SetPrimaryTranslatorResource(typeof(OverrideApplication));
var translator = new Translator();
translator.SetLocalizationResource(typeof(OverrideLibrary));
translator.SetLocalizationResource(typeof(OverrideLibrary), Assembly.GetExecutingAssembly());
var twin = Substitute.For<ITwinElement>();
var originalString = "<#Library only#>";

Expand Down