@@ -27,13 +27,18 @@ public partial class LocalizeSourceGenerator : IIncrementalGenerator
27
27
private const string ClassName = "Localize" ;
28
28
private const string PluginInterfaceName = "IPluginI18n" ;
29
29
private const string PluginContextTypeName = "PluginInitContext" ;
30
- private const string XamlPrefix = "system" ;
30
+ private const string systemPrefixUri = "clr-namespace:System;assembly=mscorlib" ;
31
+ private const string xamlPrefixUri = "http://schemas.microsoft.com/winfx/2006/xaml" ;
31
32
private const string XamlTag = "String" ;
33
+ private const string KeyTag = "Key" ;
32
34
33
- private readonly Regex _languagesXamlRegex = new Regex ( @"\\Languages\\[^\\]+\.xaml$" , RegexOptions . IgnoreCase ) ;
35
+ private static readonly Regex _languagesXamlRegex = new Regex ( @"\\Languages\\[^\\]+\.xaml$" , RegexOptions . IgnoreCase ) ;
34
36
35
37
private static readonly Version PackageVersion = typeof ( LocalizeSourceGenerator ) . Assembly . GetName ( ) . Version ;
36
38
39
+ private static readonly ImmutableArray < LocalizableString > _emptyLocalizableStrings = ImmutableArray < LocalizableString > . Empty ;
40
+ private static readonly ImmutableArray < LocalizableStringParam > _emptyLocalizableStringParams = ImmutableArray < LocalizableStringParam > . Empty ;
41
+
37
42
#endregion
38
43
39
44
#region Incremental Generator
@@ -128,26 +133,60 @@ private static ImmutableArray<LocalizableString> ParseXamlFile(AdditionalText fi
128
133
var content = file . GetText ( ct ) ? . ToString ( ) ;
129
134
if ( content is null )
130
135
{
131
- return ImmutableArray < LocalizableString > . Empty ;
136
+ return _emptyLocalizableStrings ;
132
137
}
133
138
134
139
var doc = XDocument . Parse ( content ) ;
135
- var systemNs = doc . Root ? . GetNamespaceOfPrefix ( XamlPrefix ) ; // Should be "system"
136
- var xNs = doc . Root ? . GetNamespaceOfPrefix ( "x" ) ;
140
+ var root = doc . Root ;
141
+ if ( root is null )
142
+ {
143
+ return _emptyLocalizableStrings ;
144
+ }
145
+
146
+ // Find prefixes for the target URIs
147
+ string systemPrefix = null ;
148
+ string xamlPrefix = null ;
149
+
150
+ foreach ( var attr in root . Attributes ( ) )
151
+ {
152
+ // Check if the attribute is a namespace declaration (xmlns:...)
153
+ if ( attr . Name . NamespaceName == XNamespace . Xmlns . NamespaceName )
154
+ {
155
+ string uri = attr . Value ;
156
+ string prefix = attr . Name . LocalName ;
157
+
158
+ if ( uri == systemPrefixUri )
159
+ {
160
+ systemPrefix = prefix ;
161
+ }
162
+ else if ( uri == xamlPrefixUri )
163
+ {
164
+ xamlPrefix = prefix ;
165
+ }
166
+ }
167
+ }
168
+
169
+ if ( systemPrefix is null || xamlPrefix is null )
170
+ {
171
+ return _emptyLocalizableStrings ;
172
+ }
173
+
174
+ var systemNs = doc . Root ? . GetNamespaceOfPrefix ( systemPrefix ) ;
175
+ var xNs = doc . Root ? . GetNamespaceOfPrefix ( xamlPrefix ) ;
137
176
if ( systemNs is null || xNs is null )
138
177
{
139
- return ImmutableArray < LocalizableString > . Empty ;
178
+ return _emptyLocalizableStrings ;
140
179
}
141
180
142
181
var localizableStrings = new List < LocalizableString > ( ) ;
143
182
foreach ( var element in doc . Descendants ( systemNs + XamlTag ) ) // "String" elements in system namespace
144
183
{
145
184
if ( ct . IsCancellationRequested )
146
185
{
147
- return ImmutableArray < LocalizableString > . Empty ;
186
+ return _emptyLocalizableStrings ;
148
187
}
149
188
150
- var key = element . Attribute ( xNs + "Key" ) ? . Value ; // Correctly get x:Key
189
+ var key = element . Attribute ( xNs + KeyTag ) ? . Value ; // "Key" attribute in xaml namespace
151
190
var value = element . Value ;
152
191
var comment = element . PreviousNode as XComment ;
153
192
@@ -170,7 +209,7 @@ private static (string Summary, ImmutableArray<LocalizableStringParam> Parameter
170
209
{
171
210
if ( comment == null || comment . Value == null )
172
211
{
173
- return ( null , ImmutableArray < LocalizableStringParam > . Empty ) ;
212
+ return ( null , _emptyLocalizableStringParams ) ;
174
213
}
175
214
176
215
try
@@ -187,7 +226,7 @@ private static (string Summary, ImmutableArray<LocalizableStringParam> Parameter
187
226
}
188
227
catch
189
228
{
190
- return ( null , ImmutableArray < LocalizableStringParam > . Empty ) ;
229
+ return ( null , _emptyLocalizableStringParams ) ;
191
230
}
192
231
}
193
232
0 commit comments