WPF 的全球化Globalization for WPF

本主题介绍编写全局市场 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 应用程序时应注意的问题。This topic introduces issues that you should be aware of when writing Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) applications for the global market. 全球化编程元素在 System.Globalization 命名空间的 .NET 中定义。The globalization programming elements are defined in .NET in the System.Globalization namespace.

XAML 全球化XAML Globalization

Extensible Application Markup Language (XAML)基于 XML,并利用 XML 规范中定义的全球化支持。Extensible Application Markup Language (XAML) is based on XML and takes advantage of the globalization support defined in the XML specification. 以下部分介绍了一些您应该注意的 XAMLXAML 功能。The following sections describe some XAMLXAML features that you should be aware of.

字符引用Character References

字符引用以十进制或十六进制形式为其表示的特定 Unicode 字符提供 UTF16 代码单元。A character reference gives the UTF16 code unit of the particular Unicode character it represents, in either decimal or hexadecimal. 下面的示例显示了一个用于哥普特大写字母 HORI 或 "Ϩ" 的十进制字符引用:The following example shows a decimal character reference for the COPTIC CAPITAL LETTER HORI, or 'Ϩ':

Ϩ

下面的示例演示十六进制字符引用。The following example shows a hexadecimal character reference. 请注意,它在十六进制数字前面有一个xNotice that it has an x in front of the hexadecimal number.

Ϩ

EncodingEncoding

XAMLXAML 支持的编码为 ASCII、Unicode UTF-16 和 UTF-8。The encoding supported by XAMLXAML are ASCII, Unicode UTF-16, and UTF-8. 编码语句位于 XAMLXAML 文档的开头。The encoding statement is at the beginning of XAMLXAML document. 如果不存在编码特性,并且没有任何字节顺序,则分析器默认为 UTF-8。If no encoding attribute exists and there is no byte-order, the parser defaults to UTF-8. UTF-8 和 UTF-16 都是首选编码。UTF-8 and UTF-16 are the preferred encodings. 不支持 UTF-7。UTF-7 is not supported. 下面的示例演示如何在 XAMLXAML 文件中指定 UTF-8 编码。The following example demonstrates how to specify a UTF-8 encoding in a XAMLXAML file.

?xml encoding="UTF-8"?

语言特性Language Attribute

XAMLXAML 使用xml: lang表示元素的 language 特性。uses xml:lang to represent the language attribute of an element. 若要利用 CultureInfo 类,language 特性值需要是由 CultureInfo预定义的区域性名称之一。To take advantage of the CultureInfo class, the language attribute value needs to be one of the culture names predefined by CultureInfo. xml:lang 在元素树中可继承(可按 XML 规则继承,但并不一定这样继承,因为存在依赖属性继承);在未明确赋值的情况下,其默认值为空字符串。xml:lang is inheritable in the element tree (by XML rules, not necessarily because of dependency property inheritance) and its default value is an empty string if it is not assigned explicitly.

语言特性对指定方言非常有用。The language attribute is very useful for specifying dialects. 例如,法语在法国、魁北克、比利时和瑞士的拼写、词汇和发音各不相同。For example, French has different spelling, vocabulary, and pronunciation in France, Quebec, Belgium, and Switzerland. 同时,中文、日语和朝鲜语使用 Unicode 共享码位,但表意形状不同,它们使用完全不同的字体。Also Chinese, Japanese, and Korean share code points in Unicode, but the ideographic shapes are different and they use totally different fonts.

以下 可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML) 示例使用 fr-CA language 特性指定加拿大法语。The following 可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML) example uses the fr-CA language attribute to specify Canadian French.

<TextBlock xml:lang="fr-CA">Découvrir la France</TextBlock>

UnicodeUnicode

XAMLXAML 支持所有 Unicode 功能,包括代理项。supports all Unicode features including surrogates. 只要字符集可以映射到 Unicode,就支持该字符集。As long as the character set can be mapped to Unicode, it is supported. 例如,GB18030 中引入了某些可映射到中文、日语和朝鲜语 (CFK) 扩展 A 和 B 以及代理项对的字符,因此完全受支持。For example, GB18030 introduces some characters that are mapped to the Chinese, Japanese, and Korean (CFK) extension A and B and surrogate pairs, therefore it is fully supported. WPFWPF 应用程序可以使用 StringInfo 操作字符串,而无需了解它们是否具有代理项对或组合字符。A WPFWPF application can use StringInfo to manipulate strings without understanding whether they have surrogate pairs or combining characters.

使用 XAML 设计国际用户界面Designing an International User Interface with XAML

本部分介绍编写应用程序时应考虑的 用户界面 (UI)user interface (UI) 功能。This section describes 用户界面 (UI)user interface (UI) features that you should consider when writing an application.

国际化文本International Text

WPFWPF 包括针对所有 Microsoft .NET Framework 支持的书写系统的内置处理。includes built-in processing for all Microsoft .NET Framework supported writing systems.

目前支持以下脚本:The following scripts are currently supported:

  • 阿拉伯语Arabic

  • 孟加拉语Bengali

  • 梵文Devanagari

  • 西里尔文Cyrillic

  • 希腊语Greek

  • 古吉拉特语Gujarati

  • 果鲁穆奇语Gurmukhi

  • 希伯来语Hebrew

  • 表意文字脚本Ideographic scripts

  • 卡纳达语Kannada

  • 老挝语Lao

  • 拉丁语Latin

  • 马拉雅拉姆语Malayalam

  • 蒙古语Mongolian

  • 奥里亚语Odia

  • 叙利亚语Syriac

  • 泰米尔语Tamil

  • 泰卢固语Telugu

  • 塔安那文Thaana

  • 泰语*Thai*

  • 藏语Tibetan

*此版本支持显示和编辑泰语文本,但不支持断词。*In this release the display and editing of Thai text is supported; word breaking is not.

目前不支持以下脚本:The following scripts are not currently supported:

  • 高棉语Khmer

  • 古朝鲜语Korean Old Hangul

  • 缅甸语Myanmar

  • 僧伽罗语Sinhala

所有书写系统引擎都支持 OpenType 字体。All the writing system engines support OpenType fonts. OpenType 字体可以包含 OpenType 布局表,它们使字体创建者能够设计更好的国际化和高端版式字体。OpenType fonts can include the OpenType layout tables that enable font creators to design better international and high-end typographic fonts. OpenType 字体布局表包含有关符号替换、标志符号定位、对齐和基线定位的信息,使文本处理应用程序能够改进文本布局。The OpenType font layout tables contain information about glyph substitutions, glyph positioning, justification, and baseline positioning, enabling text-processing applications to improve text layout.

OpenType 字体允许使用 Unicode 编码处理大字形集。OpenType fonts allow the handling of large glyph sets using Unicode encoding. 这种编码享有广泛的国际支持,并支持版式字形变体。Such encoding enables broad international support as well as for typographic glyph variants.

WPFWPF 文本呈现由支持分辨率独立性的 Microsoft ClearType 子像素技术提供支持。text rendering is powered by Microsoft ClearType sub-pixel technology that supports resolution independence. 这极大地提高了可读性,并为所有脚本提供了支持高质量杂志样式文档的功能。This significantly improves legibility and provides the ability to support high quality magazine style documents for all scripts.

国际化布局International Layout

WPFWPF 提供一种支持水平、双向和垂直布局的简便方式。provides a very convenient way to support horizontal, bidirectional, and vertical layouts. 在演示框架中,FlowDirection 属性可用于定义布局。In presentation framework the FlowDirection property can be used to define layout. 流方向模式包括:The flow direction patterns are:

  • LeftToRight - 适用于拉丁语和东亚语等语言的水平布局。LeftToRight - horizontal layout for Latin, East Asian and so forth.

  • RightToLeft - 适用于阿拉伯语和希伯来语等语言的双向布局。RightToLeft - bidirectional for Arabic, Hebrew and so forth.

开发可本地化的应用程序Developing Localizable Applications

编写供全球使用的应用程序时,应牢记应用程序必须可本地化。When you write an application for global consumption you should keep in mind that the application must be localizable. 以下主题指出了若干注意事项。The following topics point out things to consider.

多语言用户界面Multilingual User Interface

多语言用户界面(MUI)是用于将用户界面从一种语言切换到另一种语言的 Microsoft 支持。Multilingual User Interfaces (MUI) is a Microsoft support for switching UIs from one language to another. WPFWPF 应用程序使用程序集模型来支持 MUI。A WPFWPF application uses the assembly model to support MUI. 一个应用程序包含非特定语言程序集和与语言相关的附属资源程序集。One application contains language-neutral assemblies as well as language-dependent satellite resource assemblies. 入口点是主程序集中的托管 .EXE。The entry point is a managed .EXE in the main assembly. WPFWPF 资源加载器利用框架的资源管理器来支持资源查找和回退。resource loader takes advantage of the Framework's resource manager to support resource lookup and fallback. 多个语言附属程序集使用同一个主程序集。Multiple language satellite assemblies work with the same main assembly. 加载的资源程序集取决于当前线程的 CurrentUICultureThe resource assembly that is loaded depends on the CurrentUICulture of the current thread.

可本地化的用户界面Localizable User Interface

WPFWPF 应用程序使用 XAMLXAML 来定义其 UIUIapplications use XAMLXAML to define their UIUI. 通过 XAMLXAML,开发人员可使用一组属性和逻辑指定对象的层次结构。XAMLXAML allows developers to specify a hierarchy of objects with a set of properties and logic. XAMLXAML 的主要用途是开发 WPFWPF 应用程序,但它可用于指定任何公共语言运行时(CLR)对象的层次结构。The primary use of XAMLXAML is to develop WPFWPF applications but it can be used to specify a hierarchy of any common language runtime (CLR) objects. 大多数开发人员都使用 XAMLXAML 来指定应用程序的 UIUI,并使用编程语言C# (例如)来响应用户交互。Most developers use XAMLXAML to specify their application's UIUI and use a programming language such as C# to react to user interaction.

从资源的角度来看,旨在描述依赖于语言的 UIUIXAMLXAML 文件是一个资源元素,因此,其最终分发格式必须可本地化以支持国际语言。From a resource point of view, a XAMLXAML file designed to describe a language-dependent UIUI is a resource element and therefore its final distribution format must be localizable to support international languages. 由于 XAMLXAML 无法处理事件,许多 XAMLXAML 应用程序包含执行此操作的代码块。Because XAMLXAML cannot handle events many XAMLXAML applications contain blocks of code to do this. 有关详细信息,请参阅XAML 概述(WPF)For more information, see XAML Overview (WPF). XAMLXAML 文件标记为 XAML 的 BAML 形式后,代码会被去除并编译到不同的二进制文件中。Code is stripped out and compiled into different binaries when a XAMLXAML file is tokenized into the BAML form of XAML. BAML 形式的 XAML 文件、图像以及其他类型的托管资源对象将嵌入附属资源程序集中,该程序集可本地化为其他语言,如果不需要进行本地化,以上各项就会嵌入主程序集中。The BAML form of XAML files, images, and other types of managed resource objects are embedded in the satellite resource assembly, which can be localized into other languages, or the main assembly when localization is not required.

备注

WPFWPF 应用程序支持所有 FrameworkCLR 资源,包括字符串表、图像等。applications support all the FrameworkCLR resources including string tables, images, and so forth.

生成可本地化的应用程序Building Localizable Applications

本地化意味着将 UIUI 改编为不同的文化。Localization means to adapt a UIUI to different cultures. 若要使 WPFWPF 的应用程序可本地化,开发人员需要将所有可本地化的资源生成到一个资源程序集中。To make a WPFWPF application localizable, developers need to build all the localizable resources into a resource assembly. 资源程序集本地化为不同的语言,代码隐藏使用资源管理 API 进行加载。The resource assembly is localized into different languages, and the code-behind uses resource management API to load. WPFWPF 应用程序所需的文件之一是项目文件(proj)。One of the files required for a WPFWPF application is a project file (.proj). 应用程序中使用的所有资源都应包括在项目文件中。All resources that you use in your application should be included in the project file. 以下 .csproj 文件示例演示如何执行此操作。The following example from a .csproj file shows how to do this.

<Resource Include="data\picture1.jpg"/>
<EmbeddedResource Include="data\stringtable.en-US.restext"/>

若要在应用程序中使用资源,请实例化 ResourceManager 并加载要使用的资源。To use a resource in your application instantiate a ResourceManager and load the resource you want to use. 下面的示例演示如何执行此操作。The following example demonstrates how to do this.

void OnClick(object sender, RoutedEventArgs e)
{
  ResourceManager rm = new ResourceManager ("MySampleApp.data.stringtable",
       Assembly.GetExecutingAssembly());
  Text1.Text = rm.GetString("Message");
}

在本地化的应用程序中使用 ClickOnceUsing ClickOnce with Localized Applications

ClickOnce 是 Visual Studio 2005 附带的一种新的 Windows 窗体部署技术。ClickOnce is a new Windows Forms deployment technology that will ship with Visual Studio 2005. 通过该技术可安装应用程序和升级 Web 应用程序。It enables application installation and upgrading of Web applications. 对使用 ClickOnce 部署的应用程序进行本地化后,只能在本地化的区域性中查看该应用程序。When an application that was deployed with ClickOnce is localized it can only be viewed on the localized culture. 例如,如果已部署的应用程序本地化为日语,则只能在日语版 Microsoft Windows 上查看该应用程序。For example, if a deployed application is localized to Japanese it can only be viewed on Japanese Microsoft Windows not on English Windows. 这会带来一个问题,因为它是日语用户运行英语版本的 Windows 的常见方案。This presents a problem because it is a common scenario for Japanese users to run an English version of Windows.

此问题的解决方案是设置非特定语言回退特性。The solution to this problem is setting the neutral language fallback attribute. 应用程序开发人员可选择从主程序集中删除资源,并指定可在特定区域性对应的附属程序集中找到该资源。An application developer can optionally remove resources from the main assembly and specify that the resources can be found in a satellite assembly corresponding to a specific culture. 若要控制此过程,请使用 NeutralResourcesLanguageAttributeTo control this process use the NeutralResourcesLanguageAttribute. NeutralResourcesLanguageAttribute 类的构造函数具有两个签名,一个签名采用 UltimateResourceFallbackLocation 参数来指定 ResourceManager 应提取回退资源的位置:主程序集或附属程序集。The constructor of the NeutralResourcesLanguageAttribute class has two signatures, one that takes an UltimateResourceFallbackLocation parameter to specify the location where the ResourceManager should extract the fallback resources: main assembly or satellite assembly. 下面的示例演示如何使用此特性。The following example shows how to use the attribute. 对于最终回退位置,代码会使 ResourceManager 在当前正在执行的程序集的目录的 "de" 子目录中查找资源。For the ultimate fallback location, the code causes the ResourceManager to look for the resources in the "de" subdirectory of the directory of the currently executing assembly.

[assembly: NeutralResourcesLanguageAttribute(
    "de" , UltimateResourceFallbackLocation.Satellite)]

另请参阅See also