Silverlight 5 MarkupExtensions –Localization just got a whole lot easier

8 ביוני 2011

2 תגובות

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:

   1: public class TranslationExtension : IMarkupExtension<string>

   2: {

   3:     public string Key { get; set; }


   5:     public TranslationExtension()

   6:     {

   7:         Key = String.Empty;

   8:     }


  10:     public override string ToString()

  11:     {

  12:         return ProvideValue(null);

  13:     }


  15:     // for design time only. runtime is set in app.xaml.cs

  16:     public static CultureInfo Culture = new CultureInfo("en");


  18:     public string ProvideValue(IServiceProvider serviceProvider)

  19:     {

  20:         String s = Translations.ResourceManager.GetString(Key, Culture);


  22:         if (String.IsNullOrEmpty(s))

  23:         {

  24:             Debug.WriteLine(Key + " not found in RESX file!");

  25:             return "!" + Key + "!";

  26:         }


  28:         return s;

  29:     }

  30: }


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,


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:

   1: <TextBlock Text="{local:Translation Key=Hello}" />

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:

   1: private void Application_Startup(object sender, StartupEventArgs e)

   2: {

   3:     TranslationExtension.Culture = new CultureInfo("he"); // or en or fr

   4:     this.RootVisual = new MainPage();

   5: }


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:

   1: <SupportedCultures>en,he,fr</SupportedCultures>

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 Smile )

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *

2 תגובות

  1. Pieter Witvoet3 במאי 2012 ב 11:32

    Thanks a lot, the SupportedCultures part eluded me as well!

  2. במאי 2013 ב 2:10

    not just does it take care of appetite, it also diminishes the craving for waste meals which perform leading part with fat gain.