Saturday, 7 June 2014

Localization and Satellite Assemblies in WPF: How to support Localization in your WPF application?

Localization and Satellite Assemblies in WPF: How to support Localization in your WPF application?

In WPF applications, the unit of localization is the XAML file (technically, the compiled BAML resource that is embedded in your application). If you want to support three languages, you need to include three BAML resources. Satellite assemblies play an important role in localization of WPF applications. When you create a localized WPF application, you place each localized BAML resource in a separate satellite assembly.

To add localization support to your WPF application, just go to your .csproj file and add <UICulture>en-US</UICulture> under <PropertyGroup> element. Now Build your project and you will get a folder named en-US created where your exe is placed. 

If you go inside this folder, you will get an assembly with the same name as that of your exe but with different extensions. For example, if your application exe name is MySampleApplication.exe, its name would be MySampleApplication.resources.dll. This is called satellite assembly.

Now if you closely examine the size of your exe, it would be reduced because the BAML file is not embedded to it, but it is now contained in the satellite assembly which you just created.

When you run this application, the CLR automatically looks for satellite assemblies in the correct directory based on the computer's regional settings and loads the correct loacalized resource.

For example, if you are running in the fr-FR culture, the CLR will look for an fr-FR subdirectory and use the satellite assemblies it finds there.

Note: Extension for satellite assembly is .resources.dll.

How to localize your WPF application?

Step 1 (msbuild and Uid)

Add UID attribute to all the elements in your WPF application, you want to localize. You can use MSBUILD for adding the UID attribute to all elements used in your all the XAML files througout the application.

msbuild /t:updateuid MySampleApplication.csproj

Step 2 (LocBaml)

To extract the localizable content of all your elements, you need to use the LocBaml command-line tool. To extract the localizable details, you point LocBaml to your satellite assembly and use the /parse paramenter as shown here:

lcobaml /parse en-US\MySampleApplication.resources.dll

A .csv file will be generated named MySampleApplication.resources.csv

Step 3 (Build Satellite Assembly for other cultures e.g. fr-FR)

Again use locbaml tool with /generate parameter to create a satellite assembly with culture fr-FR.

locbaml /generate en-US\MySampleApplication.resources.dll
               /trans:MySampleApplication.resources.French.csv
       /cul:fr-FR /out:fr-FR

1 comment:

  1. With what can be provided to those who need and I believe that your website is good.

    ReplyDelete