Click here to Skip to main content
15,892,927 members
Articles / Desktop Programming / WPF
Article

How to globalize and localize a WPF project

Rate me:
Please Sign up or sign in to vote.
2.83/5 (5 votes)
4 Mar 2008CPOL9 min read 167.9K   1.5K   29   5
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”.<o:p>

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

<         <MSBuild.exe (Microsoft build engine)

<         <LocBaml.exe <o:p>

<         <Resgen.exe (Resource File Generator)<o:p>

<         <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 “<b style='mso-bidi-font-weight:normal'>Localizable String” in this document, and the other only used for code doesn’t need to be localized, which is named “<i style='mso-bidi-font-style:normal'>Non-localizable String”.

<o:p> 

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]><v:shape id="_x0000_i1025" type="#_x0000_t75" style="width:396pt; height:156pt"> <v:imagedata croptop="-65520f" cropbottom="65520f"> <![endif]-->

Globalization is the design and development of applications that perform in multiple locations.<o:p>

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.<o:p>

Step1. Create an application in WPF<o:p>

<!--[if gte vml 1]><v:shape id="_x0000_i1026" type="#_x0000_t75" style="width:160.5pt;height:122.25pt"> <<o:p>

Use <b style='mso-bidi-font-weight:normal'>Properties\Resources.resx to save the Localizable String.<o:p>

<!--[if gte vml 1]><v:shape id="_x0000_i1027" type="#_x0000_t75" style="width:323.25pt;height:74.25pt"> <<o:p>

Create <b style='mso-bidi-font-weight:normal'>NonLocalizableString.cs to save the Unlocalizable String. <o:p>

<!--[if gte vml 1]><v:shape id="_x0000_i1028" type="#_x0000_t75" style="width:315pt;height:81.75pt"> <<o:p>

Step2. <b style='mso-bidi-font-weight:normal'>Create UI<o:p>

<!--[if gte vml 1]><v:shape id="_x0000_i1031" type="#_x0000_t75" style="width:252pt;height:135pt"> <<o:p>

Step3<b style='mso-bidi-font-weight:normal'>Create responsibility function of “ShowMsg” button <o:p>

<!--[if gte vml 1]><v:shape id="_x0000_i1029" type="#_x0000_t75" style="width:341.25pt;height:138.75pt"> <o:p>

Step4<b style='mso-bidi-font-weight:normal'>Generate Uid in XAML<o:p>

Open Windows SDK command shell:<o:p>

<!--[if gte vml 1]><v:shape id="_x0000_i1030" type="#_x0000_t75" style="width:414pt;height:77.25pt"> <o:p>

And use the following commands to generate Uid and check it:<o:p>

Msbuild /t:updateuid LocalizationTest.csproj  // generate Uid<o:p>

Msbuild /t:checkuid LocalizationTest.csproj  // check Uid<o:p>

The result in XAML likes this:<o:p>

<Grid x:Uid="Grid_1"><o:p>

Step5. Build<o:p>

After built, a globalization project is OK to be localized.<o:p>

The resource to be localized contains two parts.<o:p>

<1.       <The string to be displayed for user in XAML<o:p>

<2.       <The string to be displayed for user in Code behind.<o:p>

3. Localization

Localization is the translation of application resources into localized versions for the specific cultures that the application supports.<o:p>

This part gives you a step-by-step approach to localizing the application. <o:p>

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

<b style="mso-bidi-font-weight:normal">File name<o:p>

<b style="mso-bidi-font-weight:normal">Location<o:p>

<b style="mso-bidi-font-weight:normal">Comments<o:p>

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

<o:p> 

obj\Release

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

Resources.resx

Properties\

Localizable string in code behind

<o:p> 

Notes: <o:p>

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<o:p>

The resource file:Resources.zh-cn.resx (The name is random<b style='mso-bidi-font-weight:normal'>)<o:p>

Update the resource to be localized:

<!--[if gte vml 1]><v:shape id="_x0000_i1032" ="" type="#_x0000_t75" style="width:350.25pt;height:34.5pt">

<!--[if gte vml 1]><v:shape id="_x0000_i1033" ="" type="#_x0000_t75" style="width:339.75pt;height:37.5pt">

Step2: Use resgen.exe of SDK to generate binary resource file<o:p>

     Resgen Resources.zh-cn.resx<o:p>

Step3: Rename the binary resource file generated by step2:<o:p>

     Resources.zh-cn.resources à LocalizationTest.resource.LocalizationTestResource.zh-cn.resources<o:p>

3.2 Localizing resource in XAML

Step1: Use LocBaml to abstract resource to be localized in XAML from mainly assembly. <o:p>

LocBaml /parse LocalizationTest.exe<o:p>

   And a CSV file containing resource to be localized is generated: LocalizationTest.CSV<o:p>

Note: 1. Please copy LocBaml to the folder of mainly assembly.<o:p>

     2. The output file name of LocBaml is optional. <o:p>

Step2: Edit CSV file to localize the resourceRefer to 4.4 to get CSV format.<o:p>

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

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

Step4:<b style='mso-bidi-font-weight:normal'>Rename the binary resource file generated by step3:<o:p>

LocalizationTest.g.resources à LocalizationTest.g.zh-cn.resources<o:p>

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<o:p>

<o:p> 

And create a “zh-cn” folder to save “LocalizationTest.resources.dll”.<o:p>

<o:p> 

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

<o:p> 

<!--[if gte vml 1]><v:shape id="_x0000_i1034" ="" type="#_x0000_t75" style="width:219pt;height:118.5pt"> <o:p>


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/56e<st1:chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="5" unitname="a" w:st="on">5a<st1:chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="5" unitname="C" w:st="on">5c8<st1:chmetcnv tcsc="0" numbertype="1" negative="True" hasspace="False" sourcevalue="6" unitname="C" w:st="on">-6c96-4d19-b8e1-a5be1dc564af.htm<o:p>

<o:p> 

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.<o:p>

<a href="http: www.codeproject.com="" wpf="" wpfusinglocbaml.asp"="">http://www.codeproject.com/WPF/WPFUsingLocbaml.asp<o:p>

<o:p> 

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<st1:chmetcnv unitname="a" sourcevalue="48" hasspace="False" negative="True" numbertype="1" tcsc="0" w:st="on">-48a4-9dcd-ba1b89ee6653.htm#Some_Tips_for_Using_LocBaml. <o:p>

<o:p> 

4.2 Tools

Table 4-1 Tool specification

<b style="mso-bidi-font-weight:normal">Tool<o:p>

<b style="mso-bidi-font-weight:normal">Specification<o:p>

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.<o:p>

LocBaml.exe<o:p>

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.<o:p>

Resgen.exe<o:p>

Resource File Generator <o:p>

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<o:p>

Al.exe

Assembly Linker<o:p>

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

<o:p> 

<o:p> 

<o:p> 

Some Tips for Using LocBaml<o:p>

<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).<o:p>

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

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

4.3 File type specification

Table 4-2 File type specification

<b style="mso-bidi-font-weight:normal">File type<o:p>

<b style="mso-bidi-font-weight:normal">Specification<o:p>

.resx<o:p>

XML-based resource format<o:p>

.resources<o:p>

Can be embedded in a runtime binary executable or compiled into satellite assemblies<o:p>

<o:p> 

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.<o:p>

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<o:p>

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.

<o:p> 

Best Practices for WPF Localization<o:p>

When you localize WPF–based applications, consider implementing these best practices:<o:p>

<         <Use localization comments to provide extra context for localizers.<o:p>

<         <Use localization attributes to control localization instead of selectively omitting Uid properties on elements. See <a href="ms-help: ms.mssdk.1033="" ms.netfx30sdk.1033="" wpf_conceptual="" html="" ead2d9ac-b709-4ec1-a924-39927a29d02f.htm"="">Localization Attributes and Comments for more information.<o:p>

<         <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. <o:p>

1. Do not edit or change Uid properties after you begin localization.<o:p>

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

3. Set the UltimateResourceFallback location in AssemblyInfo.* to specify the appropriate language for fallback (for example,

[assembly:
NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
). <o:p>

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)]
).<o:p>

License

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


Written By
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 Pin
Ansgar Hellwig20-Jul-10 21:06
professionalAnsgar Hellwig20-Jul-10 21:06 
GeneralBest Localization Plug-in for Visual Studio. Pin
Alexander Nesterenko17-Dec-08 21:31
Alexander Nesterenko17-Dec-08 21:31 
General加油 Pin
Jamebo31-Mar-08 23:09
Jamebo31-Mar-08 23:09 
General东北的老乡 Pin
zoujiaming4-Mar-08 20:56
zoujiaming4-Mar-08 20:56 
GeneralRe: 东北的老乡 Pin
zhouyuhui5-Mar-08 2:43
zhouyuhui5-Mar-08 2:43 

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.