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

Sample for globalization and localization in WPF

, 24 Apr 2007
Rate this:
Please Sign up or sign in to vote.
Decription how to globalize and localize in WPF.

1. Introduction

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

The following tools are necessary for the tutorial (Refer to Table 1-3 in Appendix).

  • LocBaml.exe
  • resgen.exe
  • al.exe

And you can also get a sample code to localize XAML resource and its steps 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.

2. Localization

Screenshot - flue1.jpg

2.1 Create sample project

Steps:

1. Create a project in WPF.

Screenshot - proj.jpg

2. Create some elements in XAML.

Screenshot - ui.jpg

3. Add event to button "ShowMsg"

Screenshot - btn.jpg1

2.2 Create neutral version

The neutral version of application should be created so that our localization work can be carried on after application is built.

Steps:

  • Dispose the resource in code behind

    1. Create a resource file in resx format to save the string in code behind.

    Screenshot - en.jpg

    2. Open GlobalizationTestDlgResource.resx and add string

    Screenshot - msg.jpg

    3. Create a copy of GlobalizationTestDlgResource.resx and rename it as neutral (en-US) resource file.

    Screenshot - en-us.jpg

  • Dispose resource in XAML

1. Modified the project file and AssemblyInfo.cs

Add <UICulture>en-US</UICulture> in GlobalizationTestDlg.csproj as neutral culture of project.

Screenshot - cul.jpg

Uncomment the following code if it exists, otherwise add it.

[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]

2. Use SDK cmd shell to generate id for each element in XAML.

Msbuild /t:updateuid GlobalizationTestDlg.csproj //generate id

Msbuild /t:checkuid GlobalizationTestDlg.csproj //check id

After the operation, a "Uid" is added to each element. Like this:

<Grid x:Uid="Grid_1">

3. Build the project, a neutral satellite dll containing the string to be localized is generated.

Screenshot - fol.jpg

Now it is ok to localize this project to Chinese version. And the resources to be localized contain:

  • Display name of element in XAML
  • Information to be displayed by button in Code behind.

2.3 Localization

Steps:

  • Localize the resource in Code behind:

1. Create resource file for Chinese version in resx.

Create a copy according to GlobalizationTestDlgResource.en-US.resx:

Screenshot - zh.jpg

And update the string value:

Screenshot - zn.jpg

2. Generate the file in resources format by using SDK cmd.

Resgen GlobalizationTestDlgResource.zh-cn.resx (Command line, file name is an example)

And then a file "GlobalizationTestDlgResource.zh-cn.resources" is generated. And then rename it according to the neutral resources file in folder obj\Debug.

Neutral resources file:GlobalizationTestDlg.resource.GlobalizationTestDlgResource.en-US.resources

The final file contain the localized resource in code behind:

GlobalizationTestDlg.resource.GlobalizationTestDlgResource.zh-cn.resources

  • Localize the resource in XAML

    1. Parse en-US\GlobalizationTestDlg.resources.dll and generate a CSV file containing the resource to be localized by using LocBaml.

    Locbaml /parse

    2. Localized the resource in the CSV file generated by the above step. Please refer to Table 1-1 of Appendix to get the specification for each column in CSV file.

    3. Generate the file in resources format according to the file "GlobalizationTestDlg.g.en-US.resources" in folder "obj/Debug" by using LocBaml.

LocBaml /generate:GlobalizationTestDlg.g.en-US.resources /tran:GlobalizationTestDlg.resource.CSV /cul:zh-cn /out:.

  • The final file contains the localized resource in XAML:

GlobalizationTestDlg.g.zh-CN.resources

  • Link the two resources files by using SDK cmd.

al /template:"GlobalizationTestDlg.exe" /embed:GlobalizationTestDlg.g.zh-CN.resources /embed:GlobalizationTestDlg.resource.GlobalizationTestDlgResource.zh-cn.resources /culture:zh-cn /out:.\zh-cn\GlobalizationTestDlg.resources.dll

After the steps, a Chinese satellite dll is generated.

The .NET framework automatically chooses which satellite resources assembly to load based on the application's Thread.CurrentThread.CurrentUICulture. This defaults to the culture of your Windows OS. So if you are using Chinese Windows, the zh-cn\ GlobalizationTestDlg.resources.dll loads, if you are using English Windows, the en-US\ GlobalizationTestDlg.resources.dll loads.

3. Appendix

Table1-1 Columns 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.

Table1-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

Table1-3 Tool specification

Tool

Specification

LocBaml

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

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

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

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

 
Question.resource file is not generated Pinmemberkksrinivasan8926-Sep-13 21:10 
AnswerAlternative approach with "production-ready" tools Pinmemberjbe822411-May-09 9:29 
GeneralRe: Alternative approach with "production-ready" tools Pinmemberabdurahman ibn hattab24-Oct-09 18:25 
GeneralBest Localization Plug-in for Visual Studio. PinmemberAlexander Nesterenko17-Dec-08 21:31 
GeneralRe: Best Localization Plug-in for Visual Studio. Pinmembersamuellis9-Aug-12 5:36 
QuestionCan you explain the second part of the diagram PinmemberMember 39994499-Oct-08 4:46 
Generalnice article, but this one can work with .resx files PinmemberSeriousM10-May-08 15:26 
GeneralYou're ZipFile doesn't work PinmemberZipgun1-Mar-08 2:20 
GeneralRe: You're ZipFile doesn't work Pinmemberjonathan.cho10-Aug-08 20:50 
GeneralRe: You're ZipFile doesn't work Pinmemberiliberis12-Sep-10 14:00 

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
Web04 | 2.8.140709.1 | Last Updated 25 Apr 2007
Article Copyright 2007 by zhouyuhui
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid