![]() |
Platforms, Frameworks & Libraries »
Windows Presentation Foundation »
General
Beginner
Sample for globalization and localization in WPFBy zhouyuhuiDecription how to globalize and localize in WPF. |
C# 3.0, Windows, .NET 3.0VS2005, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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
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.

Steps:
1. Create a project in WPF.

2. Create some elements in XAML.

3. Add event to button "ShowMsg"
1
The neutral version of application should be created so that our localization work can be carried on after application is built.
Steps:
1. Create a resource file in resx format to save the string in code behind.

2. Open GlobalizationTestDlgResource.resx and add string

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

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.

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.

Now it is ok to localize this project to Chinese version. And the resources to be localized contain:
Steps:
1. Create resource file for Chinese version in resx.
Create a copy according to GlobalizationTestDlgResource.en-US.resx:
And update the string value:
![]()
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
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:.
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.
|
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. |
|
File type |
Specification |
|
.resx |
XML-based resource format |
|
.resources |
Can be embedded in a runtime binary executable or compiled into satellite assemblies |
|
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.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 24 Apr 2007 Editor: |
Copyright 2007 by zhouyuhui Everything else Copyright © CodeProject, 1999-2009 Web11 | Advertise on the Code Project |