Introducing xml:lang in XAML

Introducing xml:lang in XAML

In a previous blog post I wrote about locales and cultures and described how you can use them to control the behavior and output of your programs to better match the regional expectations of your global users. Some of the behaviors controlled by locales and cultures include number and date formatting, currency symbols, and calendar types. In this blog post I introduce a similar, but not directly analogous, attribute defined by XML. This attribute called xml:lang also provides some control over the language and cultural behavior of associated XML content. The specific XML implementation of xml:lang discussed here is that of XAML which is used in WPF and Silverlight. See the simple example below.

<TextBlock xml:lang="es-ES">Buenos D í as</TextBlock>

In this example, the text is identified as Spanish (Spain) text and will be handled accordingly. In XAML the xml:lang attribute is represented by the Language property on classes derived from FrameworkElement or FrameworkContentElement. The value of xml:lang influences hyphenation, spell checking, number substitution, complex script shaping, and even font fallback. Therefore it is very important to be mindful of how Language (xml:lang) is used in your application in order to ensure proper handling of your content. The default value for xml:lang is of particular interest here because it sometimes causes confusion for WPF and Silverlight developers.

IMPORTANT: The default value for xml:lang in WPF and Silverlight is en-US.

Many people assume that xml:lang will automatically default to either CurrentCulture or CurrentUICulture, but this is not the case. Some developers may disagree with this design choice, but there are situations where content should not be displayed according to these culture values. In any case, my goal for this blog is simply to alert you to this behavior in order to help you properly manage xml:lang in your applications.

Common Issues:

· If you do not override the default (en-US) value of xml:lang in your application, you may run into problems properly displaying East Asian text. This is due to the fact that the font used to display some Unicode characters may not be appropriate for all East Asian scripts since some characters may be rendered differently depending on the language. To avoid this problem, be sure to set your xml:lang value appropriately (example: zh-CN).

· Controls used in WPF and Silverlight applications may respond differently, or not at all, to the value of xml:lang. Be sure to test your application to ensure that you are getting the expected behavior from controls that may need to display content differently based on culture or language.

· In data binding scenarios you may need to investigate overriding the Binding.ConverterCulture property in order to ensure that the correct culture value is used for your content. One option is to update your ConverterCulture property based on the CurrentCulture. Please see the links below for more information on overriding Binding.ConverterCulture.

I hope that this post has made you aware of the importance of the xml:lang attribute as well as some potential issues to avoid in your applications. If you need to dig deeper into this subject, you can find more information on MSDN and also on other blogs and websites. I encourage you to do more research if you have specific questions. Thanks for reading this post and for monitoring our Global Developer blog!

More Related Information on xml:lang:

· Xml:lang Handling in XAML

· Localizing Silverlight-based Applications

· WPF Globalization and Localization Overview

· Binding.ConverterCulture Property (Silverlight)

· Binding.ConverterCultureProperty (WPF)