Skip to content

Commit 1282168

Browse files
committed
Refactor localization resource handling to improve clarity and fallback logic for project root namespace
1 parent f342e1c commit 1282168

2 files changed

Lines changed: 36 additions & 7 deletions

File tree

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.connectors/src/AXSharp.Connector/Localizations/Translator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +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-
/// <param name="targetAssembly"></param>
56-
public void SetLocalizationResource(Type resourceType, Assembly targetAssembly = null)
55+
/// <param name="originAssembly"></param>
56+
public void SetLocalizationResource(Type resourceType, Assembly originAssembly = null)
5757
{
5858
if (resourceType != null)
5959
{
@@ -64,7 +64,7 @@ public void SetLocalizationResource(Type resourceType, Assembly targetAssembly =
6464
}
6565
else
6666
{
67-
Console.WriteLine($"No resource type provided for `{targetAssembly?.FullName}`");
67+
Console.WriteLine($"No resource type provided for `{originAssembly?.FullName}`");
6868
}
6969
}
7070

0 commit comments

Comments
 (0)