1. 概述
该文章介绍了一种国际化和本地化一个WPF工程的方法,并给出了一个Sample。该解决方法是根据SDK Documents以及CodeProject.com网站的文章总结出来的。
WPF程序会自动根据OS的版本选择资源程序集,如果本地程序集不在,则使用默认程序集资源。可以通过代码的形式让WPF程序使用某一个版本的资源。本文介绍的方法不需要再对工程进行编译,可以直接获得资源集。
该方法中需要用到的几个工具:
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 (命名任意)
<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 350.25pt; HEIGHT: 34.5pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:/Users/hp/AppData/Local/Temp/msohtml1/01/clip_image001.png"></imagedata></shape>
<shape id="_x0000_i1026" style="WIDTH: 339.75pt; HEIGHT: 37.5pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:/Users/hp/AppData/Local/Temp/msohtml1/01/clip_image003.png"><font size="2"></font></imagedata></shape>
步骤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/56e<chmetcnv w:st="on" unitname="a" sourcevalue="5" hasspace="False" negative="False" numbertype="1" tcsc="0">5a</chmetcnv><chmetcnv w:st="on" unitname="C" sourcevalue="5" hasspace="False" negative="False" numbertype="1" tcsc="0">5c</chmetcnv>8<chmetcnv w:st="on" unitname="C" sourcevalue="6" hasspace="False" negative="True" numbertype="1" tcsc="0">-6c</chmetcnv>96-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<chmetcnv w:st="on" unitname="a" sourcevalue="48" hasspace="False" negative="True" numbertype="1" tcsc="0">-48a</chmetcnv>4-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)]
).
分享到:
相关推荐
wpf国际化
wpf国际化、本地化、多语言。.net,xaml。
在WPF中界面国际化很容易实现,但是数据国际化确实是一个难题。为了解决项目中的需求,我设计了解决方案。 思路: 1.在数据库中,需要国际化的字段存放国际化key值; 2.在xaml中创建多套国际化,key为数据库中的值...
官方实例,WPF实例程序,命令,动画,资源,样式及模板,本地化,拖拽,绑定,希望对初学WPF的童鞋有用...
WPF 窗体最大化、最小化、还原 | WPF 最大化/最小化 按钮图标切换
本资源描述了.net 桌面架构WPF显示界面汉化以及中英文切换方法,包含先关代码及操作详细步骤,亲测,可用,希望对您学习有帮助
wpf 多语言 国际化 demo c# wpf 多语言 例子 不能下联系我.http://www.baiyuxiong.com
WPF个性化
wpf 实现程序运行中动态切换程序语言,此方法非常简单,实现方式是扩展标记语言实现。
在创建WPF应用的时候,你第一个看到的就是窗体类。它作为窗体的基础,提供标准的边框、工具条、最大化、最小化和关闭按钮。WPF窗体是XAML文件和后台代码文件的混合体。 本文将详细介绍关于WPF窗体最大化问题的相关...
WPF本地化LocBaml.exe文件,用于本地化翻译,支持多国语言
WPF托盘化最小化
新手学习的第一个WPF程序 新手学习WPF可以参考的第一个WPF程序FirstWPF
WPF学习1:建立WPF第一个窗体控。便于WPF新手快速入门。
WPF本地化工具,是我自己使用工具,肯定可以使用。使用请看我的博客https://blog.csdn.net/xionglifei2014/article/details/80224421
WPF本地化程序 LocBaml 用它可以生成对应的csv或txt文件。
WPF语言国际化
一个WPF例程一个WPF例程大家共同学习
VS WPF 工程 设置git编译文件忽略上传
使用标准RESX文件本地化文本,图像和任何其他WPF属性