Aracılığıyla paylaş


How to: Localize an Application

This tutorial explains how to create a localized application by using the LocBaml tool.

Note

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.

This topic contains the following sections.

  • Overview
  • Requirements
  • Create a Sample Application
  • Create the Neutral Language Resources Satellite Assembly
  • Build the LocBaml Tool
  • Use LocBaml to Parse a File
  • Translate the Localizable Content
  • Use LocBaml to Generate a New .resources.dll File
  • Some Tips for Using LocBaml
  • What's Next
  • Related Topics

Overview

This discussion gives you a step-by-step approach to localizing an application. First, you will prepare your application so that the text that will be translated can be extracted. After the text is translated, you will merge the translated text into a new copy of the original application.

Requirements

Over the course of this discussion, you will use Microsoft build engine (MSBuild), which is a compiler that runs from the command line.

Also, you will be instructed to use a project file. For instructions on how to use MSBuild and project files, see Building and Deploying Windows Presentation Foundation Applications.

All the examples in this discussion use en-US (English-US) as the culture. This enables you to work through the steps of the examples without installing a different language.

Create a Sample Application

In this step, you will prepare your application for localization. In the Windows Presentation Foundation (WPF) samples, a HelloApp sample is supplied that will be used for the code examples in this discussion. If you would like to use this sample, download the Extensible Application Markup Language (XAML) files from LocBaml Tool Sample.

  1. Develop your application to the point where you want to start localization.

  2. Specify the development language in the project file so that MSBuild generates a main assembly and a satellite assembly (a file with the .resources.dll extension) to contain the neutral language resources. The project file in the HelloApp sample is HelloApp.csproj. In that file, you will find the development language identified as follows:

    <UICulture>en-US</UICulture>

  3. Add Uids to your XAML files. Uids are used to keep track of changes to files and to identify items that must be translated. To add Uids to your files, run updateuid on your project file: 

    msbuild /t:updateuid helloapp.csproj

    To verify that you have no missing or duplicate Uids, run checkuid:

    msbuild /t:checkuid helloapp.csproj

    After running updateuid, your files should contain Uids. For example, in the Pane1.xaml file of HelloApp, you should find the following:

    <StackPanel x:Uid="StackPanel_1">

      <TextBlock x:Uid="TextBlock_1">Hello World</TextBlock>

      <TextBlock x:Uid="TextBlock_2">Goodbye World</TextBlock>

    </StackPanel>

Create the Neutral Language Resources Satellite Assembly

After the application is configured to generate a neutral language resources satellite assembly, you build the application. This generates the main application assembly, as well as the neutral language resources satellite assembly that is required by LocBaml for localization. To build the application:

  1. Compile HelloApp to create a dynamic-link library (DLL):

    msbuild helloapp.csproj

  2. The newly created main application assembly, HelloApp.exe, is created in the following folder:

    C:\HelloApp\Bin\Debug\

  3. The newly created neutral language resources satellite assembly, HelloApp.resources.dll, is created in the following folder:

    C:\HelloApp\Bin\Debug\en-US\

Build the LocBaml Tool

  1. All the files necessary to build LocBaml are located in the WPF samples. Download the C# files from the LocBaml Tool Sample sample.

  2. From the command line, run the project file (locbaml.csproj) to build the tool:

    msbuild locbaml.csproj

  3. Go to the Bin\Release directory to find the newly created executable file (locbaml.exe). Example:C:\LocBaml\Bin\Release\locbaml.exe.

  4. The options that you can specify when you run LocBaml are as follows:

    • parse or -p: Parses Baml, resources, or DLL files to generate a .csv or .txt file.

    • generate or -g: Generates a localized binary file by using a translated file.

    • out or -o [filedirectory]: Output file name.

    • culture or -cul [culture]: Locale of output assemblies.

    • translation or -trans [translation.csv]: Translated or localized file.

    • asmpath or -asmpath: [filedirectory]: If your XAML code contains custom controls, you must supply the asmpath to the custom control assembly.

    • nologo: Displays no logo or copyright information.

    • verbose: Displays verbose mode information.

    Note

    If you need a list of the options when you are running the tool, type LocBaml.exe and press ENTER.

Use LocBaml to Parse a File

Now that you have created the LocBaml tool, you are ready to use it to parse HelloApp.resources.dll to extract the text content that will be localized.

  1. Copy LocBaml.exe to your application's bin\debug folder, where the main application assembly was created.

  2. To parse the satellite assembly file and store the output as a .csv file, use the following command:

    LocBaml.exe /parse en-US/HelloApp.resources.dll /out:Hello.csv

    Note

    If the input file, HelloApp.resources.dll, is not in the same directory as LocBaml.exe move one of the files so that both files are in the same directory.

  3. When you run LocBaml to parse files, the output consists of seven fields delimited by commas (.csv files) or tabs (.txt files). The following shows the parsed .csv file for the HelloApp.resources.dll:

    HelloApp.g.en-US.resources:window1.baml,Stack1:System.Windows.Controls.StackPanel.$Content,Ignore,FALSE, FALSE,,#Text1;#Text2;

    HelloApp.g.en-US.resources:window1.baml,Text1:System.Windows.Controls.TextBlock.$Content,None,TRUE, TRUE,,Hello World

    HelloApp.g.en-US.resources:window1.baml,Text2:System.Windows.Controls.TextBlock.$Content,None,TRUE, TRUE,,Goodbye World

    The seven fields are:

    1. BAML Name. The name of the BAML resource with respect to the source language satellite assembly.

    2. Resource Key. The localized resource identifier.

    3. Category. The value type. See Localization Attributes and Comments.

    4. Readability. Whether the value can be read by a localizer. See Localization Attributes and Comments.

    5. Modifiability. Whether the value can be modified by a localizer. See Localization Attributes and Comments.

    6. Comments. Additional description of the value to help determine how a value is localized. See Localization Attributes and Comments.

    7. Value. The text value to translate to the desired culture.

    The following table shows how these fields map to the delimited values of the .csv file:

    BAML name

    Resource key

    Category

    Readability

    Modifiability

    Comments

    Value

    HelloApp.g.en-US.resources:window1.baml

    Stack1:System.Windows.Controls.StackPanel.$Content

    Ignore

    FALSE

    FALSE

    #Text1;#Text2

    HelloApp.g.en-US.resources:window1.baml

    Text1:System.Windows.Controls.TextBlock.$Content

    None

    TRUE

    TRUE

    Hello World

    HelloApp.g.en-US.resources:window1.baml

    Text2:System.Windows.Controls.TextBlock.$Content

    None

    TRUE

    TRUE

    Goodbye World

    Notice that all the values for the Comments field contain no values; if a field doesn't have a value, it is empty. Also notice that the item in the first row is neither readable nor modifiable, and has "Ignore" as its Category value, all of which indicates that the value is not localizable.

  4. To facilitate discovery of localizable items in parsed files, particularly in large files, you can sort or filter the items by Category, Readability, and Modifiability. For example, you can filter out unreadable and unmodifiable values.

Translate the Localizable Content

Use any tool that you have available to translate the extracted content. A good way to do this is to write the resources to a .csv file and view them in Microsoft Excel, making translation changes to the last column (value).

Use LocBaml to Generate a New .resources.dll File

The content that was identified by parsing HelloApp.resources.dll with LocBaml has been translated and must be merged back into the original application. Use the generate or -g option to generate a new .resources.dll file.

  1. Use the following syntax to generate a new HelloApp.resources.dll file. Mark the culture as en-US (/cul:en-US).

    LocBaml.exe /generate en-US/HelloApp.resources.dll /trans:Hello.csv /out:c:\ /cul:en-US

    Note

    If the input file, Hello.csv, is not in the same directory as the executable, LocBaml.exe, move one of the files so that both files are in the same directory.

  2. Replace the old HelloApp.resources.dll file in the C:\HelloApp\Bin\Debug\en-US\HelloApp.resources.dll directory with your newly created HelloApp.resources.dll file.

  3. "Hello World" and "Goodbye World" should now be translated in your application.

  4. To translate to a different culture, use the culture of the language that you are translating to. The following example shows how to translate to French-Canadian:

    LocBaml.exe /generate en-US/HelloApp.resources.dll /trans:Hellofr-CA.csv /out:c:\ /cul:fr-CA

  5. In the same assembly as the main application assembly, create a new culture-specific folder to house the new satellite assembly. For French-Canadian, the folder would be fr-CA.

  6. Copy the generated satellite assembly to the new folder.

  7. To test the new satellite assembly, you need to change the culture under which your application will run. You can do this in one of two ways:

    • Change your operating system's regional settings (Start | Control Panel | Regional and Language Options).

    • In your application, add the following code to App.xaml.cs:

      <Application
          xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
          x:Class="SDKSample.App"
          x:Uid="Application_1"
          StartupUri="Window1.xaml">
      </Application>
      
      using System.Windows; // Application 
      using System.Globalization; // CultureInfo 
      using System.Threading; // Thread 
      
      namespace SDKSample
      {
          public partial class App : Application
          {
              public App()
              {
                  // Change culture under which this application runs
                  CultureInfo ci = new CultureInfo("fr-CA");
                  Thread.CurrentThread.CurrentCulture = ci;
                  Thread.CurrentThread.CurrentUICulture = ci;
              }
          }
      }
      

Some Tips for Using LocBaml

  • All dependent assemblies that define custom controls must be copied into the local directory of LocBaml or installed into the GAC. This is necessary because the localization API must have access to the dependent assemblies when it reads the binary XAML (BAML).

  • If the main assembly is signed, the generated resource DLL must also be signed in order for it to be loaded.

  • The version of the localized resource DLL needs to be synchronized with the main assembly.

What's Next

You should now have a basic understanding of how to use the LocBaml tool. You should be able to make a file that contains Uids. By using the LocBaml tool, you should be able to parse a file to extract the localizable content, and after the content is translated, you should be able to generate a .resources.dll file that merges the translated content. This topic does not include every possible detail, but you now have the knowledge necessary to use LocBaml for localizing your applications.

See Also

Concepts

Globalization for the Windows Presentation Foundation

Use Automatic Layout Overview