.NET XAML Localization

Localtransformer supports localization of .NET 3.5 XAML Files.
In order to localize .NET 3.5 applications it is necessary to take the following steps :

Step 1 - Internationalize Application

As was mentioned earlier, the first step of localizing applications is to internationalize them i.e. make them ready for localization. For .NET 3.5 application , this means that all strings and messages that are to be localized , and are not part of the user interface , should be put in a separate ResourceDictionary file. In order to do this , right click on the project and add a new file of type ResourceDictionary. Modify the file, so that it looks as the one below and save it as StringResources.xaml.

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:String x:Key="buttonText">Hello World!</system:String>
<system:String x:Key="Item was purchased successfully !">Item was purchased successfully !</system:String>
</ResourceDictionary>

Now for each string or message to be localized , add a system:String node to the ResourceDictionary .The x:Key attribute and the contents of the node should be set to the value of the message or string. For example for the message "Item was purchased successfully !" add a node <system:String x:Key="Item was purchased successfully !">Item was purchased successfully !</system:String> .

Step 2 - Merge the ResourceDictionary into the application

Afterwards, it is necessary to make public the ResourceDictionary for other XAML code. Therefore , modify the App.xaml file and add the following lines as contents of the <Application.Resources> node.

<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="StringResources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>






For example the App.xaml file can have the following contents:

<Application x:Class="DotNetXamlDemo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="StringResources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

Step 3 - Add a <UICulture> Tag Pair to the Project Configuration

In the next step it is necessary to specify the default culture in the project file of the application. Open the .csproj file of the application and , if the default language is en-US, add the following line <UICulture>en-US<UICulture>. This line should be inserted after the <Platform> tag under the <PropertyGroup> tag. Then the project file will look like this :

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<UICulture>en-US</UICulture>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
...

Step 4 - Set the NeutralResourcesLanguage attribute in the Project

In Visual Studio open the properties folder of the project and locate the AssemblyInfo.cs file. Afterwards, uncomment the following line :

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

Substitute the "en-US" value with the value of the default neutral language of the resources.

Step 5 - Add Uid's to the Xaml files

Additionally, it is necessary to add Uid attributes to all fields to be localized. In order to do this , open a Visual Studio command prompt or use View->Shell window and navigate to the project folder using the 'cd' command. Then run the following command :
msbuild /t:updateuid [.NET Project File] for example msbuild /t:updateuid DotNetXamlDemo.csproj.

Step 6 - Add Language Switching Code

Moreover, in order to change languages at run time a function has to be added that will look like this :





private void ChangeLanguage(string culture)
{

System.Threading.Thread.CurrentThread.CurrentUICulture =
new System.Globalization.CultureInfo(culture);
System.Threading.Thread.CurrentThread.CurrentCulture =
new System.Globalization.CultureInfo(culture);

// Reload all the merged dictionaries to reset the resources.
List dictionaryList = new List();
foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
{
dictionaryList.Add(dictionary.Source);
}
Application.Current.Resources.MergedDictionaries.Clear();
foreach (Uri uri in dictionaryList)
{
ResourceDictionary resourceDictionary1 = new ResourceDictionary();
resourceDictionary1.Source = uri;
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary1);
}

Window1 w = new Window1();
w.Show();
this.Close();
}

As you can see above , at first we have to change the CurrentUICulture and CurrentCulture of the current thread and afterwards we have to reload the resource dictionaries. Afterwards , we show a new window with the new language and close the old one.

Step 7 - Add code for getting localized messages and strings

In order to get localized messages and strings, that are not part of the user interface , it is necessary to add the following code. At first , create a new class , called for example TranslateHelper and add a function called TranslateMessage that will return the localized message. This class will have to look similar to this :

class TranslateHelper
{
public static string TranslateMessage(string msg)
{
string localizedMessage = (string)Application.Current.FindResource(msg);
return localizedMessage;
}
}

Then use this function to get the localized messages. For example to display a localized message box of "Item was purchased successfully !" write the following line of code:

MessageBox.Show(TranslateHelper.TranslateMessage("Item was purchased successfully !"));

Step 8 - Recompile

Step 9 - Build LocBaml Tool

If it is the first time you use the LocBaml tool , you will have to download it from here http://msdn.microsoft.com/en-us/library/ms771568.aspx. Afterwards compile the tool with the following command : msbuild locbaml.csproj

Step 10 - Extract the Resources to a .CSV File Using Locbaml.exe

Use the LocBaml.exe tool in order to extract the resources to a .csv file. Copy LocBaml.exe to the \bin\Debug\en-US directory or the corresponding directory of the native language. Copy the executable .exe file of the project to the same folder. Open a command prompt , and navigate to the same directory. Run then the following command to generate the .csv file : locbaml /parse [Project Name].resources.dll /out:trans.csv. For example , if the project is called DotNetXamlDemo run the following command : locbaml /parse DotNetXamlDemo.resources.dll /out:trans.csv.

Step 11 - Run Localtransformer and scan the .CSV file

After doing all this we have to run Localtransformer and create a new project where we will add the .csv file with the extracted resources as a target. Specify, that this file is a .NET 3.5 Baml Output file. Localtransformer will scan the file and extract all data to be localized.

Step 12 - Export or Send Project to Translator

The next step is to export the data to be localized (for example as a csv text file) or send the project to the translator

Step 13 - The translator translates the data to be localized

Then , the translator can edit the export file (for example it is possible to edit the exported csv text file in Microsoft Excel) or open the Localtransformer project and translate it within Localtransformer.

Step 14 - The translator sends the translated export file or project back

After having translated the data to be localized the translator should send it back.

Step 15 - Import the localized files from the translator

Then, it is necessary to import the localized file that the translator sent.

Step 16 - Build the localized .csv file with Localtransformer

Afterwards, build the localized .csv file with the resources.

Step 17 - Generate the localized satellite Dll

At last , it is necessary to generate the localized satellite Dll file from the localized .csv resources file. Open a command prompt and navigate to the bin\Debug\en-US directory of the project (or the corresponding directory of the native language). Create a directory for the localized language , for example de-DE , by using the command md ..\de-DE . Use the LocBaml tool to generate the localized satellite dll (in this case for the language de-DE). In order to do this run a command like this :
locbaml /generate /trans:[Localized Resources .csv File] /out:..\[Local Culture] /cul:[Local Culture] [Project Name].resources.dll
Run the following command for our example :
locbaml /generate /trans:trans.csv /out:..\de-DE /cul:de-DE DotNetXamlDemo.resources.dll

Now that localization process is complete !