Click here to Skip to main content
14,028,233 members
Click here to Skip to main content
Add your own
alternative version

Stats

12.3K views
3 bookmarked
Posted 1 May 2011
Licenced CPOL

Easily Localize WPF Application via Visual Studio Template

, 1 May 2011
Rate this:
Please Sign up or sign in to vote.
A Localized WPF Application template for easily localizing a WPF application

Background

Localizing a WPF application is not an easy task. Almost any article on the localization of WPF is replete with details and manual steps to implementation of a localized application.

Existing Solutions

Localization using the utility LocBaml as described by Microsoft has lots of advantages, but is difficult to maintain. André van heerwaarde in his article proposed to simplify this decision by using the configured build step. He wrote a utility to merge the translated text fragments. However, in his article, there are still many manual steps.

Visual Studio Project Template

I propose to use a Localized WPF Application template I've created to start working on a multilingual WPF application. Projects created using this template already contain all the necessary tools for localization, as well as automates the localization process.

During application development, you add a new XAML file without having to worry about localization. Upon completion of the changes, simply build the project.

  • The custom build step will call msbuild / t: updateuid [Project Name].csproj. This step will automatically add x:Uid at every element containing the text elements.
  • Then the build process will automatically start LocBaml, which finds all the text elements in the XAML file and generates a CSV file containing the text elements.
  • The next step will run the utility from MergeLocBamlCsv by André van heerwaarde. As a result, the earlier translated segments will be merged with the new ones.
  • The utility StripLocBamlCsv cuts unused items from the CSV file.

The only thing you have to do manually is to add new languages and the actual translation itself.

Adding a New Language

To add a new language in the draft, you will have to:

copy Translation\Translate.csv Translation\Translate.ru-RU.csv
<LocBamlCsv Include="Translation\Translate.de-DE.csv">
  <Culture>de-DE</Culture>
</LocBamlCsv>

add a new language, for example:

<LocBamlCsv Include="Translation\Translate.ru-RU.csv">
  <Culture>ru-RU</Culture>
</LocBamlCsv>
  • Copy the location of the neutral language in the new CSV file
  • Open the project file in a text editor, and after these lines:

Then you need to reopen the project in Visual Studio. If the project was already open, the IDE will ask whether you wish to reopen the project to reflect the changes, answer 'Yes'.

If you did not make mistakes, then the newly opened project folder Translation will have a new file Translate.ru-RU.csv.

Start the assembly to update the translation files, then you can open a new file and start translating.

Localize Text in code-behind

StringResources.xaml is a Resource Dictionary example for translated strings:

<ResourceDictionary 

  x:Uid="ResourceDictionary_1" 

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

  xmlns:system="clr-namespace:System;assembly=mscorlib">
  
  <!-- String resource that can be localized -->
  <system:String x:Uid="system:String_1" 

  x:Key="TestString1">Test</system:String>
  <system:String x:Uid="system:String_2" 

  x:Key="MainWindow">Main Window</system:String>
</ResourceDictionary>

The usage is as easy as:

this.Title = (string)FindResource("MainWindow");

Links and Credits

License

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

Share

About the Author

Ernest Poletaev
Software Developer (Senior) Enterprise Information Systems, Plc
Thailand Thailand
Senior Software Developer in Enterprise Information Systems, an ERP / CRM / Docflow Software Solution Provider for Russian Local Market.

Professional Developer (C, C++, C#), since 1984. Experienced in many programming technologies.

Now i resides in Kalasin, Thailand, working remotely for my company

You may also be interested in...

Pro

Comments and Discussions

 
GeneralOther approach Pin
time-best3-May-11 7:11
membertime-best3-May-11 7:11 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190419.4 | Last Updated 1 May 2011
Article Copyright 2011 by Ernest Poletaev
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid