Click here to Skip to main content
15,860,844 members
Articles / Programming Languages / C#
Article

Sample for globalization and localization in WPF

Rate me:
Please Sign up or sign in to vote.
3.83/5 (11 votes)
24 Apr 20075 min read 96K   2.9K   27   10
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<chmetcnv tcsc="0" numbertype="1" negative="True" hasspace="True" sourcevalue="3" unitname="in" w:st="on">-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<shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><stroke joinstyle="miter"><path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"><lock v:ext="edit" aspectratio="t"><shape id="_x0000_i1029" style="WIDTH: 414.75pt; HEIGHT: 133.5pt" type="#_x0000_t75"><imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\01\clip_image001.png">

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

<shape id="_x0000_i1032" style="WIDTH: 414.75pt; HEIGHT: 45pt" type="#_x0000_t75"><imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\01\clip_image007.png">

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

    <shape id="_x0000_i1033" style="WIDTH: 267pt; HEIGHT: 150pt" type="#_x0000_t75"><imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\01\clip_image009.png">

    2. Open GlobalizationTestDlgResource.resx and add string

    Screenshot - msg.jpg

    <shape id="_x0000_i1034" style="WIDTH: 321pt; HEIGHT: 111pt" type="#_x0000_t75"><imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\01\clip_image011.png">

    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

<shape id="_x0000_i1036" style="WIDTH: 267.75pt; HEIGHT: 59.25pt" type="#_x0000_t75"><imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\01\clip_image015.png">

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

<shape id="_x0000_i1037" style="WIDTH: 415.5pt; HEIGHT: 20.25pt" type="#_x0000_t75"><imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\01\clip_image017.png">

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

<shape id="_x0000_i1038" style="WIDTH: 414.75pt; HEIGHT: 20.25pt" type="#_x0000_t75"><imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\01\clip_image019.png">

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

    <shape id="_x0000_i1026" style="WIDTH: 414.75pt; HEIGHT: 44.25pt" type="#_x0000_t75"><imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\01\clip_image027.png">

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

<shape id="_x0000_i1025" style="WIDTH: 415.5pt; HEIGHT: 57.75pt" type="#_x0000_t75"><imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\01\clip_image029.png">

  • 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


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

 
Question.resource file is not generated Pin
kksrinivasan8926-Sep-13 21:10
kksrinivasan8926-Sep-13 21:10 
AnswerAlternative approach with "production-ready" tools Pin
jbe822411-May-09 9:29
jbe822411-May-09 9:29 
GeneralRe: Alternative approach with "production-ready" tools Pin
abdurahman ibn hattab24-Oct-09 18:25
abdurahman ibn hattab24-Oct-09 18:25 
GeneralBest Localization Plug-in for Visual Studio. Pin
Alexander Nesterenko17-Dec-08 21:31
Alexander Nesterenko17-Dec-08 21:31 
GeneralRe: Best Localization Plug-in for Visual Studio. Pin
samuellis9-Aug-12 5:36
professionalsamuellis9-Aug-12 5:36 
QuestionCan you explain the second part of the diagram Pin
Asif Abdulla9-Oct-08 4:46
Asif Abdulla9-Oct-08 4:46 
Generalnice article, but this one can work with .resx files Pin
SeriousM10-May-08 15:26
SeriousM10-May-08 15:26 
GeneralYou're ZipFile doesn't work Pin
Emmanuel Provost1-Mar-08 2:20
Emmanuel Provost1-Mar-08 2:20 
GeneralRe: You're ZipFile doesn't work Pin
jonathan.cho10-Aug-08 20:50
jonathan.cho10-Aug-08 20:50 
GeneralRe: You're ZipFile doesn't work Pin
iliberis12-Sep-10 14:00
iliberis12-Sep-10 14:00 

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.