Click here to Skip to main content
11,705,451 members (51,608 online)
Click here to Skip to main content

WPF Localization for Dummies

, 13 Dec 2011 CPOL 51.1K 1.9K 78
Rate this:
Please Sign up or sign in to vote.
Explains how to easily localize a WPF application

Introduction

I was looking for a way to localize a WPF application and found a few solutions here on CodeProject plus a few more somewhere else. It took me quite some time to read through all of them but the end result was confusion - I could not figure out which way to go. Coming from C++ background, the resource files seemed like a logical choice so I started looking only at the resource files for the localization. It took me some time to figure it out and I realized that it's actually quite simple. The problem is, as very often the case, the documentation.
So I decided to write a beginner's article to make the localization simple, hence the name "for dummies". The goal is to explain step by step how to localize a WPF application.

Step 1

Create a WPF project. I called it, obviously, "WPFLocalizationForDummies". Note that the Properties folder has an empty resource data file named Resources.resx. Change the access modifier to Public otherwise we will not be able to use it in XAML.

Resource file

Add a string resource as shown below. We'll use this string as a main window title.

Resource file

Compile and run the project. It should run without any surprises.

Step 2

Create a localized resource file. Select Resources.resx file in the Visual Studio solution window and press Ctrl+C keys. Now press Ctrl+V keys. The copy of the Resources.resx file will be created.

Copy of the resources file

Rename "Copy of Resources.resx" file. The new name should be Resources.XX.resx where "XX" is a culture name. A culture is a combination of a specific language and country/region name and it can be found on National Language Support (NLS) API Reference page.

I suggest having an English localized resource file if you plan on supporting English language despite the fact that the default Resources.resx file may have the English strings as well. It'll be much easier later to switch to the English language and back. So our first localized resource file should be named Resources.en.resx.

English resources file

Step 3

Let's create one more localized resource file as an example, let's say Russian. Repeat step 2, the only difference is that the new file should be named Resources.ru-RU.resx.

Russian resources file

Change the value of the string "Title" to some Russian text.

Russian Resource file

Step 4

Compile the application. The compiler and linker will create a separate satellite assembly for each culture. The satellite assemblies will be placed in sub-directories under the directory holding your main assembly. The sub-directories will be named by culture, allowing the .NET runtime to locate the resources appropriate to the culture in which the application runs. The main (default) resources file will be embedded in the main assembly.

In my example, since I have 2 resource files, 2 sub-folders are created each containing the resource satellite DLL - English and Russian.

Compile output

Step 5

Set the culture in the application. It can be done explicitly as shown in the following code example.

// Set the current user interface culture to the specific culture Russian
System.Threading.Thread.CurrentThread.CurrentUICulture = 
			new System.Globalization.CultureInfo("ru-RU");

Step 6

Now we have the resource DLLs, so let's use them in the application. The localized resources can be used in both XAML and C# code.

  • In XAML file:

    Add a namespace declaration to each XAML file where you'd want to use the localized resources (which is like the C# using statement).

    xmlns:p="clr-namespace:WPFLocalizationForDummies.Properties"

    In the MainWindow.xaml, replace...

    Title="Main" Height="350" Width="525">

    ...with:

    Title="{x:Static p:Resources.Title}" Height="350" Width="525">
  • In C# code, a string from the resource DLL can be retrieved as shown below:
    Title = WPFLocalizationForDummies.Properties.Resources.Title;

Conclusion

As you can see, the localization in WPF applications can be achieved very easily by following just a few steps explained above.

System Requirements

  1. Windows 7
  2. Visual Studio 2010
  3. .NET 4.0

History

  • Initial release - 12/12/2011

License

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

Share

About the Author

Igor Vigdorchik
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Carlos Toledo10-Dec-14 10:16
memberCarlos Toledo10-Dec-14 10:16 
GeneralRe: My vote of 5 Pin
Igor Vigdorchik10-Dec-14 12:25
memberIgor Vigdorchik10-Dec-14 12:25 
GeneralGreat Work!!! Pin
Carlos Toledo10-Dec-14 10:16
memberCarlos Toledo10-Dec-14 10:16 
GeneralRe: Great Work!!! Pin
Igor Vigdorchik10-Dec-14 12:25
memberIgor Vigdorchik10-Dec-14 12:25 
QuestionDynamic change of language Pin
Member 1093462816-Jul-14 22:36
memberMember 1093462816-Jul-14 22:36 
AnswerRe: Dynamic change of language Pin
Igor Vigdorchik17-Jul-14 3:17
memberIgor Vigdorchik17-Jul-14 3:17 
SuggestionRe: Dynamic change of language Pin
FormatException11-Aug-14 9:06
memberFormatException11-Aug-14 9:06 
AnswerRe: Dynamic change of language Pin
MCY20-Aug-15 22:21
professionalMCY20-Aug-15 22:21 
QuestionMy Vote is 5 Pin
Igal_Kroyter3-Jan-14 6:22
memberIgal_Kroyter3-Jan-14 6:22 
GeneralMy vote of 5 Pin
zlanizer2-Jun-13 17:55
memberzlanizer2-Jun-13 17:55 
GeneralMy vote of 5 Pin
johannesnestler29-May-13 4:14
memberjohannesnestler29-May-13 4:14 
GeneralMy vote of 5 Pin
jahfer14-Feb-13 19:53
memberjahfer14-Feb-13 19:53 
QuestionLocatilzation for Dummies Pin
xaldox11-Feb-13 7:59
memberxaldox11-Feb-13 7:59 
AnswerRe: Locatilzation for Dummies Pin
Igor Vigdorchik12-Feb-13 16:50
memberIgor Vigdorchik12-Feb-13 16:50 
QuestionProblem in XAML - Public modifier Pin
bjansson12-Dec-12 21:35
memberbjansson12-Dec-12 21:35 
AnswerRe: Problem in XAML - Public modifier Pin
Member 878276323-Jan-14 3:31
memberMember 878276323-Jan-14 3:31 
QuestionRun time localzation Pin
beiuck20204-Sep-12 1:56
memberbeiuck20204-Sep-12 1:56 
AnswerRe: Run time localzation Pin
Igor Vigdorchik5-Sep-12 14:19
memberIgor Vigdorchik5-Sep-12 14:19 
GeneralRe: Run time localzation Pin
Ritschy17-Apr-13 4:23
memberRitschy17-Apr-13 4:23 
AnswerRe: Run time localzation Pin
MCY20-Aug-15 22:22
professionalMCY20-Aug-15 22:22 
GeneralGreat! Igor! Excellent!! Pin
Member 31213939-Aug-12 19:41
memberMember 31213939-Aug-12 19:41 
GeneralMy vote of 5 Pin
CandyJoin26-Jul-12 10:06
memberCandyJoin26-Jul-12 10:06 
GeneralMy vote of 5 Pin
Farhan Ghumra17-Jul-12 2:56
memberFarhan Ghumra17-Jul-12 2:56 
GeneralMy vote of 5 Pin
Madhan Mohan Reddy26-Jun-12 1:16
memberMadhan Mohan Reddy26-Jun-12 1:16 
QuestionPrivet! Pin
Omar Gamil27-Dec-11 3:13
memberOmar Gamil27-Dec-11 3:13 
AnswerRe: Privet! Pin
Igor Vigdorchik27-Dec-11 4:24
memberIgor Vigdorchik27-Dec-11 4:24 
QuestionNice article! Pin
Sérgio Vicente26-Dec-11 7:14
memberSérgio Vicente26-Dec-11 7:14 
AnswerRe: Nice article! Pin
Igor Vigdorchik26-Dec-11 11:06
memberIgor Vigdorchik26-Dec-11 11:06 
GeneralMy vote of 5 Pin
Member 308248715-Dec-11 1:21
memberMember 308248715-Dec-11 1:21 
GeneralRe: My vote of 5 Pin
Igor Vigdorchik15-Dec-11 4:44
memberIgor Vigdorchik15-Dec-11 4:44 
GeneralWell explained Pin
Meshack Musundi14-Dec-11 3:19
memberMeshack Musundi14-Dec-11 3:19 
GeneralRe: Well explained Pin
Igor Vigdorchik14-Dec-11 14:24
memberIgor Vigdorchik14-Dec-11 14:24 
Questionuseful Pin
CIDev13-Dec-11 6:05
memberCIDev13-Dec-11 6:05 
AnswerRe: useful Pin
Igor Vigdorchik13-Dec-11 15:41
memberIgor Vigdorchik13-Dec-11 15:41 
GeneralMy vote of 5 Pin
stooboo13-Dec-11 3:20
memberstooboo13-Dec-11 3:20 
GeneralRe: My vote of 5 Pin
Igor Vigdorchik13-Dec-11 15:41
memberIgor Vigdorchik13-Dec-11 15:41 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 13 Dec 2011
Article Copyright 2011 by Igor Vigdorchik
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid