One of the features we’ve been most waiting for in Silverlight is the support for Markup Extensions, which WPF had since day one.
In order to support localization, we can create a markup extension like so:
This markup extension relies on an auto-generated class named translations, which gets created automatically when we create a new resource dictionary (*.resx) file:
This is where we can put all the resources for English, and create additional resource files with the extension of the relevant culture. For Hebrew for example, we’ll use the name Translations.he.resx . For Frech, Translations.fr.resx:
In each resx files we can write pairs ok Keys and Values for the localized strings:
To use this translation extension, all we do is write the following in XAML:
And it will display different translations based on the Culture that is set inside the TranslationExtension. This is usually set only once inside app.xaml.cs, before setting the Root Visual:
There’s just one last issue to note here. In order for the resources to load correctly, we have to add the following line to the <MyProject>.csproj file:
Inside the <PropertyGroup> tag, and everything will work as expected.
That last bit alluded me for quite a while, until the holy man of .net, Shlomo Goldberg helped me figure it out. (he actually figured it out – I just blogged about it )