Monday, June 2, 2008

WPF Globalization and Localization

If you're starting out with globalization and the Windows Presentation Foundation (WPF), then you're in for a real treat. Microsoft has made it easier than ever before to build globalized software. I suggest you start out by reading this overview on the MSDN. You should be able to figure out the rest with a little trial and error. However, if you're too lazy to read the whole article, I've transcribed some concepts below.

WPF offers several paths to resource localization. You can bind localizable resources in your application to an XML file, store text and images in more traditional resource files, or you can markup your localization preferences in XAML with your user interface. Here is an example of how you can apply a localization comment for the benefit of a translator and attribute content as readable but unmodifiable.

<TextBlock x:Uid="TextBlock_1"
Localization.Attributes="$Content(Readable Unmodifiable)"
Localization.Comments=
"$Content(This is a trademark and should not be translated.)
FontFamily(Font Unreadable Unmodifiable)">
Microsoft Corporation
</TextBlock>

Localization attributes are important because they give you greater control over your resources. In the example, "Microsoft Corporation" is marked as unmodifiable and therefore cannot be changed in a satellite assembly.

The example also illustrates the use the UID property. These identifiers are a necessary part of the localization API and help to track changes in localized resources. You can add these unique identifiers through msbuild by calling msbuild /t:updateuid Sample.csproj.

With the necessary localization markup in place, you need to perform a few more steps before you can produce satellite assemblies. The first is to add the following XML to your project file:

  <PropertyGroup>
<
UICulture>en-US</UICulture>
</
PropertyGroup>

and then add the following attribute to your assembly info file:

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

Substitute "en-US" in the above for whatever invariant culture you are using. Note that the fallback location is set to Satellite because the main assembly will not contain any invariant resources. This means that the assembly loader will produce a runtime error if you were to remove all the satellite assemblies.

Building the project will now create a satellite assembly for your invariant culture. Now you need a way to export the resources out of your satellite assembly so that they can be translated. You also need a means to generate new satellite assemblies for your target cultures.

As a step towards helping you on your way, Microsoft have create a sample tool called LocBaml that allows you to parse satellite assemblies to create localizable resources and generate a localized binary. The tool will export the resources out to a CSV file that you can give to a translator. Once the CSV file contains the necessary localizations, you simply pass it to the tool and identify the target locale so it can create the new satellite.


Technorati Tags: ,

No comments:

Post a Comment