diff --git a/Flow.Launcher.Localization.SourceGenerators/Localize/LocalizeSourceGenerator.cs b/Flow.Launcher.Localization.SourceGenerators/Localize/LocalizeSourceGenerator.cs index 1d48f6e..2b3da5a 100644 --- a/Flow.Launcher.Localization.SourceGenerators/Localize/LocalizeSourceGenerator.cs +++ b/Flow.Launcher.Localization.SourceGenerators/Localize/LocalizeSourceGenerator.cs @@ -27,13 +27,18 @@ public partial class LocalizeSourceGenerator : IIncrementalGenerator private const string ClassName = "Localize"; private const string PluginInterfaceName = "IPluginI18n"; private const string PluginContextTypeName = "PluginInitContext"; - private const string XamlPrefix = "system"; + private const string systemPrefixUri = "clr-namespace:System;assembly=mscorlib"; + private const string xamlPrefixUri = "http://schemas.microsoft.com/winfx/2006/xaml"; private const string XamlTag = "String"; + private const string KeyTag = "Key"; - private readonly Regex _languagesXamlRegex = new Regex(@"\\Languages\\[^\\]+\.xaml$", RegexOptions.IgnoreCase); + private static readonly Regex _languagesXamlRegex = new Regex(@"\\Languages\\[^\\]+\.xaml$", RegexOptions.IgnoreCase); private static readonly Version PackageVersion = typeof(LocalizeSourceGenerator).Assembly.GetName().Version; + private static readonly ImmutableArray _emptyLocalizableStrings = ImmutableArray.Empty; + private static readonly ImmutableArray _emptyLocalizableStringParams = ImmutableArray.Empty; + #endregion #region Incremental Generator @@ -128,15 +133,49 @@ private static ImmutableArray ParseXamlFile(AdditionalText fi var content = file.GetText(ct)?.ToString(); if (content is null) { - return ImmutableArray.Empty; + return _emptyLocalizableStrings; } var doc = XDocument.Parse(content); - var systemNs = doc.Root?.GetNamespaceOfPrefix(XamlPrefix); // Should be "system" - var xNs = doc.Root?.GetNamespaceOfPrefix("x"); + var root = doc.Root; + if (root is null) + { + return _emptyLocalizableStrings; + } + + // Find prefixes for the target URIs + string systemPrefix = null; + string xamlPrefix = null; + + foreach (var attr in root.Attributes()) + { + // Check if the attribute is a namespace declaration (xmlns:...) + if (attr.Name.NamespaceName == XNamespace.Xmlns.NamespaceName) + { + string uri = attr.Value; + string prefix = attr.Name.LocalName; + + if (uri == systemPrefixUri) + { + systemPrefix = prefix; + } + else if (uri == xamlPrefixUri) + { + xamlPrefix = prefix; + } + } + } + + if (systemPrefix is null || xamlPrefix is null) + { + return _emptyLocalizableStrings; + } + + var systemNs = doc.Root?.GetNamespaceOfPrefix(systemPrefix); + var xNs = doc.Root?.GetNamespaceOfPrefix(xamlPrefix); if (systemNs is null || xNs is null) { - return ImmutableArray.Empty; + return _emptyLocalizableStrings; } var localizableStrings = new List(); @@ -144,10 +183,10 @@ private static ImmutableArray ParseXamlFile(AdditionalText fi { if (ct.IsCancellationRequested) { - return ImmutableArray.Empty; + return _emptyLocalizableStrings; } - var key = element.Attribute(xNs + "Key")?.Value; // Correctly get x:Key + var key = element.Attribute(xNs + KeyTag)?.Value; // "Key" attribute in xaml namespace var value = element.Value; var comment = element.PreviousNode as XComment; @@ -170,7 +209,7 @@ private static (string Summary, ImmutableArray Parameter { if (comment == null || comment.Value == null) { - return (null, ImmutableArray.Empty); + return (null, _emptyLocalizableStringParams); } try @@ -187,7 +226,7 @@ private static (string Summary, ImmutableArray Parameter } catch { - return (null, ImmutableArray.Empty); + return (null, _emptyLocalizableStringParams); } }