国际化和本地化一个WPF工程

来源:互联网 发布:mac 拖拽文件 编辑:程序博客网 时间:2024/04/28 06:52

1. 概述

       该文章介绍了一种国际化和本地化一个WPF工程的方法,并给出了一个Sample。该解决方法是根据SDK Documents以及CodeProject.com网站的文章总结出来的。

      WPF程序会自动根据OS的版本选择资源程序集,如果本地程序集不在,则使用默认程序集资源。可以通过代码的形式让WPF程序使用某一个版本的资源。本文介绍的方法不需要再对工程进行编译,可以直接获得资源集。

      该方法中需要用到的几个工具:

  •   MSBuild.exe (Microsoft build engine)
  •  LocBaml.exe
  •  Resgen.exe (Resource File Generator)
  • Al.exe (Assembly Linker)

2. 国际化

2.1 字符串处理

     在WPF工程中非XAML的Code中,通常有两种类型的字符串:一种是显示给用户的需要本地化的,一种是程序内部使用的不需要本地化的。对于这两种字符串,我们可以采取下述措施进行:

2.2 国际化

国际化是可以在多种语言区域运行的应用程序的设计与开发。

步骤1:创建一个WPF程序

使用Properties/Resources.resx来保存需要本地化的字符串:

使用NonLocalizableString.cs来保存不需要本地化的字符串:

步骤2:创建下图所示的UI

步骤3:创建ShowMsg按钮的点击事件函数

步骤4:生成XAML文件中控件的Uid

打开SDK的命令行工具

使用如下命令生成并检查Uid:

         Msbuild /t:updateuid LocalizationTest.csproj  // generate Uid
Msbuild /t:checkuid LocalizationTest.csproj  // check Uid

步骤5:Build工程,一个可以进行本地化的工程就建立成功了。

3. 本地化

  应用程序要支持某特定的语言,就需要把程序资源翻译成本地版本,这个翻译的过程就是本地化。对于WPF工

程的本地化需要下列工具:

1.           LocBaml.exe

2.           Resgen.exe

3.           Al.exe

另外还需要一些必要的辅助文件:

Table 3-1 Essential files for localization

File name

Location

Comments

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

 

obj/Release

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

Resources.resx

Properties/

Localizable string in code behind

 注意:该Sample是对Release版本本地化,为了简化,在命令行忽略了文件全路径。

 

3.1 本地化非XAML资源

 

步骤1: 创建一个 Properties/Resources.resx 的拷贝,翻译相应的资源

资源文件:Resources.zh-cn.resx (命名任意)

步骤2: 使用SDK的resgen.exe命令生成二进制资源文件

     Resgen Resources.zh-cn.resx

步骤3: 对步骤2生成的文件重名名:

     Resources.zh-cn.resources à LocalizationTest.resource.LocalizationTestResource.zh-cn.resources

3.2 本地化XAML资源

步骤1: 使用LocBaml工具从主程序集中抽取XAML资源

LocBaml /parse LocalizationTest.exe

执行上述命令会生成一个CSV格式的文件,需要把LocBaml拷贝到主程序集文件夹

步骤2: 编辑该CSV文件,根据4.4提供的CSV格式对需要本地化的资源进行翻译

步骤3: 使用LocBaml工具生成二进制资源文件

      LocBaml /generate LocalizationTest.g.resources /tran: LocalizationTest.CSV /cul:zh-cn /out:./zh-cn/

步骤4:.对步骤3产生的二进制资源文件重命名:

LocalizationTest.g.resources à LocalizationTest.g.zh-cn.resources

3.3 生成资源DLL

使用SDK工具“al.exe”,执行如下命令链接两个二进制资源。Al /template:LocalizationTest.exe /embed: LocalizationTest.Properties.Resources.zh-cn.resources

/embed:LocalizationTest.g.zh-cn.resources /culture:zh-cn /out:LocalizationTest.resources.dll

 

创建一个“zh-cn” 文件保存 “LocalizationTest.resources.dll”.

 

经过以上步骤一个中文版本创建成功,运行测试:

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

 

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-48a4-9dcd-ba1b89ee6653.htm#Some_Tips_for_Using_LocBaml.

 

4.2 Tools

Table 4-1 Tool specification

Tool

Specification

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.

LocBaml.exe

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

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

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.

4.3 File type specification

Table 4-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

 

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.

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

When you design a WPF–based UI, consider implementing these best practices:

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

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

l         Provide extra space in margins because localized text often requires more space. Extra space allows for possible overhanging characters.

l         Enable TextWrapping on TextBlock to avoid clipping.

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

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

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

 

Best Practices for WPF Localization

When you localize WPF–based applications, consider implementing these best practices:

l         Use localization comments to provide extra context for localizers.

l         Use localization attributes to control localization instead of selectively omitting Uid properties on elements. See Localization Attributes and Comments for more information.

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

1. Do not edit or change Uid properties after you begin localization.

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

3. Set the UltimateResourceFallback location in AssemblyInfo.* to specify the appropriate language for fallback (for example, [assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]).

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)]).

 

原创粉丝点击