Partager via


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.

NoteNote:

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:

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

  1. Create a Windows Application (WPF) project named LocalizingWpfInWf.

  2. Add a WPF UserControl element called SimpleControl to the project.

  3. 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

  1. In Solution Explorer, double-click SimpleControl.xaml to open it in the Code Editor.

  2. 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>
    
  3. In Solution Explorer, double-click Form1 to open it in the Windows Forms Designer.

  4. Open the Toolbox and double-click Label to add a label control to the form. Set the value of its Text property to "Hello".

  5. 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

  1. In Solution Explorer, double-click Form1.cs to open it in the Windows Forms Designer.

  2. In the Properties window, set the value of the form's Localizable property to true.

  3. In the Properties window, set the value of the Language property to Spanish (Spain).

  4. In the Windows Forms Designer, select the label control.

  5. 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.

  6. In Solution Explorer, right-click Form1.cs and click View Code to open it in the Code Editor.

  7. Copy the following code into the Form1 constructor, preceding the call to InitializeComponent.

    public Form1()
    {
        System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("es-ES");
    
        InitializeComponent();
    }
    
  8. In Solution Explorer, right-click LocalizingWpfInWf and click Unload Project.

    The project name is labeled (unavailable).

  9. Right-click LocalizingWpfInWf, and click Edit LocalizingWpfInWf.csproj.

    The project file opens in the Code Editor.

  10. Copy the following line into the first PropertyGroup in the project file.

    <UICulture>en-US</UICulture> 
    
  11. Save the project file and close it.

  12. 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

  1. From the Start Menu, open the Visual Studio 2005 Command Prompt.

  2. Use the following command to assign resource identifiers to your localizable content.

    msbuild /t:updateuid LocalizingWpfInWf.csproj
    
  3. 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="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>
    
  4. 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

  1. Copy LocBaml.exe to your project's obj\Debug folder. For more information, see How to: Localize an Application.

  2. 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
    
  3. Open the temp.csv file with Visual Studio or another text editor. Replace the string "Hello" with its Spanish translation, "Hola".

  4. Save the temp.csv file.

  5. 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.

  6. 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.

  7. Copy the LocalizingWpfInWf.resources.dll file to the project's bin\Debug\es-ES folder. Replace the existing file.

  8. 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

ElementHost
WindowsFormsHost

Concepts

How to: Localize an Application

Other Resources

Migration and Interoperability How-to Topics