Android Addicted Apps Creator

June 5, 2009

WPF Localization – Do you speak English ? Sprechen Sie Deutsch? 你讲中文?

Filed under: WPF — alinberce @ 19:59
Tags: , , ,

    By using localization, you allow controls in a application to change their content (particularly text labels and images) according to user preferences or according to current culture settings of the Windows operating system. In WPF unit of localization is in the XAML file (the compiled BAML resource). This means that for every language supported by the application you will have a BAML resource.

Let’s begin by creating a new WPF Application named WpfLocalization. In it we will add two buttons and a text block each with it’s English text. The Windows1.xaml file will look like this:

<Window x:Class="WpfLocalization.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <Button  Margin="30" Name="button1">Save</Button>
        <Button  Margin="30" Name="button2">Cancel</Button>
        <TextBlock Margin="30">Item saved succesfully</TextBlock>
    </StackPanel>
</Window>

As you can notice, I’ve already started to create the English version of the application.

First thing we need to do is add localization support for our project. To do this, just open the WpfLocalization.csproj file and add the following line

<UICulture>en-US</UICulture>

in the <PropertyGroup> section of the file. Reload the project and let’s continue. We need also to set the NeutralResourceLanguage assembly attribute. The simplest way to do this is open the AssemblyInfo.cs file in the Properties Folder and uncomment the following line:

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

By setting this we tell the CRL loader how to find the correct resource DLL for the selected culture. If it can’t find the indicated culture he will use the neutral one.Run the application and go to the Debug folder. In there we have the exe file and a folder en-US which contains the US culture resource DLL.

All the controls that need to be localized must have the Uid attribute. This means that we must modify in our .xaml files, all the controls. Pretty awful stuff to do manually. But we are lucky, Microsoft thought of this and helps us by offering the msbuild tool that will do the job automatically for us.

Start a command prompt and go to the following path: C:\Windows\Microsoft.net\Framwork\v3.5 in here is the msbuild.exe which we will use.

Run the following command:

msbuild /t:updateuit C:\WpfLocalization\WpfLocalization.csproj

The result should look like this:

image

And if you reload the project, in window1.xaml the stack panel is updated and it should have the Uid attribute for it’s elements.

<StackPanel x:Uid="StackPanel_1">
        <Button x:Uid="button1" Margin="30" Name="button1">Save</Button>
        <Button x:Uid="button2" Margin="30" Name="button2">Cancel</Button>
        <TextBlock x:Uid="TextBlock_1" Margin="30"> The item was saved succesfully</TextBlock>
    </StackPanel>

If you think that this was hard to do, wait to see what’s coming. Now we must extract the resources as a .csv file and translate the text as wanted. To do this we will use the Locbaml.exe tool. Copy the LocBaml.exe to bin\Debug\en-US folder of the project. Also copy the executable file WpfLocalized.exe to the same folder. Open a command prompt and go to the en-US folder and run the following command.

Locbaml /parse WPFLocalization.resources.dll /out:translate.csv

The result of this command will be the creation of a translate.csv file. Open this file and replace the English words with the German translation:

  • Save -> Sichern
  • Cancel -> Abbrechen
  • The item was saved succesfully! -> Posten erfolgreich gespeichert

After the translation is done, we need to generate the localized satellite DLL file and install it. Create a sister directory to en-US and name it de-DE. To do this we need to use the Locbaml file again. Open a command window and navigate to the folder where translate.csv is. Run the command:

Locbaml /generate /trans:translate.csv /out:..\de-DE /cul:de-DE WpfLocalization.resources.dll

We managed to complete the localization of the code. The result isn’t visible yet. Because we don’t want to change the Windows region settings to see the localized version, we will programmatically make the switch in the app.xaml.cs file.

public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            //string sCulture = "de-DE";
            string sCulture = "en-US";
            System.Threading.Thread.CurrentThread.CurrentUICulture =
                new System.Globalization.CultureInfo(sCulture);
            System.Threading.Thread.CurrentThread.CurrentCulture =
                new System.Globalization.CultureInfo(sCulture);
            base.OnStartup(e);
        }    
    }

Final result is encouraging :)  Happy coding.

image image

The video lesson: (sorry about the video, it’s quality is absolutely horrible, I uploaded at high quality but it looks really rubish)

 

Advertisements

3 Comments »

  1. Is possible to switch the UI language at runtime durring the application execution and without restarting it?

    Comment by Claudiu — June 7, 2009 @ 08:00 | Reply

  2. I did some testing… this means that I added two buttons, one for english one for german. I added the code from app onStartUp. It didn’t do anything. Probably the idea is to use a dictionary and map the controls to this dictionary. On change of culture, reload the dictionary and this would do a refresh for the controls. I tried and it closed the windows and it opened it again with the selected language.

    Comment by alinberce — June 9, 2009 @ 16:55 | Reply

  3. Hi i managed to successfully create dlls. But the language is not trnslating to the correct language.

    Comment by Ashen — January 24, 2011 @ 11:37 | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: