Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / WPF

Simple WPF Localization

Rate me:
Please Sign up or sign in to vote.
4.84/5 (28 votes)
9 Jul 2009CPOL7 min read 288K   4.4K   89   104
An article on how to easily and effectively fully localize a WPF application.

Introduction

After searching the Internet for a while on how to localize a WPF application, I found two very nice solutions which can be found here and here. However, both solutions looked too complicated to me, and I decided to implement a solution that will just do what I need with simplicity and effectiveness.

My solution by far does not have the features of the above solutions, but it still does its job perfectly: to localize the text in my project.

Background

The basic goals are:

  • Easy to use localization that allows localizing the XAML directly during design time
  • Use of embedded resources
  • The language can be changed at runtime
  • Ability to see the localized resources in the designer of Visual Studio
  • The implementation to be as simple as possible and to not introduce performance issues

Easy to Use

To reach the first goal, I borrowed the great idea from the other two solutions to use a WPF extension. Extensions can be used directly in XAML to assign values to properties, and are extremely useful for the purpose.

For example, the localization of a TextBlock in XAML would look like this:

XML
<TextBlock Text="{Loc Text_Hello}"/>

Use Embedded Resources

The second goal can be reached by connecting a resource manager to the localization component.

When you create a WPF application in Visual Studio, Visual Studio creates a Resources.resx file in the Properties folder of the project. Because this looks like a perfect location to put all your localizable resources in, I chose to attach the localization component to this resource file by default. This is the same resource file you normally access by using the Properties.Resources.MyResourceName expressions in your code-behind.

For flexibility, an arbitrary resource manager can be explicitly connected to the localization component at any time. This will also cause all of the localized content to be updated with the new values.

Design Time Support

Design time support is limited only to the default culture (i.e., the resources you put in the Properties/Resources.resx file). This means you can see any localized resource rendered in the designer but only those you have put in the mentioned file. Any culture specific resource you put in additional resource files (like Resources.fr-FR.resx) will not be visible during design time unless you temporarily rename the files to see your changes. If you find any simple solution to this problem, please let me know.

Design Time Issues

As simple as the code is, you may have some issues during design time, like you may not see your recent changes or you may not see any localized resources at all.

The first issue is easily solved by recompiling the project which causes Visual Studio to reload the main assembly of the project and then to refresh the designer area.

The second issue is more subtle.

At runtime, the default resource file is easily found by obtaining the main entry assembly of the application and creating a resource manager that references the file. But this does not work during design time because an assembly is deemed to be the entry assembly of an application only if the application has either been started by it or by the AppDomain.ExecuteAssembly method (refer to the documentation of the System.Windows.Application.ResourceAssembly property for more information). Therefore, the localization component uses the following procedure to try to find the main assembly of the application during design time: obtain the list of all assemblies loaded in the current domain and search for the first assembly that contains both the Main method and a MyAssemblyName.App class that derives directly or indirectly from System.Windows.Application. If the component finds such an assembly, it creates a resource manager connected to the MyAssemblyName.Properties.Resources resource file of the assembly.

If you have the above issue during design time, you can directly load the assembly of your application by replacing some of the code in the LocalizationManager.GetResourceManager class with code similar to Assembly.Load("MyassemblyName") and use it as the resource assembly.

The third and the fourth goals are strictly an implementation issue and are not covered in this article. The only thing I would mention is that the implementation consists of only two very simple classes and does not add any performance penalty to your application.

Using the Code

To use the solution, you need to do the following:

  1. Add a reference to the WPFLocalization assembly, or better - include the two source code files into one of your class library projects that accompany your applications. The second solution is better because having a library that consists of only two classes adds cost to your application.
  2. If you choose to include the two source code files in your application, note the following:

    1. I advise you to add the classes in one of your class libraries instead of to the main application project as you may have design time issues every time there are any compilation errors (i.e., Visual Studio may fail to load the assembly and raise an error in the designer).
    2. If you change the name of the namespace from "WPFLocalization" to something else, you have to make the same change on the second argument of the System.Windows.Markup.XmlnsDefinition assembly attributes at the beginning of the LocExtension.cs file.
  3. Extract all your localizable resources for the default culture and put them into the Properties/Resources.resx file. Of course, you can place the resources in a resource file anywhere else as well, but this will break the design time support unless you make changes to the code.
  4. For every additional culture, add a corresponding resource file to the Properties folder (e.g., Properties/Resources.fr.resx) and create a localized version of the resources.

  5. Use the localization extension in XAML to reference the resources.

While steps 1 and 2 are straightforward, step 3 needs additional explanation.

Using the Localization Extension

To use the localization extension, you simply need to apply it in XAML on the properties you want to localize, similar to the following code:

XML
<TextBlock Text="{Loc MyResourceKey}"/>  

MyResourceKey is the name of the resource in the Properties/Resources.resx file you want to reference.

To use formatting, use code like:

XML
<TextBlock Text="{Loc CreatorName, Format='Created by {0}'}"/>  

You do not have to add any namespace references (like xmlns:loc="...") at the beginning of the XAML file, because the extension registers to the default Microsoft namespaces at startup (another idea I have borrowed from one of the other solutions).

Images

I have not tested the extension with images and I have no idea if it will work. Handling images most probably requires additional effort and is something I have not planned.

Missing Resources

If a resource you reference is not found, then the following applies:

  • If the application is a debug version, the resource is substituted by the text "[Resource: ResourceKey]".
  • If the application is a release version, the resource is substituted by an empty string.

Changing the Current Culture at Runtime

The current culture can be changed during runtime at any moment by assigning a value to the LocalizationManager.UICulture property. A change to the property does the following:

  1. Changes the UI culture of the current thread to the specified culture (i.e., assigns the value to Thread.CurrentThread.CurrentUICulture).
  2. Updates all the properties localized by the extension.

Using a Different Resource Manager

To use a different resource manager, simply assign a value to the LocalizationManager.ResourceManager property. Assigning a value will also update the localized properties.

Conclusion

Any comments and/or suggestions are welcome.

History

  • 2008-10-10: Initial version.
  • 2008-11-28: Added support for templates.

Updated Version

  • 2009-07-10: Added support for non-dependency properties (i.e., standard properties) to support the Microsoft.Windows.Controls.Ribbon control.
  • 2009-07-10: If a resource is not found during design time, an ArgumentOutOfRangeException exception is thrown instead of showing a [Resource: ResourceKey] message. This makes missing resources easier to spot.

License

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


Written By
Software Developer (Senior)
Bulgaria Bulgaria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionBind Date to localization system Pin
valentini.roberto26-Jul-12 2:42
valentini.roberto26-Jul-12 2:42 
AnswerRe: Bind Date to localization system Pin
Jecho Jekov30-Jul-12 1:46
Jecho Jekov30-Jul-12 1:46 
SuggestionTooltips aren't updated Pin
Pieter Witvoet3-May-12 22:01
Pieter Witvoet3-May-12 22:01 
GeneralRe: Tooltips aren't updated Pin
Jecho Jekov30-Jul-12 1:51
Jecho Jekov30-Jul-12 1:51 
QuestionChinese doesn't work? Pin
Member 72316226-Feb-12 7:32
Member 72316226-Feb-12 7:32 
AnswerRe: Chinese doesn't work? Pin
Jecho Jekov26-Feb-12 7:37
Jecho Jekov26-Feb-12 7:37 
Hi,

I have never tried with Chinese. Could the problem be that the current UI culture does not match the one of the resources?

Regards,
Jecho
GeneralRe: Chinese doesn't work? Pin
Member 72316226-Feb-12 8:19
Member 72316226-Feb-12 8:19 
AnswerRe: Chinese doesn't work? Pin
Jecho Jekov26-Feb-12 8:22
Jecho Jekov26-Feb-12 8:22 
QuestionUnable to get the labels in design time, in VB.NET Pin
Member 858223019-Jan-12 17:07
Member 858223019-Jan-12 17:07 
AnswerRe: Unable to get the labels in design time, in VB.NET Pin
Jecho Jekov19-Jan-12 23:29
Jecho Jekov19-Jan-12 23:29 
GeneralRe: Unable to get the labels in design time, in VB.NET Pin
Member 858223020-Jan-12 18:40
Member 858223020-Jan-12 18:40 
NewsNEW VERSION AVAILABLE Pin
Jecho Jekov9-Sep-11 8:24
Jecho Jekov9-Sep-11 8:24 
Questionusing Localization extension in user control Pin
chualuoi11-Jun-11 16:32
chualuoi11-Jun-11 16:32 
AnswerRe: using Localization extension in user control Pin
Jecho Jekov12-Jun-11 10:10
Jecho Jekov12-Jun-11 10:10 
AnswerRe: using Localization extension in user control Pin
Jecho Jekov9-Sep-11 8:26
Jecho Jekov9-Sep-11 8:26 
GeneralResx in current project [modified] Pin
valentini.roberto20-Dec-10 3:02
valentini.roberto20-Dec-10 3:02 
GeneralRe: Resx in current project Pin
Jecho Jekov20-Dec-10 11:12
Jecho Jekov20-Dec-10 11:12 
GeneralRe: Resx in current project Pin
Jecho Jekov22-Dec-10 13:12
Jecho Jekov22-Dec-10 13:12 
GeneralRe: Resx in current project Pin
valentini.roberto9-Jan-11 23:47
valentini.roberto9-Jan-11 23:47 
GeneralRe: Resx in current project Pin
Jecho Jekov11-Jan-11 11:08
Jecho Jekov11-Jan-11 11:08 
GeneralMy vote of 4 Pin
gopichandana6-Dec-10 1:44
gopichandana6-Dec-10 1:44 
GeneralCrash while Translating for TextBlock.Text Property when Style is applied. Pin
sunnygore30-Nov-10 2:52
sunnygore30-Nov-10 2:52 
GeneralRe: Crash while Translating for TextBlock.Text Property when Style is applied. Pin
Jecho Jekov1-Dec-10 0:22
Jecho Jekov1-Dec-10 0:22 
QuestionHow to use this localization with Binding Pin
d30224128-Sep-10 8:04
d30224128-Sep-10 8:04 
AnswerRe: How to use this localization with Binding Pin
Jecho Jekov9-Oct-10 3:56
Jecho Jekov9-Oct-10 3:56 

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.