Walkthrough: Localizing a Hybrid Application
This walkthrough shows you how to localize WPF elements in a Windows Forms-based hybrid application.
Tasks illustrated in this walkthrough include:
Creating the Windows Forms host project.
Adding localizable content.
Assigning resource identifiers.
Using the LocBaml tool to produce a satellite assembly.
For a complete code listing of the tasks illustrated in this walkthrough, see Localizing a Hybrid Application Sample.
When you are finished, you will have a localized hybrid application.
The first step is to create the Windows Forms application project and add a WPF element with content that you will localize.
To create the host project
Create a WPF Application project named LocalizingWpfInWf. For more information, see How to: Create a New Windows Forms Application Project.
Add a WPF UserControl element called SimpleControl to the project.
Use the ElementHost control to place a SimpleControl element on the form. For more information, see Walkthrough: Hosting a 3-D WPF Composite Control in Windows Forms.
Next, you will add a Windows Forms label control and set the WPF element's content to a localizable string.
To add localizable content
In Solution Explorer, double-click SimpleControl.xaml to open it in the WPF Designer.
Set the content of the Button control using the following code.
In Solution Explorer, double-click Form1 to open it in the Windows Forms Designer.
Open the Toolbox and double-click Label to add a label control to the form. Set the value of its Text property to "Hello".
Press F5 to build and run the application.
Both the SimpleControl element and the label control display the text "Hello".
The Windows Forms Designer provides settings for enabling localization in a satellite assembly.
To enable localization
In Solution Explorer, double-click Form1.cs to open it in the Windows Forms Designer.
In the Properties window, set the value of the form's Localizable property to true.
In the Properties window, set the value of the Language property to Spanish (Spain).
In the Windows Forms Designer, select the label control.
In the Properties window, set the value of the Text property to "Hola".
A new resource file named Form1.es-ES.resx is added to the project.
In Solution Explorer, right-click Form1.cs and click View Code to open it in the Code Editor.
Copy the following code into the Form1 constructor, preceding the call to InitializeComponent.
In Solution Explorer, right-click LocalizingWpfInWf and click Unload Project.
The project name is labeled (unavailable).
Right-click LocalizingWpfInWf, and click Edit LocalizingWpfInWf.csproj.
The project file opens in the Code Editor.
Copy the following line into the first PropertyGroup in the project file.
Save the project file and close it.
In Solution Explorer, right-click LocalizingWpfInWf and click Reload Project.
You can map your localizable content to resource assemblies by using resource identifiers. The MsBuild.exe application automatically assigns resource identifiers when you specify the updateuid option.
To assign resource identifiers
From the Start Menu, open the Visual Studio Command Prompt.
Use the following command to assign resource identifiers to your localizable content.
msbuild /t:updateuid LocalizingWpfInWf.csproj
In Solution Explorer, double-click SimpleControl.xaml to open it in the Code Editor. You will see that the msbuild command has added the Uid attribute to all the elements. This facilitates localization through the assignment of resource identifiers.
<UserControl x:Uid="UserControl_1" x:Class="LocalizingWpfInWf.SimpleControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > <Canvas x:Uid="Canvas_1"> <Button x:Uid="Button_1" Content="Hello"/> </Canvas> </UserControl>
Press F6 to build the solution.
Your localized content is stored in a resource-only satellite assembly. Use the command-line tool LocBaml.exe to produce a localized assembly for your WPF content.
To produce a satellite assembly
Copy LocBaml.exe to your project's obj\Debug folder. For more information, see How to: Localize an Application.
In the Command Prompt window, use the following command to extract resource strings into a temporary file.
LocBaml /parse LocalizingWpfInWf.g.en-US.resources /out:temp.csv
Open the temp.csv file with Visual Studio or another text editor. Replace the string "Hello" with its Spanish translation, "Hola".
Save the temp.csv file.
Use the following command to generate the localized resource file.
LocBaml /generate /trans:temp.csv LocalizingWpfInWf.g.en-US.resources /out:. /cul:es-ES
The LocalizingWpfInWf.g.es-ES.resources file is created in the obj\Debug folder.
Use the following command to build the localized satellite assembly.
Al.exe /out:LocalizingWpfInWf.resources.dll /culture:es-ES /embed:LocalizingWpfInWf.Form1.es-ES.resources /embed:LocalizingWpfInWf.g.es-ES.resources
The LocalizingWpfInWf.resources.dll file is created in the obj\Debug folder.
Copy the LocalizingWpfInWf.resources.dll file to the project's bin\Debug\es-ES folder. Replace the existing file.
Run LocalizingWpfInWf.exe, which is located in your project's bin\Debug folder. Do not rebuild the application or the satellite assembly will be overwritten.
The application shows the localized strings instead of the English strings.