Globalization for WPF
This topic introduces issues that you should be aware of when writing Windows Presentation Foundation (WPF) applications for the global market. The globalization programming elements are defined in .NET in the System.Globalization namespace.
XAML Globalization
Extensible Application Markup Language (XAML) is based on XML and takes advantage of the globalization support defined in the XML specification. The following sections describe some XAML features that you should be aware of.
Character References
A character reference gives the UTF16 code unit of the particular Unicode character it represents, in either decimal or hexadecimal. The following example shows a decimal character reference for the COPTIC CAPITAL LETTER HORI, or 'Ϩ':
Ϩ
The following example shows a hexadecimal character reference. Notice that it has an x in front of the hexadecimal number.
Ϩ
Encoding
The encoding supported by XAML are ASCII, Unicode UTF-16, and UTF-8. The encoding statement is at the beginning of 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 XAML file.
?xml encoding="UTF-8"?
Language Attribute
XAML uses xml:lang to represent the language attribute of an element. To take advantage of the CultureInfo class, the language attribute value needs to be one of the culture names predefined by 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 regional languages. For example, French has different spelling, vocabulary, and pronunciation in France, Quebec, Belgium, and Switzerland. Also Chinese, Japanese, and Korean share code points in Unicode, but the ideographic shapes are different and they use totally different fonts.
The following 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>
Unicode
XAML supports all Unicode features including surrogates. As long as the character set can be mapped to 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 WPF application can use StringInfo to manipulate strings without understanding whether they have surrogate pairs or combining characters.
Designing an International User Interface with XAML
This section describes user interface (UI) features that you should consider when writing an application.
International Text
WPF 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
All the writing system engines support OpenType fonts. OpenType fonts can include the OpenType layout tables that enable font creators to design better international and high-end typographic fonts. 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 fonts allow the handling of large glyph sets using Unicode encoding. Such encoding enables broad international support as well as for typographic glyph variants.
WPF 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
WPF provides a very convenient way to support horizontal, bidirectional, and vertical layouts. In presentation framework the FlowDirection 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 Microsoft support for switching UIs from one language to another. A WPF 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. WPF 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. The resource assembly that is loaded depends on the CurrentUICulture of the current thread.
Localizable User Interface
WPF applications use XAML to define their UI. XAML allows developers to specify a hierarchy of objects with a set of properties and logic. The primary use of XAML is to develop WPF applications but it can be used to specify a hierarchy of any common language runtime (CLR) objects. Most developers use XAML to specify their application's UI and use a programming language such as C# to react to user interaction.
From a resource point of view, a UI is a resource element and therefore its final distribution format must be localizable to support international languages. Because XAML cannot handle events many XAML applications contain blocks of code to do this. For more information, see XAML in WPF. Code is stripped out and compiled into different binaries when a 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
WPF applications support all the FrameworkCLR resources including string tables, images, and so forth.
Building Localizable Applications
Localization means to adapt a UI to different cultures. To make a WPF 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 API to load. One of the files required for a WPF 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 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");
}
Using ClickOnce with Localized Applications
ClickOnce is a new Windows Forms deployment technology that will ship with Visual Studio 2005. 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 Microsoft Windows not on English 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. To control this process use the NeutralResourcesLanguageAttribute. 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. 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
.NET Desktop feedback