WPF 全球化和本地化概述WPF Globalization and Localization Overview

当你将自己的产品限制为只能通过一种语言使用时,便将潜在的客户群限制为全球 65 亿人口中的一小部分。When you limit your product's availability to only one language, you limit your potential customer base to a fraction of our world’s 6.5 billion population. 如果想让自己的应用程序被全球用户所接受,那么对产品进行经济而有效的本地化将是赢得更多客户的最好、最经济的方法。If you want your applications to reach a global audience, cost-effective localization of your product is one of the best and most economical ways to reach more customers.

本概述介绍 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF)中的全球化和本地化。This overview introduces globalization and localization in Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF). 全球化是指设计和开发在多个地点执行的应用程序。Globalization is the design and development of applications that perform in multiple locations. 例如,全球化支持适用于不同区域性用户的本地化用户界面和区域数据。For example, globalization supports localized user interfaces and regional data for users in different cultures. WPFWPF 提供了全球化设计功能,包括自动布局、附属程序集以及本地化特性和注释。provides globalized design features, including automatic layout, satellite assemblies, and localized attributes and commenting.

本地化是针对应用程序所支持的特定区域性将应用程序资源转换为本地化版本的过程。Localization is the translation of application resources into localized versions for the specific cultures that the application supports. WPFWPF中进行本地化时,使用 System.Windows.Markup.Localizer 命名空间中的 Api。When you localize in WPFWPF, you use the APIs in the System.Windows.Markup.Localizer namespace. 这些 Api 支持LocBaml 工具的示例命令行工具。These APIs power the LocBaml Tool Sample command-line tool. 有关如何生成和使用 LocBaml 的信息,请参阅本地化应用程序For information about how to build and use LocBaml, see Localize an Application.

在 WPF 中进行全球化和本地化的最佳做法Best Practices for Globalization and Localization in WPF

通过遵循本部分提供的 UI 设计和本地化相关提示,你可以充分利用中 WPFWPF 内置的大多数全球化和本地化功能。You can make the most of the globalization and localization functionality that is built into WPFWPF by following the UI design and localization-related tips that this section provides.

WPF UI 设计的最佳做法Best Practices for WPF UI Design

设计基于 WPFWPFUIUI时,请考虑实施以下最佳做法:When you design a WPFWPF–based UIUI, consider implementing these best practices:

  • UIUI 写入 XAMLXAML中;避免在代码中创建 UIUIWrite your UIUI in XAMLXAML; avoid creating UIUI in code. 使用 XAMLXAML创建 UIUI 时,可以通过内置的本地化 Api 公开此。When you create your UIUI by using XAMLXAML, you expose it through built-in localization APIs.

  • 避免使用绝对位置和固定大小对内容进行布局;请改用相对或自动调整大小。Avoid using absolute positions and fixed sizes to lay out content; instead, use relative or automatic sizing.

    • 使用 SizeToContent,并将宽度和高度设置为 AutoUse SizeToContent and keep widths and heights set to Auto.

    • 避免使用 CanvasUIUI进行布局。Avoid using Canvas to lay out UIUIs.

    • 使用 Grid 及其大小的共享功能。Use Grid and its size-sharing feature.

  • 在边距中提供额外的空间,因为本地化文本通常需要更多空间。Provide extra space in margins because localized text often requires more space. 额外空间为可能会延伸的字符预留了余地。Extra space allows for possible overhanging characters.

  • 启用 TextBlock 上的 TextWrapping 以避免剪辑。Enable TextWrapping on TextBlock to avoid clipping.

  • 设置 xml:lang 特性。Set the xml:lang attribute. 此属性描述特定元素及其子元素的区域性。This attribute describes the culture of a specific element and its child elements. 此属性的值更改 WPFWPF中多个功能的行为。The value of this property changes the behavior of several features in WPFWPF. 例如,它可以更改断字、拼写检查、数字替换、复杂脚本成型和字体回退的行为。For example, it changes the behavior of hyphenation, spell checking, number substitution, complex script shaping, and font fallback. 有关在 XAML 中设置 xml: Lang 处理的详细信息,请参阅适用于WPF 的全球化See Globalization for WPF for more information about setting the xml:lang Handling in XAML.

  • 创建自定义的复合字体,以更好地控制用于不同语言的字体。Create a customized composite font to obtain better control of fonts that are used for different languages. 默认情况下,WPFWPF 使用 Windows\Fonts 目录中的 GlobalUserInterface 字体。By default, WPFWPF uses the GlobalUserInterface.composite font in your Windows\Fonts directory.

  • 当你创建的导航应用程序可能在以从右向左格式呈现文本的区域性中进行本地化时,请显式设置每个页面的 FlowDirection,以确保页面不会从 NavigationWindow继承 FlowDirectionWhen 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.

  • 当你创建在浏览器外部托管的独立导航应用程序时,将初始应用程序的 StartupUri 设置为 NavigationWindow 而不是页(例如 <Application StartupUri="NavigationWindow.xaml">)。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">). 利用此设计,您可以更改窗口和导航栏的 FlowDirectionThis design enables you to change the FlowDirection of the Window and the navigation bar. 有关详细信息和示例,请参阅全球化主页示例For more information and an example, see Globalization Homepage Sample.

WPF 本地化的最佳做法Best Practices for WPF Localization

当你对基于 WPFWPF的应用程序进行本地化时,请考虑实施下列最佳做法:When you localize WPFWPF–based applications, consider implementing these best practices:

  • 使用本地化注释为本地化人员提供额外的上下文。Use localization comments to provide extra context for localizers.

  • 使用本地化特性来控制本地化,而不是选择性地省略元素上的 Uid 属性。Use localization attributes to control localization instead of selectively omitting Uid properties on elements. 有关详细信息,请参阅本地化特性和注释See Localization Attributes and Comments for more information.

  • 使用 msbuild -t:updateuid-t:checkuid 添加和检查 XAMLXAML中的 Uid 属性。Use msbuild -t:updateuid and -t:checkuid to add and check Uid properties in your XAMLXAML. 使用 Uid 属性跟踪开发和本地化之间的更改。Use Uid properties to track changes between development and localization. Uid 属性可帮助你本地化新的开发更改。Uid properties help you localize new development changes. 如果手动将 Uid 属性添加到 UIUI中,则该任务通常单调乏味且不太准确。If you manually add Uid properties to a UIUI, the task is typically tedious and less accurate.

    • 开始本地化后,请不要编辑或更改 Uid 属性。Do not edit or change Uid properties after you begin localization.

    • 不要使用重复的 Uid 属性(在使用复制和粘贴命令时请记住此提示)。Do not use duplicate Uid properties (remember this tip when you use the copy-and-paste command).

    • 设置 AssemblyInfo 中的 UltimateResourceFallback 位置,以便为回退指定适当的语言(例如,[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)])。Set the UltimateResourceFallback location in AssemblyInfo.* to specify the appropriate language for fallback (for example, [assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]).

      如果通过省略项目文件中的 <UICulture> 标记来决定在主程序集中包含源语言,请将 UltimateResourceFallback 位置设置为主程序集而不是附属程序集(例如 [assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)])。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 应用程序进行本地化Localize a WPF Application

WPFWPF 应用程序进行本地化时,有几个选项。When you localize a WPFWPF application, you have several options. 例如,可以将应用程序中的可本地化资源绑定到 XML 文件,将可本地化的文本存储在 resx 表中,或让本地化人员使用 可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML) 文件。For example, you can bind the localizable resources in your application to an XML file, store localizable text in resx tables, or have your localizer use 可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML) files. 本部分介绍使用 XAML 的 BAML 形式的本地化工作流,该工作流提供以下几个优点:This section describes a localization workflow that uses the BAML form of XAML, which provides several benefits:

  • 您可以在生成后进行本地化。You can localize after you build.

  • 您可以使用 XAML 的 BAML 形式的更早版本中的本地化更新为 XAML 的 BAML 形式的更高版本,以便您可以在开发时进行本地化。You can update to a newer version of the BAML form of XAML with localizations from an older version of the BAML form of XAML so that you can localize at the same time that you develop.

  • 可以在编译时验证原始源元素和语义,因为 XAML 的 BAML 形式是 XAMLXAML的编译形式。You can validate original source elements and semantics at compile time because the BAML form of XAML is the compiled form of XAMLXAML.

本地化生成过程Localization Build Process

开发 WPFWPF 应用程序时,本地化的生成过程如下所示:When you develop a WPFWPF application, the build process for localization is as follows:

  • 开发人员创建并其全球化 WPFWPF 应用程序。The developer creates and globalizes the WPFWPF application. 在开发人员将 <UICulture>en-US</UICulture> 的项目文件中,这样,在编译应用程序时,将生成一个非特定语言的主程序集。In the project file the developer sets <UICulture>en-US</UICulture> so that when the application is compiled, a language-neutral main assembly is generated. 此程序集具有一个附属 .resources.dll 文件,其中包含所有可本地化的资源。This assembly has a satellite .resources.dll file that contains all the localizable resources. 您可以根据需要将源语言保留在主程序集中,因为我们的本地化 Api 支持从主程序集进行提取。Optionally, you can keep the source language in the main assembly because our localization APIs support extraction from the main assembly.

  • 将该文件编译为生成时,XAMLXAML 将转换为 XAML 的 BAML 形式。When the file is compiled into the build, the XAMLXAML is converted to the BAML form of XAML. 非特定区域性 MyDialog.exe 和与区域性相关的(英语) MyDialog.resources.dll 文件将发布给英语客户。The culturally neutral MyDialog.exe and the culturally dependent (English) MyDialog.resources.dll files are released to the English-speaking customer.

本地化工作流Localization Workflow

本地化过程在生成未本地化 MyDialog.resources.dll 文件后开始。The localization process begins after the unlocalized MyDialog.resources.dll file is built. 原始 XAMLXAML 中的 UIUI 元素和属性通过使用 System.Windows.Markup.Localizer下的 Api 从 XAML 的 BAML 形式提取到键值对。The UIUI elements and properties in your original XAMLXAML are extracted from the BAML form of XAML into key-value pairs by using the APIs under System.Windows.Markup.Localizer. 本地化人员使用键/值对来对应用程序进行本地化。Localizers use the key-value pairs to localize the application. 在本地化完成之后,可以从新值生成一个新的 .resource.dll。You can generate a new .resource.dll from the new values after localization is complete.

键/值对的键是 x:Uid 开发人员置于原始 XAMLXAML中的值。The keys of the key-value pairs are x:Uid values that are placed by the developer in the original XAMLXAML. 这些 x:Uid 值使 API 能够跟踪和合并本地化期间开发人员和本地化人员之间发生的更改。These x:Uid values enable the API to track and merge changes that happen between the developer and the localizer during localization. 例如,如果在本地化人员开始本地化后,开发人员更改了 UIUI,则可以将开发更改与已完成的本地化工作合并,以便最小翻译工作丢失。For example, if the developer changes the UIUI after the localizer begins localizing, you can merge the development change with the already completed localization work so that minimal translation work is lost.

下图显示了一个基于 XAML 的 BAML 形式的典型本地化工作流。The following graphic shows a typical localization workflow that is based on the BAML form of XAML. 此关系图假设开发人员用英语编写应用程序。This diagram assumes the developer writes the application in English. 开发人员创建 WPF 应用程序并将其全球化。The developer creates and globalizes the WPF application. 开发人员将 <UICulture>en-US</UICulture> 的项目文件设置为在生成时,使用包含所有可本地化资源的附属. .resources 生成语言中性主程序集。In the project file the developer sets <UICulture>en-US</UICulture> so that on build, a language neutral main assembly gets generated with a satellite .resources.dll containing all localizable resources. 或者,因为 WPF 本地化 API 支持从主程序集进行提取,所以还可以保留主程序集中的源语言。Alternately, one could keep the source language in the main assembly because WPF localization APIs support extraction from the main assembly. 生成过程结束之后,XAML 会编译为 BAML。After the build process, the XAML get compiled into BAML. 将向说英语的客户提供非特定区域性的 MyDialog.exe.resources.dll。The culturally neutral MyDialog.exe.resources.dll get shipped to the English speaking customer.

显示本地化工作流的关系图。

显示未本地化工作流的关系图。

WPF 本地化示例Examples of WPF Localization

本部分包含可帮助你了解如何生成和本地化 WPFWPF 应用程序的本地化应用程序的示例。This section contains examples of localized applications to help you understand how to build and localize WPFWPF applications.

“运行”对话框示例Run Dialog Box Example

下图显示 "运行" 对话框示例的输出。The following graphics show the output of the Run dialog box sample.

英语:English:

显示 "英语运行" 对话框的屏幕截图。

德语:German:

显示德语 "运行" 对话框的屏幕截图。

设计全球化“运行”对话框Designing a Global Run Dialog Box

此示例通过使用 WPFWPFXAMLXAML生成一个 "运行" 对话框。This example produces a Run dialog box by using WPFWPF and XAMLXAML. 此对话框等效于 Microsoft Windows "开始" 菜单中提供的 "运行" 对话框。This dialog box is equivalent to the Run dialog box that is available from the Microsoft Windows Start menu.

生成全球化对话框的一些要点包括:Some highlights for making global dialog boxes are:

自动布局Automatic Layout

在 Window1.xaml 中:In Window1.xaml:

<Window SizeToContent="WidthAndHeight">

以前的 Window 属性会根据内容大小自动调整窗口大小。The previous Window property automatically resizes the window according to the size of the content. 此属性可以防止窗口切断在本地化之后大小增加的内容;它还可以在内容由于本地化而大小减小时删除不必要的空格。This property prevents the window from cutting off content that increases in size after localization; it also removes unneeded space when content decreases in size after localization.

<Grid x:Uid="Grid_1">

要使 WPFWPF 的本地化 Api 正常工作,Uid 属性是必需的。Uid properties are needed in order for WPFWPF localization APIs to work correctly.

WPFWPF 本地化 Api 使用这些方法跟踪 用户界面 (UI)user interface (UI)的开发和本地化之间的更改。They are used by WPFWPF localization APIs to track changes between the development and localization of the 用户界面 (UI)user interface (UI). Uid 属性使你能够将较新版本的 UIUIUIUI的较旧本地化合并在一起。Uid properties enable you to merge a newer version of the UIUI with an older localization of the UIUI. 可以通过在命令行界面中运行 msbuild -t:updateuid RunDialog.csproj 来添加 Uid 属性。You add a Uid property by running msbuild -t:updateuid RunDialog.csproj in a command shell. 这是添加 Uid 属性的建议方法,因为手动添加它们通常是耗时且不太准确的。This is the recommended method of adding Uid properties because manually adding them is typically time-consuming and less accurate. 可以通过运行 msbuild -t:checkuid RunDialog.csproj来检查 Uid 属性是否正确设置。You can check that Uid properties are correctly set by running msbuild -t:checkuid RunDialog.csproj.

通过使用 Grid 控件来构造 UIUI,这是在 WPFWPF中利用自动布局的有用控件。The UIUI is structured by using the Grid control, which is a useful control for taking advantage of the automatic layout in WPFWPF. 请注意,对话框拆分成三行五列。Note that the dialog box is split into three rows and five columns. 不是行和列定义中的一个具有固定的大小;因此,位于每个单元格中的 UIUI 元素可以在本地化过程中适应大小的增加和减小。Not one of the row and column definitions has a fixed size; hence, the UIUI elements that are positioned in each cell can adapt to increases and decreases in size during localization.

<Grid.ColumnDefinitions>
  <ColumnDefinition x:Uid="ColumnDefinition_1" />
  <ColumnDefinition x:Uid="ColumnDefinition_2" />

放置打开的: 标签和 ComboBox 的前两列使用 UIUI 总宽度的10%。The first two columns where the Open: label and ComboBox are placed use 10 percent of the UIUI total width.

  <ColumnDefinition x:Uid="ColumnDefinition_3" SharedSizeGroup="Buttons" />
  <ColumnDefinition x:Uid="ColumnDefinition_4" SharedSizeGroup="Buttons" />
  <ColumnDefinition x:Uid="ColumnDefinition_5" SharedSizeGroup="Buttons" />
</Grid.ColumnDefinitions>

请注意,此示例使用 Grid的共享大小调整功能。Note that of the example uses the shared-sizing feature of Grid. 最后三列通过将其放在同一 SharedSizeGroup中来利用这一点。The last three columns take advantage of this by placing themselves in the same SharedSizeGroup. 正如属性名称所示,此属性允许不同的列采用相同大小。As one would expect from the name of the property, this allows the columns to share the same size. 因此,当 "浏览 ..."已本地化为较长的字符串 "Durchsuchen ... ...",所有按钮的宽度都将增加,而不是使用较小的 "确定" 按钮和不成比例的 "Durchsuchen ... ..."鼠标.So when the "Browse…" gets localized to the longer string "Durchsuchen…", all buttons grow in width instead of having a small "OK" button and a disproportionately large "Durchsuchen…" button.

xml:langxml:lang

xml:lang="en-US"

请注意, XAML 中的 xml: Lang 处理置于 UIUI的根元素中。Notice the xml:lang Handling in XAML placed at the root element of the UIUI. 此属性描述给定元素及其子元素的区域性。This property describes the culture of a given element and its children. 此值由 WPFWPF 中的多个功能使用,并且应在本地化过程中相应地进行更改。This value is used by several features in WPFWPF and should be changed appropriately during localization. 此值会更改在断字以及对字词进行拼写检查时所使用的字典。This value changes what language dictionary is use to hyphenate and spell check words. 它还会影响数字的显示以及字体回退系统选择所用字体的方式。It also affects the display of digits and how the font fallback system selects which font to use. 最后,该属性会影响数值的显示方式,形成在复杂脚本中编写文本的方式。Finally, the property affects the way numbers are displayed and the way texts written in complex scripts are shaped. 默认值为“en-US”。The default value is "en-US".

生成附属资源程序集Building a Satellite Resource Assembly

在 .csproj 中:In .csproj:

编辑 .csproj 文件,并将以下标记添加到无条件 <PropertyGroup>Edit the .csproj file and add the following tag to an unconditional <PropertyGroup>:

<UICulture>en-US</UICulture>

请注意,添加了 UICulture 值。Notice the addition of a UICulture value. 如果将此项设置为有效 CultureInfo 值(例如 en-us),则生成项目时将生成包含其中所有可本地化资源的附属程序集。When this is set to a valid CultureInfo value such as en-US, building the project will generate a satellite assembly with all localizable resources in it.

<Resource Include="RunIcon.JPG">

<Localizable>False</Localizable>

</Resource>

不需要对 RunIcon.JPG 进行本地化,因为它对于所有区域性都应该是相同的。The RunIcon.JPG does not need to be localized because it should appear the same for all cultures. Localizable 设置为 false,以使其保留在语言中性主程序集中,而不是附属程序集。Localizable is set to false so that it remains in the language neutral main assembly instead of the satellite assembly. 所有 noncompilable 资源的默认值均 Localizable 设置为 trueThe default value of all noncompilable resources is Localizable set to true.

本地化“运行”对话框Localizing the Run Dialog

分析Parse

生成应用程序之后,对其进行本地化的第一步是将可本地化的资源从附属程序集中分析出来。After building the application, the first step in localizing it is parsing the localizable resources out of the satellite assembly. 出于本主题的目的,请使用示例 LocBaml 工具,该工具可在LocBaml 工具示例中找到。For the purposes of this topic, use the sample LocBaml tool which can be found at LocBaml Tool Sample. 请注意,LocBaml 只是一个示例工具,用于帮助你了解有关如何生成适合你的本地化过程的本地化工具的入门知识。Note that LocBaml is only a sample tool meant to help you get started in building a localization tool that fits into your localization process. 使用 LocBaml,运行以下内容来分析: LocBaml/Parse rundialog.csproj/out: 生成 "rundialog.csproj" 文件的文件。Using LocBaml, run the following to parse: LocBaml /parse RunDialog.resources.dll /out: to generate a "RunDialog.resources.dll.CSV" file.

本地化Localize

使用你喜欢的支持 Unicode 的 CSV 编辑器来编辑此文件。Use your favorite CSV editor that supports Unicode to edit this file. 筛选掉本地化类别为“None”的所有项。Filter out all entries with a localization category of "None". 应看到下面的项:You should see the following entries:

资源键Resource Key 本地化类别Localization Category {2>值<2}Value
Button_1:System.Windows.Controls.Button.$ContentButton_1:System.Windows.Controls.Button.$Content ButtonButton 确定OK
Button_2:System.Windows.Controls.Button.$ContentButton_2:System.Windows.Controls.Button.$Content ButtonButton 取消Cancel
Button_3:System.Windows.Controls.Button.$ContentButton_3:System.Windows.Controls.Button.$Content ButtonButton 浏览...Browse...
ComboBox_1:System.Windows.Controls.ComboBox.$ContentComboBox_1:System.Windows.Controls.ComboBox.$Content 组合框ComboBox
TextBlock_1:System.Windows.Controls.TextBlock.$ContentTextBlock_1:System.Windows.Controls.TextBlock.$Content 文本Text Windows 将根据您所输入的名称,为您打开相应的程序、文件夹、文档或 Internet 资源。Type the name of a program, folder, document, or Internet resource, and Windows will open it for you.
TextBlock_2:System.Windows.Controls.TextBlock.$ContentTextBlock_2:System.Windows.Controls.TextBlock.$Content 文本Text 打开:Open:
Window_1:System.Windows.Window.TitleWindow_1:System.Windows.Window.Title 职务Title 运行Run

将该应用程序本地化为德语版本需要进行下面的翻译:Localizing the application to German would require the following translations:

资源键Resource Key 本地化类别Localization Category {2>值<2}Value
Button_1:System.Windows.Controls.Button.$ContentButton_1:System.Windows.Controls.Button.$Content ButtonButton 确定OK
Button_2:System.Windows.Controls.Button.$ContentButton_2:System.Windows.Controls.Button.$Content ButtonButton AbbrechenAbbrechen
Button_3:System.Windows.Controls.Button.$ContentButton_3:System.Windows.Controls.Button.$Content ButtonButton Durchsuchen…Durchsuchen…
ComboBox_1:System.Windows.Controls.ComboBox.$ContentComboBox_1:System.Windows.Controls.ComboBox.$Content 组合框ComboBox
TextBlock_1:System.Windows.Controls.TextBlock.$ContentTextBlock_1:System.Windows.Controls.TextBlock.$Content 文本Text Geben Sie den Namen eines Programms, Ordners, Dokuments oder einer Internetresource an.Geben Sie den Namen eines Programms, Ordners, Dokuments oder einer Internetresource an.
TextBlock_2:System.Windows.Controls.TextBlock.$ContentTextBlock_2:System.Windows.Controls.TextBlock.$Content 文本Text 打开:Öffnen:
Window_1:System.Windows.Window.TitleWindow_1:System.Windows.Window.Title 职务Title 运行Run

生成Generate

本地化的最后一步涉及创建新进行本地化的附属程序集。The last step of localization involves creating the newly localized satellite assembly. 可以使用下面的 LocBaml 命令完成此操作:This can be accomplished with the following LocBaml command:

LocBaml.exe /generate RunDialog.resources.dll /trans:RunDialog.resources.dll.CSV /out: . /cul:de-DELocBaml.exe /generate RunDialog.resources.dll /trans:RunDialog.resources.dll.CSV /out: . /cul:de-DE

在德语窗口上,如果将此资源 .dll 放置在主程序集旁边的取消拖放文件夹中,则此资源将自动加载,而不是 en-us 文件夹中的一个。On German Windows, if this resources.dll is placed in a de-DE folder next to the main assembly, this resource will automatically load instead of the one in the en-US folder. 如果你没有德语版本的 Windows 来测试这种情况,请将区域性设置为要使用的 Windows 的任何区域性(例如 en-US),并替换原始的资源 DLL。If you do not have a German version of Windows to test this, set the culture to whatever culture of Windows you are using (for example, en-US), and replace the original resources DLL.

附属资源加载Satellite Resource Loading

MyDialog.exeMyDialog.exe en-US\MyDialog.resources.dllen-US\MyDialog.resources.dll de-DE\MyDialog.resources.dllde-DE\MyDialog.resources.dll
代码Code 原始英语版 BAMLOriginal English BAML 本地化的 BAMLLocalized BAML
非特定区域性资源Culturally neutral resources 其他英语资源Other resources in English 已本地化为德语的其他资源Other resources localized to German

.NET framework 会根据应用程序的 Thread.CurrentThread.CurrentUICulture自动选择要加载的附属资源程序集。The .NET framework automatically chooses which satellite resources assembly to load based on the application’s Thread.CurrentThread.CurrentUICulture. 此值默认为 Windows OS 的区域性。This defaults to the culture of your Windows OS. 因此,如果您使用的是德语 Windows,则会加载 de-DE\MyDialog.resources.dll,如果您使用的是英语 Windows,则 en-US\MyDialog.resources.dll 会加载。So if you are using German Windows, the de-DE\MyDialog.resources.dll loads, if you are using English Windows, the en-US\MyDialog.resources.dll loads. 通过在项目的 AssemblyInfo.* 中指定 NeutralResourcesLanguage,可以设置应用程序的最终回退资源。You can set the ultimate fallback resource for your application by specifying the NeutralResourcesLanguage in your project’s AssemblyInfo.*. 例如,如果指定:For example if you specify:

[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]

如果 de-DE\MyDialog.resources.dll 和 de\MyDialog.resources.dll 都不可用,则德语版 Windows 将使用 en-US\MyDialog.resources.dll。then the en-US\MyDialog.resources.dll will be used with German Windows if a de-DE\MyDialog.resources.dll or de\MyDialog.resources.dll are both unavailable.

Microsoft 沙特阿拉伯主页Microsoft Saudi Arabia Homepage

下图显示了英语和阿拉伯语主页。The following graphics show an English and Arabic Homepage. 有关生成这些图形的完整示例,请参阅全球化主页示例For the complete sample that produces these graphics see Globalization Homepage Sample.

英语:English:

显示英语主页的屏幕截图。

阿拉伯语:Arabic:

显示阿拉伯语主页的屏幕截图。

设计全球 Microsoft 主页Designing a Global Microsoft home page

Microsoft 沙特阿拉伯网站的这个实体模型说明了针对从右向左布局语言提供的全球化功能。This mock up of the Microsoft Saudi Arabia web site illustrates the globalization features provided for RightToLeft languages. 希伯来语和阿拉伯语等语言具有从右到左的阅读顺序,因此,UIUI 布局的布局通常与从左到右的语言(如英语)的布局不同。Languages such as Hebrew and Arabic have a right-to-left reading order so the layout of UIUI must often be laid out quite differently than it would be in left-to-right languages such as English. 从从左到右布局语言本地化到从右到左布局语言可能相当困难,反之亦然。Localizing from a left-to-right language to a right-to-left language or vice versa can be quite challenging. WPFWPF 可使此类本地化工作变得更容易。has been designed to make such localizations much easier.

FlowDirectionFlowDirection

Homepage.xaml:Homepage.xaml:

<Page x:Uid="Page_1" x:Class="MicrosoftSaudiArabiaHomepage.Homepage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
    FlowDirection="LeftToRight" 
    Localization.Comments="FlowDirection(This FlowDirection controls the actual content of the homepage)"
    xml:lang="en-US">

请注意 Page上的 FlowDirection 属性。Notice the FlowDirection property on Page. 将此属性更改为 "RightToLeft 将更改 Page 及其子元素的 FlowDirection,以便将此 UIUI 的布局反向转换为与阿拉伯语用户所期望的从右到左。Changing this property to RightToLeft will change the FlowDirection of the Page and its children elements so that the layout of this UIUI is flipped to become right-to-left as an Arabic user would expect. 通过在任何元素上指定显式 FlowDirection,可以重写继承行为。One can override the inheritance behavior by specifying an explicit FlowDirection on any element. FlowDirection 属性可用于任何 FrameworkElement 或文档相关元素,并且具有隐式值 LeftToRightThe FlowDirection property is available on any FrameworkElement or document related element and has an implicit value of LeftToRight.

请注意,当根 FlowDirection 更改时,即使背景渐变画笔也能正确翻转:Observe that even the background gradient brushes are flipped correctly when the root FlowDirection is changed:

FlowDirection="LeftToRight"FlowDirection="LeftToRight"

显示从左到右的渐变流的屏幕截图。

FlowDirection="RightToLeft"FlowDirection="RightToLeft"

显示从右到左渐变流的屏幕截图。

避免对面板和控件使用固定维度Avoid Using Fixed Dimensions for Panels and Controls

浏览主页 .xaml,注意除了为顶部 DockPanel上的整个 UIUI 指定的固定宽度和高度以外,没有其他固定尺寸。Take a look through Homepage.xaml, notice that aside from the fixed width and height specified for the entire UIUI on the top DockPanel, there are no other fixed dimensions. 请勿使用固定尺寸,以防裁剪可能比源文本长的本地化文本。Avoid using fixed dimensions to prevent clipping localized text that may be longer than the source text. WPFWPF 面板和控件将根据其所包含的内容自动调整大小。panels and controls will automatically resize based on the content that they contain. 大多数控件也具有可以设置以进行更多控制的最小和最大尺寸(例如,MinWidth = "20")。Most controls also have minimum and maximum dimensions that you can set for more control (for example, MinWidth="20"). 使用 Grid,还可以通过使用 "*" (例如 Width="0.25*")或使用其单元大小共享功能设置相对宽度和高度。With Grid, you can also set relative widths and heights by using ‘*’ (for example, Width="0.25*") or use its cell size sharing feature.

本地化注释Localization Comments

在很多情况下,内容可能不太明确,难以翻译。There are many cases where content may be ambiguous and difficult to translate. 开发人员或设计人员可通过本地化注释为本地化人员提供额外的上下文和注释。The developer or designer has the ability to provide extra context and comments to localizers through localization comments. 例如,下面的 Localization.Comments 阐明了字符“|”的用法。For example the Localization.Comments below clarifies the usage of the character ‘|’.

<TextBlock 
  x:Uid="TextBlock_2" 
  DockPanel.Dock="Right" 
  Foreground="White" 
  Margin="5,0,5,0"
  Localization.Comments="$Content(This character is used as a decorative rule.)">
  |
</TextBlock>

此注释将与 TextBlock_1 的内容相关联,并在 LocBaml 工具(请参阅本地化应用程序)的情况下,可在输出 .csv 文件中 TextBlock_1 行的第6列中查看:This comment becomes associated with TextBlock_1’s content and in the case of the LocBaml Tool, ( see Localize an Application), it can be seen in the 6th column of the TextBlock_1 row in the output .csv file:

资源键Resource Key 类别Category 可读性Readable 可修改性Modifiable 备注Comment {2>值<2}Value
TextBlock_1:System.Windows.Controls.TextBlock.$ContentTextBlock_1:System.Windows.Controls.TextBlock.$Content 文本Text trueTRUE trueTRUE 此字符用作装饰性规则。This character is used as a decorative rule. ||

使用下面的语法可以将注释放置在任何元素的内容或属性上:Comments can be placed on the content or property of any element using the following syntax:

<TextBlock 
  x:Uid="TextBlock_1" 
  DockPanel.Dock="Right" 
  Foreground="White" 
  Margin="5,0,5,0"
  Localization.Comments="$Content(This is a comment on the TextBlock's content.)
     Margin(This is a comment on the TextBlock's Margin property.)">
  |
 </TextBlock>

本地化特性Localization Attributes

通常,开发人员或本地化经理需要控制本地化人员能够阅读和修改的内容。Often the developer or localization manager needs control of what localizers can read and modify. 例如,可能不希望本地化人员翻译你公司的名称或法律用语。For example, you might not want the localizer to translate the name of your company or legal wording. WPFWPF 提供了一些特性,使用这些特性可以设置元素的内容或属性(本地化工具可以使用这些内容或属性锁定、隐藏元素或对元素进行排序)的可读性、可修改性和类别。provides attributes that enable you to set the readability, modifiability, and category of an element’s content or property which your localization tool can use to lock, hide, or sort elements. 有关更多信息,请参见AttributesFor more information, see Attributes. 此示例中 LocBaml 工具仅输出这些特性的值。For the purposes of this sample, the LocBaml Tool just outputs the values of these attributes. WPFWPF 控件对这些特性都使用默认值,但可以替代这些默认值。controls all have default values for these attributes, but you the can override them. 例如,下面的示例将重写 TextBlock_1 的默认本地化属性,并将内容设置为可读但对于本地化人员是不可修改的。For example, the following example overrides the default localization attributes for TextBlock_1 and sets the content to be readable but unmodifiable for localizers.

<TextBlock
x:Uid="TextBlock_1"
Localization.Attributes=
"$Content(Readable Unmodifiable)">
  Microsoft Corporation
</TextBlock>

除了可读性和可修改性特性之外,WPFWPF 还提供了一个可用于向本地化人员提供更多上下文的常见 UI 类别(LocalizationCategory)的枚举。In addition to the readability and modifiability attributes, WPFWPF provides an enumeration of common UI categories (LocalizationCategory) that can be used to give localizers more context. 还可以在 XAMLXAML 中覆盖平台控件 WPFWPF 默认类别:The WPFWPF default categories for platform controls can be overridden in XAMLXAML as well:

<TextBlock x:Uid="TextBlock_2">
<TextBlock.ToolTip>
<TextBlock
x:Uid="TextBlock_3"
Localization.Attributes=
"$Content(ToolTip Readable Unmodifiable)">
Microsoft Corporation
</TextBlock>
</TextBlock.ToolTip>
Windows Vista
</TextBlock>

还可以通过代码覆盖 WPFWPF 提供的默认本地化特性,以便为自定义控件正确设置正确的默认值。The default localization attributes that WPFWPF provides can also be overridden through code, so you can correctly set the right default values for custom controls. 例如:For example:

[Localizability(Readability = Readability.Readable, Modifiability=Modifiability.Unmodifiable, LocalizationCategory.None)]
public class CorporateLogo : TextBlock
{
    // ...
}

XAMLXAML 中设置的每个实例属性将优先于在自定义控件的代码中设置的值。The per instance attributes set in XAMLXAML will take precedence over the values set in code on custom controls. 有关属性和注释的详细信息,请参阅本地化特性和注释For more information on attributes and comments, see Localization Attributes and Comments.

字体回退和复合字体Font Fallback and Composite Fonts

如果指定的字体不支持给定的点区范围,WPFWPF 将自动回退到使用 Windows\Fonts 目录中的全局用户界面的 compositefont。If you specify a font that does not support a given codepoint range, WPFWPF will automatically fallback to one that does by using the Global User Interface.compositefont that is located in your Windows\Fonts directory. 复合字体的工作方式与任何其他字体一样,可通过设置元素的 FontFamily (例如,FontFamily="Global User Interface")来显式使用。Composite fonts work just as any other font and can be used explicitly by setting an element’s FontFamily (for instance, FontFamily="Global User Interface"). 通过创建你自己的复合字体并指定针对特定代码点范围和语言所使用的字体,可以指定你自己的字体回退首选项。You can specify your own font fallback preference by creating your own composite font and specifying what font to use for specific codepoint ranges and languages.

有关复合字体的详细信息,请参阅 FontFamilyFor more information on composite fonts see FontFamily.

本地化 Microsoft 主页Localizing the Microsoft Homepage

可以按照“运行”对话框示例中的相同步骤对此应用程序进行本地化。You can follow the same steps as the Run Dialog example to localize this application. 全球化主页示例中提供了适用于阿拉伯语的本地化 .csv 文件。The localized .csv file for Arabic is available for you in the Globalization Homepage Sample.