Skip to content

Commit 880663e

Browse files
committed
Refactor string literal localization extraction logic due to broke compatibility stc
- Replace AddToDictionaryIfLocalizedString with AddToDictionaryIfLocalizedStringInLiterals for improved string literal processing. - Only process string literals in Generate(); pragma extraction is now disabled. - Add IsStringLiteral helper to identify string literal nodes. - Remove obsolete and commented-out code for clarity. - Update working directory for "app-withref" in launchSettings.json.
1 parent 77ed62b commit 880663e

2 files changed

Lines changed: 57 additions & 8 deletions

File tree

src/AXSharp.compiler/src/ixr/Program.cs

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ void Generate(Options o)
8383
foreach (var syntaxTree in syntaxTrees)
8484
{
8585
IterateSyntaxTreeForStringLiterals(syntaxTree.GetRoot(),lw, Path.GetRelativePath(axProjectFolder, syntaxTree.Filename));
86-
8786
IterateSyntaxTreeForPragmas(syntaxTree.GetRoot(), lw, Path.GetRelativePath(axProjectFolder, syntaxTree.Filename));
8887
}
8988

@@ -93,12 +92,11 @@ void Generate(Options o)
9392

9493
void IterateSyntaxTreeForStringLiterals(ISyntaxNode root, LocalizedStringWrapper lw, string fileName)
9594
{
96-
//foreach (var literalSyntax in GetChildNodesRecursive(root).OfType<ILiteralSyntax>())
97-
//{
98-
// var token = literalSyntax.Tokens.First();
99-
// //literalSyntax.Location
100-
// AddToDictionaryIfLocalizedString(token,lw,fileName);
101-
//}
95+
foreach (var literalSyntax in GetChildNodesRecursive(root).OfType<ILiteralSyntax>())
96+
{
97+
var token = literalSyntax.Tokens.First();
98+
AddToDictionaryIfLocalizedStringInLiterals(literalSyntax, lw, fileName);
99+
}
102100
}
103101

104102

@@ -147,6 +145,44 @@ void AddToDictionaryIfLocalizedString(PragmaSyntax token, LocalizedStringWrapper
147145
}
148146
}
149147
}
148+
149+
void AddToDictionaryIfLocalizedStringInLiterals(ILiteralSyntax literal, LocalizedStringWrapper lw, string fileName)
150+
{
151+
// if is valid token
152+
// if (IsStringLiteral(literal) || true)
153+
{
154+
foreach (var token in literal.Tokens)
155+
{
156+
157+
158+
// try to acquire localized string
159+
var localizedStringList = lw.TryToGetLocalizedStrings(token.FullText);
160+
161+
if (localizedStringList == null)
162+
{
163+
return;
164+
}
165+
166+
foreach (string localizedString in localizedStringList)
167+
{
168+
//get raw text from localized string
169+
var rawText = lw.GetRawTextFromLocalizedString(localizedString);
170+
171+
//create id
172+
var id = AXSharp.Connector.Localizations.LocalizationHelper.CreateId(rawText);
173+
174+
//check if identifier is valid
175+
if (lw.IsValidId(id))
176+
{
177+
var pos = token.Location.GetLineSpan().StartLinePosition;
178+
var wrapper = new StringValueWrapper(rawText, fileName, pos.Line);
179+
// add id and wrapper to dictionary
180+
lw.LocalizedStringsDictionary.TryAdd(id, wrapper);
181+
}
182+
}
183+
}
184+
}
185+
}
150186
bool IsPragmaToken(PragmaSyntax token)
151187
{
152188
//if(token.SyntaxKind == SyntaxKind.PragmaToken)
@@ -168,6 +204,19 @@ bool IsStringToken(PragmaSyntax token)
168204
return false;
169205
}
170206

207+
208+
bool IsStringLiteral(ILiteralSyntax literal)
209+
{
210+
if (literal.SyntaxKind == SyntaxKind.TypedStringDToken ||
211+
literal.SyntaxKind == SyntaxKind.TypedStringSToken ||
212+
literal.SyntaxKind == SyntaxKind.UntypedStringDToken ||
213+
literal.SyntaxKind == SyntaxKind.UntypedStringSToken)
214+
{
215+
return true;
216+
}
217+
return false;
218+
}
219+
171220
IEnumerable<ISyntaxNode> GetChildNodesRecursive(ISyntaxNode syntaxNode)
172221
{
173222
yield return syntaxNode;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"app-withref": {
1111
"commandName": "Project",
12-
"workingDirectory": "c:\\W\\Develop\\gh\\inxton\\axopen.templates\\templates.simple\\app\\"
12+
"workingDirectory": "c:\\W\\Develop\\gh\\inxton\\simatic-ax\\axopen.templates\\axopen.template.simple\\ax\\"
1313
}
1414
}
1515
}

0 commit comments

Comments
 (0)