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
<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.
2. Create some elements in XAML.
3. Add event to button "ShowMsg"
1
<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.
<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
<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.
-
1. Modified the project file and AssemblyInfo.cs
Add <UICulture>en-US</UICulture> in GlobalizationTestDlg.csproj as neutral culture of project.
<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.
Now it is ok to localize this project to Chinese version. And the resources to be localized contain:
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:
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
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">
GlobalizationTestDlg.g.zh-CN.resources
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.
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.