Quick way of checking if you’ve localised your Silverlight or WPF Xaml files

I typically use data binding to perform localisation of labels but I typically start out just by having plain text and go back later and replace these with bindings at a later stage.

e.g. <TextBlock Text=”{Binding Strings.Label_ContactDetails}” />

Here’s a really quick and easy way to make sure you’ve got your Xaml files completely localised.

1. Make sure you have downloaded LINQPad and installed it.

2. Start LINQPad and change the Language dropdown to C# Statement(s)

3. Paste this query into LINQPad and change the path to point to your project or solution folder

Code Snippet
  1. string path = @"C:\MyProjectFolder";
  2. XNamespace ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;;
  4. var attributes = new List<XName> { "Text", "Tag", "Content", "Header", "ToolTipService.ToolTip" };
  6. var files = System.IO.Directory.GetFiles(path, "*.xaml", System.IO.SearchOption.AllDirectories);
  7. var q = from eachFile in files
  8.         let fileContent = XElement.Load(eachFile)
  9.         from eachNode in fileContent.Descendants()
  10.         from eachAttribute in eachNode.Attributes()
  11.         where attributes.Contains(eachAttribute.Name)
  12.         where !eachAttribute.Value.StartsWith("{")
  13.         select new
  14.         {
  15.             Text = eachAttribute.Value,
  16.             Node = eachNode.Name.LocalName,
  17.             Attribute = eachAttribute.Name,
  18.             FileName = eachFile.Substring(path.Length)
  19.         };
  21. q.Dump();


4. Click Run and viola you have a list of all the nodes in all Xaml files that need localising!

Hope this helps!

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: