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.
Enabling localization.
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.
Note: |
---|
The dialog boxes and menu commands you see might differ from those described in Help, depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. |
Prerequisites
To complete this walkthrough you will need:
- Development Tools for .NET Framework 3.0, which enable you to create a WPF application project. For information on installing these tools, see Installation Instructions for the Windows SDK.
Creating the Windows Forms Host Project
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 Windows Application (WPF) project named LocalizingWpfInWf.
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 Windows Presentation Foundation Composite Control in Windows Forms.
Adding Localizable Content
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 Code Editor.
Set the content of the Button control using the following code.
<UserControl x:Class="LocalizingWpfInWf.SimpleControl" xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" > <Canvas> <Button Content="Hello"/> </Canvas> </UserControl>
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".
Enabling Localization
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 toInitializeComponent
.public Form1() { System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("es-ES"); 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.<UICulture>en-US</UICulture>
Save the project file and close it.
In Solution Explorer, right-click ManuallyAddingAWpfElement and click Reload Project.
Assigning Resource Identifiers
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 2005 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 theUid
attribute to all the elements. This facilitates localization through the assignment of resource identifiers.<UserControl x:Uid="UserControl_1" x:Class="LocalizingWpfInWf.SimpleControl" xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="https://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.
Using LocBaml to Produce a Satellite Assembly
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.
See Also
Reference
Concepts
How to: Localize an Application