Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<LocalizableString> _emptyLocalizableStrings = ImmutableArray<LocalizableString>.Empty;
private static readonly ImmutableArray<LocalizableStringParam> _emptyLocalizableStringParams = ImmutableArray<LocalizableStringParam>.Empty;

#endregion

#region Incremental Generator
Expand Down Expand Up @@ -128,26 +133,60 @@ private static ImmutableArray<LocalizableString> ParseXamlFile(AdditionalText fi
var content = file.GetText(ct)?.ToString();
if (content is null)
{
return ImmutableArray<LocalizableString>.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<LocalizableString>.Empty;
return _emptyLocalizableStrings;
}

var localizableStrings = new List<LocalizableString>();
foreach (var element in doc.Descendants(systemNs + XamlTag)) // "String" elements in system namespace
{
if (ct.IsCancellationRequested)
{
return ImmutableArray<LocalizableString>.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;

Expand All @@ -170,7 +209,7 @@ private static (string Summary, ImmutableArray<LocalizableStringParam> Parameter
{
if (comment == null || comment.Value == null)
{
return (null, ImmutableArray<LocalizableStringParam>.Empty);
return (null, _emptyLocalizableStringParams);
}

try
Expand All @@ -187,7 +226,7 @@ private static (string Summary, ImmutableArray<LocalizableStringParam> Parameter
}
catch
{
return (null, ImmutableArray<LocalizableStringParam>.Empty);
return (null, _emptyLocalizableStringParams);
}
}

Expand Down
Loading