@@ -27,13 +27,18 @@ public partial class LocalizeSourceGenerator : IIncrementalGenerator
2727 private const string ClassName = "Localize" ;
2828 private const string PluginInterfaceName = "IPluginI18n" ;
2929 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" ;
3132 private const string XamlTag = "String" ;
33+ private const string KeyTag = "Key" ;
3234
33- private readonly Regex _languagesXamlRegex = new Regex ( @"\\Languages\\[^\\]+\.xaml$" , RegexOptions . IgnoreCase ) ;
35+ private static readonly Regex _languagesXamlRegex = new Regex ( @"\\Languages\\[^\\]+\.xaml$" , RegexOptions . IgnoreCase ) ;
3436
3537 private static readonly Version PackageVersion = typeof ( LocalizeSourceGenerator ) . Assembly . GetName ( ) . Version ;
3638
39+ private static readonly ImmutableArray < LocalizableString > _emptyLocalizableStrings = ImmutableArray < LocalizableString > . Empty ;
40+ private static readonly ImmutableArray < LocalizableStringParam > _emptyLocalizableStringParams = ImmutableArray < LocalizableStringParam > . Empty ;
41+
3742 #endregion
3843
3944 #region Incremental Generator
@@ -128,26 +133,60 @@ private static ImmutableArray<LocalizableString> ParseXamlFile(AdditionalText fi
128133 var content = file . GetText ( ct ) ? . ToString ( ) ;
129134 if ( content is null )
130135 {
131- return ImmutableArray < LocalizableString > . Empty ;
136+ return _emptyLocalizableStrings ;
132137 }
133138
134139 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 ) ;
137176 if ( systemNs is null || xNs is null )
138177 {
139- return ImmutableArray < LocalizableString > . Empty ;
178+ return _emptyLocalizableStrings ;
140179 }
141180
142181 var localizableStrings = new List < LocalizableString > ( ) ;
143182 foreach ( var element in doc . Descendants ( systemNs + XamlTag ) ) // "String" elements in system namespace
144183 {
145184 if ( ct . IsCancellationRequested )
146185 {
147- return ImmutableArray < LocalizableString > . Empty ;
186+ return _emptyLocalizableStrings ;
148187 }
149188
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
151190 var value = element . Value ;
152191 var comment = element . PreviousNode as XComment ;
153192
@@ -170,7 +209,7 @@ private static (string Summary, ImmutableArray<LocalizableStringParam> Parameter
170209 {
171210 if ( comment == null || comment . Value == null )
172211 {
173- return ( null , ImmutableArray < LocalizableStringParam > . Empty ) ;
212+ return ( null , _emptyLocalizableStringParams ) ;
174213 }
175214
176215 try
@@ -187,7 +226,7 @@ private static (string Summary, ImmutableArray<LocalizableStringParam> Parameter
187226 }
188227 catch
189228 {
190- return ( null , ImmutableArray < LocalizableStringParam > . Empty ) ;
229+ return ( null , _emptyLocalizableStringParams ) ;
191230 }
192231 }
193232
0 commit comments