* Links: .NET Desktop - framework\wpf * Apply suggestions from code review Co-authored-by: Andy De George <[email protected]>
7.6 KiB
title, ms.date, helpviewer_keywords, ms.assetid
| title | ms.date | helpviewer_keywords | ms.assetid | ||
|---|---|---|---|---|---|
| Walkthrough: Localizing a Hybrid Application | 08/18/2018 |
|
fbc0c54e-930a-4c13-8e9c-27b83665010a |
Walkthrough: Localizing a Hybrid Application
This walkthrough shows you how to localize [!INCLUDETLA2#tla_winclient] 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.
Prerequisites
You need the following components to complete this walkthrough:
- Visual Studio 2017
Creating the Windows Forms Host Project
The first step is to create the Windows Forms application project and add a [!INCLUDETLA2#tla_winclient] element with content that you will localize.
To create the host project
-
Create a WPF App project named
LocalizingWpfInWf. (File > New > Project > Visual C# or Visual Basic > Classic Desktop > WPF Application). -
Add a [!INCLUDETLA2#tla_winclient]xref:System.Windows.Controls.UserControl element called
SimpleControlto the project. -
Use the xref:System.Windows.Forms.Integration.ElementHost control to place a
SimpleControlelement on the form. For more information, see Walkthrough: Hosting a 3D WPF Composite Control in Windows Forms.
Adding Localizable Content
Next, you will add a Windows Forms label control and set the [!INCLUDETLA2#tla_winclient] 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 xref:System.Windows.Controls.Button control using the following code.
[!code-xamlLocalizingWpfInWf#10]
-
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 xref:System.Windows.Forms.Control.Text%2A property to
"Hello". -
Press F5 to build and run the application.
Both the
SimpleControlelement 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 xref:System.Windows.Forms.Control.Text%2A 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
Form1constructor, preceding the call toInitializeComponent.[!code-csharpLocalizingWpfInWf#2]
-
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
PropertyGroupin the project file.<UICulture>en-US</UICulture> -
Save the project file and close it.
-
In Solution Explorer, right-click LocalizingWpfInWf 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 Developer Command Prompt for Visual Studio.
-
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
msbuildcommand has added theUidattribute to all the elements. This facilitates localization through the assignment of resource identifiers.[!code-xamlLocalizingWpfInWf#20]
-
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 [!INCLUDETLA2#tla_winclient] content.
To produce a satellite assembly
-
Copy LocBaml.exe to your project's obj\Debug folder. For more information, see 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-ESThe 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.resourcesThe 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.