WPF and Windows Form Integration Application Localization

Published on Wednesday, July 30, 2014

We have a Windows From application to load WPF modules, due to Microsoft programming guide, they called this kind of application as WPF and Windows Form integration application.

What ever, we now need the integration application to support multiple cultures, is localization.

Let’s start from a simple application, a Windows Form application load a WPF control.

1: We are using Visual Studio 2013, first we build a Windows Form application, and add a Windows Form project called WinformHoster;

2: add a WPF User Control Library application, named WpfControlLibrary1;

In UserControl1.xaml file, add a button, and use the following code:

<Grid>
    <Grid.RowDefinitions >
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Label Content="This is WPF control" HorizontalAlignment="Center"  Margin="5″ VerticalAlignment="Bottom" />
    <Button Grid.Row="1" HorizontalAlignment="Center"   Margin="5″ VerticalAlignment="Top"  Width="98″ Height="36″ Content="Hello"/>
</Grid>

The screen looks like the following:

image

3: Go back to Windows Form project WinformHoster, in Form1.cs file, add a label and set content to “Hello in Windows Form”;

Add add an ElementHost control, to load WPF control later;

Add reference to WpfControlLibrary1 in WinformHoster project;

4: Add Form1's load event as following:

private void Form1_Load(object sender, EventArgs e) { WpfControlLibrary1.UserControl1 wpfUC = new WpfControlLibrary1.UserControl1();

wpfUC.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
wpfUC.VerticalContentAlignment = System.Windows.VerticalAlignment.Stretch;

this.elementHost1.Child = wpfUC;

}

5: Run the application, you will see the screen like this, now you see the screen is showing English texts;

image

6: Now we change Windows Form Form1 language property to Chinese, and change label in Form1 content to “你好 in Windows Form”;

image

Now you see a Form1.zh-Han.resx file was added to the project. when you double click the resource file, you will see label1.Text Chinese string was stored there;

7: Unload WpfControlLibrary1 project and edit the project file named WpfControlLibrary.csprj, add <UICulture> into the first <PropertyGroup> section:

image

8: Now we use Visual Studio Command Prompt.

BTW: find the VS command prompt might not easy since it is not there and let you see directly, find it using the following way:

Windows 7 start menu, find the Visual Studio Tools menu under Visual Studio 2013

image

You will see the following command list, choose the one which fit for your Windows environment, for me, I chose Developer Command Prompt for VS2013:

image

Then I change directory to input the following command:

msbuild /t:updateuid WpfControlLibrary1.csproj

The screenshot as the following:

image

9: Now you can see UserControl1.xaml file was modified, all elements were added x:uid attribute:

image

10: Download LocBaml.exe tool:

The LocBaml tool is not a production-ready application. It is presented as a sample that uses some of the localization APIs and illustrates how you might write a localization tool. – From Microsoft.

But, if you use .NET 4.0 above, you have to download from 3rd party website: LocBaml fro .Net 4.0;

for elder version LocBaml, you can download from Microsoft site.

After you downloading, copy LacBaml.exe to obj\Debug folder;

11: Go to obj\Debug foder, run the following command:

locbaml /parse WpfControlLibrary1.g.en-US.resources /out:temp.csv

A file named temp.csv will be generated.

Open temp.csv file using any text editor tool, change Hello to 你好.

Note: if you can not see anything after open temp.csv file, means the temp.csv file generation failed, you have to clean your solution in Visual Studio, then use above locBaml command again.

12: Use the following command to generate Chinese resource file:

locbaml /generate /trans:temp.csv WpfControlLibrary1.g.en-US.resources /out:. /cul:zh-Hans

You will see WpfControlLibrary1.g.zh-Hans.resources file is created in obj\Debug folder.

13: Build localized satellite assembly:

Al.exe /out:WpfControlLibrary1.resources.dll /culture:zh-Hans /embed:WpfControlLibrary1.g.zh-Hans.resources

Now, WpfControlLibrary1.resources.dll file is generated in the same folder.

14: Copy WpfControlLibrary1.resources.dll file to bin\Debug\zh-Hans folder, if no target folder existed, create one by manually;

15: Now go back to Windows Form project WinformHoster, In Form1.cs file, add the following code in Red color to Form1 function:

public Form1()
{
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(“zh-Hans”);
    InitializeComponent();
}

16: Run the application, you will see Chinese text come in both of Windows Form part and WPF part:

image Reference: Microsoft Walkthrough: Localizing a Hybrid Application.