没有合适的资源?快使用搜索试试~ 我知道了~
WPF Localization Guidance
4星 · 超过85%的资源 需积分: 7 18 下载量 14 浏览量
2009-07-27
14:02:06
上传
评论
收藏 2.19MB PDF 举报
温馨提示
试读
66页
WPF Localization Guidance WPF程序实现本地化的方法介绍
资源推荐
资源详情
资源评论
WPF Localization Guidance
Rick Strahl & Michele Leroux Bustamante, June 2009
wpflocalization.codeplex.com Page 1
Contents
Localization Guidance for WPF ..................................................................................................................... 2
Localization Considerations .......................................................................................................................... 2
Unicode Support ....................................................................................................................................... 3
Culture Mapping ....................................................................................................................................... 3
Localizing Resources ................................................................................................................................. 4
Localizing WPF = Choices .......................................................................................................................... 5
Resources and Culture Formatting ............................................................................................................... 6
Setting Culture and UICulture ................................................................................................................... 6
Resources and Resource Fallback ............................................................................................................. 7
Preferred Culture and ResourceFallback .................................................................................................. 9
Working with Resx Resources ................................................................................................................. 10
Accessing Resources with the ResourceManager .................................................................................. 14
Using XAML Resources and LocBaml to Localize Content .......................................................................... 16
What can you localize with LocBaml? ..................................................................................................... 19
Using Resource Dictionaries for Runtime Access to XAML Resources ................................................... 20
Localizing with LocBaml .............................................................................................................................. 23
Enabling Localization in your Visual Studio Project ................................................................................ 25
Run MsBuild to generate unique Uid’s for each UI element in your XAML ............................................ 29
Using LocBaml to Export Resources into a CSV file ................................................................................ 30
Localizing Resources in the CSV File ....................................................................................................... 32
Embedding Localized Resources back into Satellite Assemblies ............................................................ 33
BAML and Resx Resources Combined ..................................................................................................... 34
Using an MSBuild Task ............................................................................................................................ 36
LocBaml – Not for the faint of heart ....................................................................................................... 38
Localizing Resx Resources ........................................................................................................................... 39
Binding to Strongly-Typed Resx Resources ............................................................................................. 40
Organizing Static Resx Resources............................................................................................................ 42
Things to Consider with x:Static Resource Bindings ............................................................................... 44
WPF Localization Guidance
Rick Strahl & Michele Leroux Bustamante, June 2009
wpflocalization.codeplex.com Page 2
Custom Markup Extensions for Resx Resources ......................................................................................... 46
How the Custom Markup Extension Works ............................................................................................ 50
Attached Property Binding .......................................................................................................................... 55
Other Topics of Interest .............................................................................................................................. 60
Use Autosizing for Elements ................................................................................................................... 60
Right To Left Display ............................................................................................................................... 61
Switching Languages on the Fly .............................................................................................................. 62
Assigning a Resx Image Resource to an Image Control .......................................................................... 64
Summary ..................................................................................................................................................... 65
Acknowledgements ..................................................................................................................................... 66
Resources .................................................................................................................................................... 66
Localization Guidance for WPF
Application localization is not a trivial task for any type of application scenario. The process is based on a
few core principles that apply to WPF as they do to any other type of client application with a user
interface. It is important for developers to understand the basic concepts of regional data display,
locale-specific user interface customization and how to serve localized resources in both static and
dynamic fashion. These concepts are very similar for most client applications but the actual process of
localizing the static user interface components tends to vary between environments and WPF
introduces yet another approach to resource localization for XAML resources.
This whitepaper will start with a quick review of general localization considerations for completeness,
discuss how the .NET Framework handles resources for all applications, and then focus specifically on
localization scenarios for WPF explaining some of the trade-offs within each approach.
Localization Considerations
Localization is the process of preparing an application to run in multiple locations. The .NET Framework
provides very thorough support for localizing all types of client applications. That doesn’t mean it’s easy
– localization is a very tedious and complex task and this whitepaper makes no attempt at covering the
entire topic – it’d be enough to fill a book. In case you are new to localization concepts, this section and
the next will serve as a short introduction to key localization concepts for.NET Framework applications.
WPF Localization Guidance
Rick Strahl & Michele Leroux Bustamante, June 2009
wpflocalization.codeplex.com Page 3
Unicode Support
Unicode support is crucial for representing the wide variety of characters of the world’s many languages
seamlessly. This may seem obvious now, but it wasn’t long before the .NET Framework was released
that ANSI character sets and codepage translations were the norm. Unicode transcends the issues of
these antiquated character set formats and their incompatibilities and provides an easy way to
represent characters from multiple languages easily in the same string without having to worry about
encoding issues.
The .NET Framework supports Unicode end-to-end – from the user interface all the way down to the
database layer. This means that today, applications needn’t do anything special to receive input, process
or save data from different languages. In WPF most of the user interface content is encoded in static
XAML documents which are just XML documents encoded in UTF-8 or UTF-16. Resx resources, the core
resource format for .NET Framework applications, also are encoded in XML. These XML formats are
Unicode compliant so the full range of characters supported by the world’s languages can be directly
expressed in XAML markup as well as in Resx resources that hold localized data.
As a point of interest, UTF-8 and UTF-16 are both capable of representing single-byte and
double-byte character sets. UTF-8 does so in a more efficient way from a storage perspective,
but is interpreted less efficiently for double-byte character sets. UTF-16 stores all characters
using two bytes thus is a less efficient storage format but can yield more efficient interpretation
of double-byte character sets. So, although UTF-8 will suffice for most scenarios, the latter is
usually used specifically for double-byte scenarios.
Culture Mapping
Locations are defined – at least in the context of .NET localization lingo – as a language and
country/region pair. For example en-US is for English in the United States or fr-CA for French in Canada.
The country/region can also be omitted so en and fr are valid, non-specific culture identifiers which
represent English and French without the regional code. Non-specific cultures are useful for storing
resources that work for all regions, but are problematic for formatting and parsing since different
regions often have different formatting rules for things like currency or date and time. For this reason
the .NET Framework separates the concept of UICulture (for resources presented to the user which
includes text, graphics and any other display elements) and Culture (for formatting currency, numbers,
date and time, lists, sorting and so forth). It is quite possible to use a different culture setting for
UICulture and Culture – the former used to determine which resources will be used to populate a
localized user interface.
Culture information and manipulation in the .NET Framework is exposed through an instance of the
CultureInfo class, which can be used to read the culture settings for a given locale, as well as setting a
specific locale in an application. Every thread maintains a set of culture information stored as CultureInfo
settings in CurrentCulture and CurrentUICulture properties. CurrentCulture determines behavior for
WPF Localization Guidance
Rick Strahl & Michele Leroux Bustamante, June 2009
wpflocalization.codeplex.com Page 4
formatting and parsing (as previously discussed, for things like numbers and dates) and CurrentUICulture
indicates the appropriate resources to load for the thread’s locale. From any thread you can access
CurrentCulture and CurrentUICulture from the following static members:
Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.CurrentUICulture or
CultureInfo.CurrentCulture, and CultureInfo.CurrentUICulture.
The main WPF thread will always be assigned a default setting for CurrentCulture and
CurrentUICulture. You can also explicitly set these values based on user preferences (to be
discussed). An important point to note, however, is that if a WPF application spins up new
threads, those threads will not inherit the main UI thread’s culture settings, in fact they will not
be set at all! So, be sure to explicitly assign new threads with the appropriate values using the
techniques discussed in this article.
Localizing Resources
The most prominent and tedious process of localization and the focus of this whitepaper is resource
localization which refers to the process of translating the static pieces – primarily text – of an
application. The idea is that the static pieces of an application – strings in labels, tooltips, menu items,
headers, static messages and any other user interface content – are stored in such a way that they can
be translated separately from the immediate user interface. The generally accepted approach for this is
to provide a mechanism to extract relevant component properties as resources so that they can be
stored and localized separate from the user interface with a copy for each supported culture. Resources
are then applied to their respective component properties at runtime based on the current UI culture.
An important benefit to this approach is that a single code-based is used for the application, and that
future localizations to support new cultures should not impact the compiled code.
In WPF development there are two main approaches to resource localization: XAML-based and Resx-
based.
XAML-based localization involves grouping localized content per XAML document rather than
specifically mapping individual resources to specific XAML element properties. The XAML documents in
the application serve as the base resource storage mechanism for all static content that is created in
XAML. The idea is that for the neutral language you just create your XAML without any special markup
or bindings for localization. A tool called LocBaml can then be used to export all the localizable, static
content from compiled applications in a CSV text format. The exported resources can then be localized
for each specific culture. Finally LocBaml can be used to merge and compile the localized resources into
culture specific satellite assemblies (or binary resources). This approach is purely static resulting in
compiled XAML (BAML) resources for each supported culture, where each culture-specific BAML
resource contains the entire set of localized resource values. The approach is efficient but completely
static and there is no way to interact with the way that resources are individually loaded. LocBaml is a
WPF Localization Guidance
Rick Strahl & Michele Leroux Bustamante, June 2009
wpflocalization.codeplex.com Page 5
command line tool provided by Microsoft as a utility sample application and the process of using it is
rather rigid and potentially error prone.
Resx-based resources are the traditional approach for .NET Framework application localization. They are
XML-based files that compile into binary resources and can be accessed by the application through
individual resource keys. Resx support is deeply integrated in the .NET Framework and Visual Studio,
although there’s no specific WPF experience in Visual Studio or in other XAML designers such as Blend.
Binding Resx resources to XAML elements involves using bindings and manually mapping resources to
individual properties with WPF binding syntax. The Resx designer and strongly-typed resources are still
available in WPF projects so the resource editing process is straight forward. There’s no official story for
Resx resources in XAML so it’s no surprise there are a lot of different approaches available for using Resx
resources with XAML in the developer community. Two common approaches are using x:Static bindings
to strongly-typed resources or using custom markup extensions. Resx resources are more granular than
BAML resources as they are individually mapped to properties, but it also takes a little more effort to
map resources to element properties at design time.
Localizing WPF = Choices
As you can see there are a couple of different approaches available for resource localization with WPF.
The one you choose depends on the way you like to work and whether your localization process
happens once the application is complete or incrementally while you are building the application.
The LocBaml approach is a very static process and best done when the application does not change
frequently. Its big benefit is that you don’t have to do much during development to get your application
ready for localization as you can simply create XAML content in your default language and defer
localization until later. The process of using LocBaml is rigid and fairly complicated using a command line
tool that exports CSV files for the actual task of localization. Each culture has to be individually exported,
localized and explicitly re-generated. In addition LocBaml has to be integrated into the build process
explicitly to build localized output. It’s not for the faint of heart.
Many Resx based solutions have sprung up in the developer community to simplify things. Resx is well
supported in the Visual Studio environment so it’s easy to enter and edit Resx resources. Resx
localization is an open and extensible architecture that is more applicable in incremental localization
processes and provides more flexibility in binding resource values. But it does require more forethought
and interaction while the application is built as you have to map resource keys and bindings at
development time explicitly in your XAML markup.
As you can probably tell by these descriptions localization support in WPF and Visual Studio lacks the
sophistication that you might be used to in Windows Forms. With Windows Forms there was a clear
path to localization with the Windows Forms Designer integrated solution. In WPF there’s no such clear
path – the choice is left up to you and either way requires building or using custom built components or
command line tools. In this whitepaper the goal is to demonstrate the various approaches available and
剩余65页未读,继续阅读
资源评论
- 佣工70012015-06-16可用,还是比较麻烦!
wmjcom
- 粉丝: 68
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功