Multi-Language Add-In for Visual Studio

Multi-Language Add-In for Visual Studio

XAML Localization with a markup extension


In previous versions of Multi-Language, localization of XAML files was implemeted using the markup extension x:Static, for example:

XAML Localization with x:Static
Content="{x:Static res:Resources.Apply}"

Language switching at run time was implemeted - identically to Windows Forms projects - by generating a function named ml_UpdateControls and calling it from an event handler.

This does not work if you try to localize a string in a DataTemplate in XAML. In this case, Multi-Language would generate code in the function ml_UpdateControls which would not compile.

Changing the text in a DataTemplate at run time is difficult problem. The whole idea of a template is that it may be instantiated multiple times. If you want to change it at run time, then you have to update each place where the template is used.

This is probably not possible in a simple function like ml_UpdateControls.

Starting with version 6.03, I have made a significant change to the localization in XAML files.

Multi-Language now adds a new file to the project, called MLExtension.cs or MLExtension.vb, which contains a markup extension named LangExtension in the namespace MultiLanguageMarkup.

In each XAML file, Multi-Language adds a namespace definition:

Namespace definition

Texts are now localized using the new markup extension, for example:

XAML Localization with m:Lang
Content="{m:Lang Apply}"

This instantiates the markup extension LangExtension and passes the name of the resource as a parameter to its constructor.

Switching the language at run time is handled in the markup extension.

The markup extension maintains a list of all the places where it is used. In most cases this list will contain one item, but if the markup extension is used in a DataTemplate, then it will contain one item for each time the template is used.

The markup extension is added to your project as a source file, so you can read how it works if it interests you. I have tried to keep it as simple as possible, while doing the necessary work, and provided plenty of comments.

Because language switching is now handled in the markup extension, the function ml_UpdateControls is not necessary, and is no longer generated for XAML files.

When you add support for switching language at run time, Multi-Language adds the project MLRuntime (as before) and makes a minor change to file containing the markup extension. It simple replaces the conditional compilation statement #if false with #if true, to activate the code which attaches to the language changed event handler.

(If language switching does not work, you might start by checking that this change was actually made. biggrin)

When you use Multi-Language version 6.03 it will automatically:

  • update the previous localization to use m:Lang style
  • remove the function ml_UpdateControls

The previous method is no longer supported.

Finally, this change also applies to Silverlight projects, but I have not tested it at all.

If anybody is actually using Multi-Language with Silverlight and has problems, please let me know.