Click here to Skip to main content
15,881,757 members
Articles / Programming Languages / C#

A Great WPF Localization Package

Rate me:
Please Sign up or sign in to vote.
4.60/5 (5 votes)
18 Mar 2013CPOL2 min read 14.6K   12   3
The package is open source, allows switching the locales at run time and also allows localizing any Dependency or Attached properties – not only strings.

Several months ago, I needed to localize a WPF application (it had to have English and German versions. I found an excellent localization package for WPF on the internet and used it for my project. The package was created by Tomer Shamam and was described at WPF Localization – On-the-fly Language Selection (though the link is no longer available). I published the code for the package (kindly sent to me by Tomer) on github at WPFLocalizationPackage.

It proved to be a great solution allowing to create a German version very fast.

The package is open source, allows switching the locales at run time and also allows localizing any Dependency or Attached properties – not only strings. It provides “Translate” markup extension that can be used from within XAML.

In order to localize an application with the help of this package, you need to create XML catalogs for each of the locales you want your application to support. Here is an excerpt from such a catalog that comes with the demo that comes together with the source:

XML
<Dictionary EnglishName="English" 
CultureName="English" Culture="en-US">
<Value Id="0" FlowDirection="LeftToRight" 
             Title="Demo Window" 
             Width="700" Height="340" />
<Value Id="1" Content="This is a simple text" />
...
<Dictionary>

Each Value tag has an Id attribute used for matching values from the catalogs into XAML or C# code. The rest of the attributes specify WPF object properties and values that those properties should get under the locale.

Here is an example of referring to a catalog value from XAML code:

XML
<Window ...
Title="{loc:Translate Title, Uid=0}"
...
>

Translate markup extension refers to attribute title under Value tag with Id=0.

Mapping between the different cultures and catalogs is done with the help of LanguageDictionary.RegisterDictionary method, e.g.:

C#
LanguageDictionary.RegisterDictionary(
CultureInfo.GetCultureInfo("en-US"),
new XmlLanguageDictionary("Languages/en-US.xml"));

LanguageDictionary.RegisterDictionary(
CultureInfo.GetCultureInfo("he-IL"),
new XmlLanguageDictionary("Languages/he-IL.xml"));

maps culture “en-US” into en-US.xml catalog file under Languages folder while culture “he-IL” is mapped into he-IL.xml file.

Here is the code to change localization catalog at run time:

C#
if (LanguageContext.Instance.Culture == CultureInfo.GetCultureInfo("he-IL"))
{
  LanguageContext.Instance.Culture = CultureInfo.GetCultureInfo("en-US");
}
else
{
  LanguageContext.Instance.Culture = CultureInfo.GetCultureInfo("he-IL");
}

The demo contains an application that can switch between English and Hebrew locales. Here is how the English version looks:

LocaleAppEnglish

And here is its Hebrew counterpart:

LocaleAppHebrew

You can switch between English and Hebrew by pressing the button with the corresponding flag. As you can see, not only strings, but also sizes and (even more impressively) the FlowDirection changes when the locale is switched.

Please find more information at WPF Localization – On-the-fly Language Selection and by browsing the demo code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect AWebPros
United States United States
I am a software architect and a developer with great passion for new engineering solutions and finding and applying design patterns.

I am passionate about learning new ways of building software and sharing my knowledge with others.

I worked with many various languages including C#, Java and C++.

I fell in love with WPF (and later Silverlight) at first sight. After Microsoft killed Silverlight, I was distraught until I found Avalonia - a great multiplatform package for building UI on Windows, Linux, Mac as well as within browsers (using WASM) and for mobile platforms.

I have my Ph.D. from RPI.

here is my linkedin profile

Comments and Discussions

 
GeneralMy vote of 4 Pin
Nisarg S Shah18-Mar-13 16:51
Nisarg S Shah18-Mar-13 16:51 
QuestionWorking copy of the screenshot Pin
Nisarg S Shah18-Mar-13 3:36
Nisarg S Shah18-Mar-13 3:36 
AnswerRe: Working copy of the screenshot Pin
Nick Polyak18-Mar-13 5:58
mvaNick Polyak18-Mar-13 5:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.