WPF Localization For Dummies
I was looking for a way to localize a WPF application and found a few solutions.
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 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.
Add a string resource as shown below. We'll use this string as a main window title.
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.
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.
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.
Change the value of the string "Title" to some Russian text.
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.
Step 5
Set the culture in the application. It can be done explicitly as shown in the following
code example.
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.