| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | |||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Globalization for WPF |
03/30/2017 |
.net-framework |
|
article |
|
4571ccfe-8a60-4f06-9b37-7ac0b1c2d10f |
35 |
dotnet-bot |
dotnetcontent |
wpickett |
Globalization for WPF
This topic introduces issues that you should be aware of when writing [!INCLUDETLA#tla_winclient] applications for the global market. The globalization programming elements are defined in [!INCLUDETLA#tla_net] in System.Globalization.
XAML Globalization
[!INCLUDETLA#tla_xaml#initcap] is based on [!INCLUDETLA#tla_xml] and takes advantage of the globalization support defined in the [!INCLUDETLA2#tla_xml] specification. The following sections describe some [!INCLUDETLA2#tla_xaml] features that you should be aware of.
Character References
A character reference gives the number of the particular [!INCLUDETLA#tla_unicode] character it represents, in either decimal or hexadecimal. The following example shows a decimal character reference.
Ϩ
This example shows a hexadecimal character reference. Notice that it has an x in front of the hexadecimal number.
Ϩ
Encoding
The encoding supported by [!INCLUDETLA2#tla_xaml] are [!INCLUDETLA#tla_ascii], [!INCLUDETLA2#tla_unicode] UTF-16, and UTF-8. The encoding statement is at the beginning of [!INCLUDETLA2#tla_xaml] document. If no encoding attribute exists and there is no byte-order, the parser defaults to UTF-8. UTF-8 and UTF-16 are the preferred encodings. UTF-7 is not supported. The following example demonstrates how to specify a UTF-8 encoding in a [!INCLUDETLA2#tla_xaml] file.
?xml encoding="UTF-8"?
Language Attribute
[!INCLUDETLA2#tla_xaml] uses xml:lang to represent the language attribute of an element. To take advantage of the xref:System.Globalization.CultureInfo class, the language attribute value needs to be one of the culture names predefined by xref:System.Globalization.CultureInfo. 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. Also Chinese, Japanese, and Korean share code points in [!INCLUDETLA2#tla_unicode], but the ideographic shapes are different and they use totally different fonts.
The following [!INCLUDETLA#tla_xaml] example uses the fr-CA language attribute to specify Canadian French.
<TextBlock xml:lang="fr-CA">Découvrir la France</TextBlock> Unicode
[!INCLUDETLA2#tla_xaml] supports all [!INCLUDETLA2#tla_unicode] features including surrogates. As long as the character set can be mapped to [!INCLUDETLA2#tla_unicode], it is supported. 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. A [!INCLUDETLA2#tla_winclient] application can use xref:System.Globalization.StringInfo to manipulate strings without understanding whether they have surrogate pairs or combining characters.
Designing an International User Interface with XAML
This section describes [!INCLUDETLA#tla_ui] features that you should consider when writing an application.
International Text
[!INCLUDETLA2#tla_winclient] includes built-in processing for all [!INCLUDETLA#tla_winfx] 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
All the writing system engines support [!INCLUDETLA#tla_opentype] fonts. [!INCLUDETLA2#tla_opentype] fonts can include the [!INCLUDETLA2#tla_opentype] layout tables that enable font creators to design better international and high-end typographic fonts. The [!INCLUDETLA2#tla_opentype] font layout tables contain information about glyph substitutions, glyph positioning, justification, and baseline positioning, enabling text-processing applications to improve text layout.
[!INCLUDETLA2#tla_opentype] fonts allow the handling of large glyph sets using [!INCLUDETLA2#tla_unicode] encoding. Such encoding enables broad international support as well as for typographic glyph variants.
[!INCLUDETLA2#tla_winclient] text rendering is powered by [!INCLUDETLA#tla_ct] 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
[!INCLUDETLA2#tla_winclient] provides a very convenient way to support horizontal, bidirectional, and vertical layouts. In presentation framework the xref:System.Windows.FrameworkElement.FlowDirection%2A property can be used to define layout. The flow direction patterns are:
-
LeftToRight - horizontal layout for Latin, East Asian and so forth.
-
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
Multilingual User Interfaces (MUI) is a [!INCLUDETLA#tla_ms] support for switching [!INCLUDETLA2#tla_ui#plural] from one language to another. A [!INCLUDETLA2#tla_winclient] application uses the assembly model to support MUI. One application contains language-neutral assemblies as well as language-dependent satellite resource assemblies. The entry point is a managed .EXE in the main assembly. [!INCLUDETLA2#tla_winclient] resource loader takes advantage of the [!INCLUDETLA2#tla_netframewk]'s resource manager to support resource lookup and fallback. Multiple language satellite assemblies work with the same main assembly. The resource assembly that is loaded depends on the xref:System.Globalization.CultureInfo.CurrentUICulture%2A of the current thread.
Localizable User Interface
[!INCLUDETLA2#tla_winclient] applications use [!INCLUDETLA2#tla_xaml] to define their [!INCLUDETLA2#tla_ui]. [!INCLUDETLA2#tla_xaml] allows developers to specify a hierarchy of objects with a set of properties and logic. The primary use of [!INCLUDETLA2#tla_xaml] is to develop [!INCLUDETLA2#tla_winclient] applications but it can be used to specify a hierarchy of any [!INCLUDETLA#tla_clr] objects. Most developers use [!INCLUDETLA2#tla_xaml] to specify their application's [!INCLUDETLA2#tla_ui] and use a programming language such as [!INCLUDETLA#tla_cshrp] to react to user interaction.
From a resource point of view, a [!INCLUDETLA2#tla_xaml] file designed to describe a language-dependent [!INCLUDETLA2#tla_ui] is a resource element and therefore its final distribution format must be localizable to support international languages. Because [!INCLUDETLA2#tla_xaml] cannot handle events many [!INCLUDETLA2#tla_xaml] applications contain blocks of code to do this. For more information, see XAML Overview (WPF). Code is stripped out and compiled into different binaries when a [!INCLUDETLA2#tla_xaml] file is tokenized into the BAML form of 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.
[!NOTE] [!INCLUDETLA2#tla_winclient] applications support all the [!INCLUDETLA2#tla_netframewk][!INCLUDETLA2#tla_clr] resources including string tables, images, and so forth.
Building Localizable Applications
Localization means to adapt a [!INCLUDETLA2#tla_ui] to different cultures. To make a [!INCLUDETLA2#tla_winclient] application localizable, developers need to build all the localizable resources into a resource assembly. The resource assembly is localized into different languages, and the code-behind uses resource management [!INCLUDETLA#tla_api] to load. One of the files required for a [!INCLUDETLA2#tla_winclient] application is a project file (.proj). All resources that you use in your application should be included in the project file. The following example from a .csproj file shows how to do this.
<Resource Include="data\picture1.jpg"/>
<EmbeddedResource Include="data\stringtable.en-US.restext"/> To use a resource in your application instantiate a xref:System.Resources.ResourceManager and load the resource you want to use. The following example demonstrates how to do this.
[!code-csharpLocalizationResources#2]
Using ClickOnce with Localized Applications
ClickOnce is a new Windows Forms deployment technology that will ship with [!INCLUDETLA#tla_visualstu2005]. It enables application installation and upgrading of Web applications. When an application that was deployed with ClickOnce is localized it can only be viewed on the localized culture. For example, if a deployed application is localized to Japanese it can only be viewed on Japanese [!INCLUDETLA#tla_win] not on English [!INCLUDETLA2#tla_win]. This presents a problem because it is a common scenario for Japanese users to run an English version of [!INCLUDETLA2#tla_win].
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. To control this process use the xref:System.Resources.NeutralResourcesLanguageAttribute. The constructor of the xref:System.Resources.NeutralResourcesLanguageAttribute class has two signatures, one that takes an xref:System.Resources.UltimateResourceFallbackLocation parameter to specify the location where the xref:System.Resources.ResourceManager should extract the fallback resources: main assembly or satellite assembly. The following example shows how to use the attribute. For the ultimate fallback location, the code causes the xref:System.Resources.ResourceManager to look for the resources in the "de" subdirectory of the directory of the currently executing assembly.
[assembly: NeutralResourcesLanguageAttribute(
"de" , UltimateResourceFallbackLocation.Satellite)]