Click here to Skip to main content
Click here to Skip to main content

How to globalize and localize a WPF project

, 4 Mar 2008
Rate this:
Please Sign up or sign in to vote.
Describe a method to globalize and localize a WPF project, a sample is given

1. Overview

This document introduces a way of globalizing and localizing in Windows Presentation Foundation (WPF) and gives a sample project. The solution mentioned in this document is based on Windows SDK documents and some sample projects of the technical web site “CodeProject.com”.

The process of globalization and localization needs some software tools to assist. The following tools will be used:

<         <MSBuild.exe (Microsoft build engine)

<         <LocBaml.exe

<         <Resgen.exe (Resource File Generator)

<         <Al.exe (Assembly Linker)

<2.   <Globalization

There are two kinds of string constant in code behind. One displayed for user needs to be localized, which is named “Localizable String” in this document, and the other only used for code doesn’t need to be localized, which is named “Non-localizable String”.

 

For the two types of string:

1. Use a Class NonLocalizableString.cs to save the Non-localizable String.

2. User Properties\Resources.resx to save the Localizable String.

<<!--[if mso & !supportInlineShapes & supportFields]> <![endif]-->

Globalization is the design and development of applications that perform in multiple locations.

The tool is used to globalize a project (refer to 4.2):

<           <Microsoft build engine (Msbuild.exe), which is a compiler that runs from the command line.

<1.1 < A Sample of Globalization Application

In this part, a sample of globalization application is provided and a step-by-step approach is given.

Step1. Create an application in WPF

<!--[if gte vml 1]> <

Use Properties\Resources.resx to save the Localizable String.

<!--[if gte vml 1]> <

Create NonLocalizableString.cs to save the Unlocalizable String.

<!--[if gte vml 1]> <

Step2. Create UI

<!--[if gte vml 1]> <

Step3Create responsibility function of “ShowMsg” button

<!--[if gte vml 1]>

Step4Generate Uid in XAML

Open Windows SDK command shell:

<!--[if gte vml 1]>

And use the following commands to generate Uid and check it:

Msbuild /t:updateuid LocalizationTest.csproj  // generate Uid

Msbuild /t:checkuid LocalizationTest.csproj  // check Uid

The result in XAML likes this:

<Grid x:Uid="Grid_1">

Step5. Build

After built, a globalization project is OK to be localized.

The resource to be localized contains two parts.

<1.       <The string to be displayed for user in XAML

<2.       <The string to be displayed for user in Code behind.

3. Localization

Localization is the translation of application resources into localized versions for the specific cultures that the application supports.

This part gives you a step-by-step approach to localizing the application.

The tools are used to localize (refer to 4.2).

<1.           <LocBaml.exe

<2.           <Resgen.exe

<3.           <Al.exe

The essential files to be used for localization:

Table 3-1 Essential files for localization

File name

Location

Comments

LocalizationTest.exe

bin\Release

Main assembly to be used to abstract resource to be localized in XAML.

LocalizationTest.g.resources

obj\Release

Generate binary resource file of XAML by using it.

LocalizationTest.Properties.Resources.resources

 

obj\Release

Rename the binary resource file by using the format of its file name.

Resources.resx

Properties\

Localizable string in code behind

 

Notes:

1. The release version is used to be localized.

2. To simplify specification, the full path isn’t used in command.

3.1 Localizing resource in code behind

Step1: Create a copy of Properties\Resources.resx and localize relative strings

The resource file:Resources.zh-cn.resx (The name is random)

Update the resource to be localized:

<!--[if gte vml 1]>

<!--[if gte vml 1]>

Step2: Use resgen.exe of SDK to generate binary resource file

     Resgen Resources.zh-cn.resx

Step3: Rename the binary resource file generated by step2:

     Resources.zh-cn.resources à LocalizationTest.resource.LocalizationTestResource.zh-cn.resources

3.2 Localizing resource in XAML

Step1: Use LocBaml to abstract resource to be localized in XAML from mainly assembly.

LocBaml /parse LocalizationTest.exe

   And a CSV file containing resource to be localized is generated: LocalizationTest.CSV

Note: 1. Please copy LocBaml to the folder of mainly assembly.

     2. The output file name of LocBaml is optional.

Step2: Edit CSV file to localize the resourceRefer to 4.4 to get CSV format.

Step3: Use LocBaml to generate binary resource file with LocalizationTest.g.resources and CSV file localized.

      LocBaml /generate LocalizationTest.g.resources /tran: LocalizationTest.CSV /cul:zh-cn /out:.\zh-cn\

Step4:Rename the binary resource file generated by step3:

LocalizationTest.g.resources à LocalizationTest.g.zh-cn.resources

3.3 Embed the two binary resources into a satellite

Use the tool “al.exe” of SDK and execute the following command.

Al /template:LocalizationTest.exe /embed:LocalizationTest.Properties.Resources.zh-cn.resources /embed:LocalizationTest.g.zh-cn.resources /culture:zh-cn /out:LocalizationTest.resources.dll

 

And create a “zh-cn” folder to save “LocalizationTest.resources.dll”.

 

After all steps are finished, a localization version in zh-cn is made successfully. Run the app and check it.

 

<!--[if gte vml 1]>


4. Appendix

4.1 Material Link

Please get the overview of globalization and localization in WPF from the following link:
ms-help://MS.MSSDK.1033/MS.NETFX30SDK.1033/wpf_conceptual/html/56e5a5c8-6c96-4d19-b8e1-a5be1dc564af.htm

 

Please get another sample code from the following link. There are three different ways of localizing a WPF .EXE application in the sample code. And the third approach is our need, by which the localization work can be carried on after the project building.

http://www.codeproject.com/WPF/WPFUsingLocbaml.asp

 

And you can also get a tutorial how to create a localized application by using the LocBaml tool from the link:

ms-help://MS.MSSDK.1033/MS.NETFX30SDK.1033/wpf_conceptual/html/5001227e-9326-48a4-9dcd-ba1b89ee6653.htm#Some_Tips_for_Using_LocBaml.

 

4.2 Tools

Table 4-1 Tool specification

Tool

Specification

MSBuild.exe

The Microsoft Build Engine (MSBuild) is the new build platform for Microsoft and Visual Studio. MSBuild is completely transparent with regards to how it processes and builds software, enabling developers to orchestrate and build products in build lab environments where Visual Studio is not installed.

LocBaml.exe

The LocBaml tool is not a production-ready application. It is presented as a sample that uses some of the localization APIs and illustrates how you might write a localization tool.

Resgen.exe

Resource File Generator

The Resource File Generator converts .txt files and .resx (XML-based resource format) files to common language runtime binary .resources files that can be embedded in a runtime binary executable or compiled into satellite assemblies

Al.exe

Assembly Linker

The Assembly Linker generates a file with an assembly manifest from one or more files that are either modules or resource files.

 

 

 

Some Tips for Using LocBaml

<1.         <All dependent assemblies that define custom controls must be copied into the local directory of LocBaml or installed into the GAC. This is necessary because the localization API must have access to the dependent assemblies when it reads the binary XAML (BAML).

<2.         <If the main assembly is signed, the generated resource dll must also be signed in order for it to be loaded.

<3.         <The version of the localized resource dll needs to be synchronized with the main assembly.

4.3 File type specification

Table 4-2 File type specification

File type

Specification

.resx

XML-based resource format

.resources

Can be embedded in a runtime binary executable or compiled into satellite assemblies

 

4.4 Columns description in CSV file generated by LocBaml

Table 4-3 Columns description in CSV file generated by LocBaml

Column

Description

Baml Name

Identifies the BAML stream; the value will be of the form AssemblyManifestStreamName:SubStream Name.

Resource Key

Identifies the localizable resource; the value will be of the form Uid:Element Type.$Property.

Localization Category

An entry from the LocalizationCategory enumeration, indicating what kind of content this is.

Readable

Indicates whether the resource is visible for translation.

Modifiable

Indicates whether this value can be modified during translation.

Comments

Localization comments.

Value

The value of this resource.

4.4 Best Practices for Globalization and Localization in WPF

Best Practices for WPF UI Design

When you design a WPF–based UI, consider implementing these best practices:

<         <Write your UI in XAML; avoid creating UI in code. When you create your UI by using XAML, you expose it through built-in localization APIs.

<         <Avoid using absolute positions and fixed sizes to lay out content; instead, use relative or automatic sizing.

1. Use SizeToContent; and keep widths and heights set to Auto.

2. Avoid using Canvas to lay out UIs.

3. Use Grid and its size-sharing feature.

<         <Provide extra space in margins because localized text often requires more space. Extra space allows for possible overhanging characters.

<         <Enable TextWrapping on TextBlock to avoid clipping.

<         <Set the xml:lang attribute. This attribute describes the culture of a specific element and its child elements. The value of this property changes the behavior of several features in WPF. For example, it changes the behavior of hyphenation, spell checking, number substitution, complex script shaping, and font fallback.

<l         <Create a customized composite font to obtain better control of fonts that are used for different languages. By default, WPF uses the GlobalUserInterface.composite font in your Windows\Fonts directory.

<         <When you create navigation applications that may be localized in a culture that presents text in a right-to-left format, explicitly set the FlowDirection of every page to ensure the page does not inherit FlowDirection from the NavigationWindow.

<         <When you create stand-alone navigation applications that are hosted outside a browser, set the StartupUri for your initial application to a NavigationWindow instead of to a page (for example, <Application StartupUri="NavigationWindow.xaml">). This design enables you to change the FlowDirection of the Window and the navigation bar.

 

Best Practices for WPF Localization

When you localize WPF–based applications, consider implementing these best practices:

<         <Use localization comments to provide extra context for localizers.

<         <Use localization attributes to control localization instead of selectively omitting Uid properties on elements. See Localization Attributes and Comments for more information.

<         <Use msbuild /t:updateuid and /t:checkuid to add and check Uid properties in your XAML. Use Uid properties to track changes between development and localization. Uid properties help you localize new development changes. If you manually add Uid properties to a UI, the task is typically tedious and less accurate.

1. Do not edit or change Uid properties after you begin localization.

2. Do not use duplicate Uid properties (remember this tip when you use the copy-and-paste command).

3. Set the UltimateResourceFallback location in AssemblyInfo.* to specify the appropriate language for fallback (for example, [assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]).

If you decide to include your source language in the main assembly by omitting the <UICulture> tag in your project file, set the UltimateResourceFallback location as the main assembly instead of the satellite (for example, [assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)]).

License

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

Share

About the Author

zhouyuhui
Software Developer
China China
Hello, I am from China. I got master degree of computer science and technology from JiLin University in 2006. Now I engaged in software development about C# and WPF. I enjoy my life.

Comments and Discussions

 
GeneralLinks in the article PinmemberAnsgar Hellwig20-Jul-10 21:06 
GeneralBest Localization Plug-in for Visual Studio. PinmemberAlexander Nesterenko17-Dec-08 21:31 
General加油 PinmemberJamebo31-Mar-08 23:09 
General东北的老乡 Pinmemberzoujiaming4-Mar-08 20:56 
GeneralRe: 东北的老乡 Pinmemberzhouyuhui5-Mar-08 2:43 

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 | Mobile
Web03 | 2.8.140814.1 | Last Updated 5 Mar 2008
Article Copyright 2008 by zhouyuhui
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid