Share via


Working with Localization and Globalization

When developing an application that you intend to localize for an international audience, we strongly recommend that you do not hard-code strings, images, sounds, videos, or other resources. Instead, load resources from a resource-only DLL. When you need to modify a resource, you modify it once in the resource DLL rather than needing to update your source code. In addition, the process of localization is much easier and more efficient.

For more information and guidelines about localization and globalization, see Globalizing and Localizing Applications on the MSDN Web site.

In addition, some of the challenges in this area are described in the Planning World-Ready Applications topic on the MSDN Web site, including the following:

  • Determining the extent of world-readiness in the application.
  • Globalizing the application (such as how to accept and process various data formats and languages).
  • Enabling the application for localization.
  • Customizing the application for a given culture/locale.

To add Win32 resources to a managed assembly:

  1. Create an .RC file to represent the resources.

    You can create an .RC file directly in a text editor. Or, if you use Visual Studio, choose Add Resources to create the .RC file.

  2. Compile the .RC file into an .RES file.

    You can use the resource compiler tool (rc.exe) that is included with Visual Studio 2005 (located in %ProgramFiles%\Microsoft Visual Studio 8\VC\Bin). The command line is:

    rc.exe /r path_to_the_RC_file
    

    This tool creates an .RES file with the same name and location as the .RC file.

    You can also configure your .csproj file to automatically compile the .RC file into an .RES file as a pre-build task. Open the .csproj file in a text editor, remove the comment section at the bottom of the .csproj file that includes the BeforeBuild target, and author a target similar to the following as a BeforeBuild step:

    <Target Name="BeforeBuild" Inputs="my_resource_file.rc" Outputs="my_resource_file.res">
        <Exec Command="&quot;%ProgramFiles%\Microsoft Visual Studio 8\VC\bin\rc.exe&quot; /r my_resource_file.rc" />
    </Target>
    

Note This command assumes that you have Visual Studio 2005 installed to the default location on your system.

  1. Build your managed assembly with a reference to the .RES file.

    You can associate an .RES file to a Visual C# project in Visual Studio 2005 by right-clicking on the project in the Solution Explorer, choosing the Application tab, selecting Resource File, and then specifying the .RES file.

    Alternatively, you can open your .csproj file in a text editor and add the following line within the PropertyGroup element the top of the file:

    <Win32Resource>my_resource_file.res</Win32Resource>
    

    Visual Studio passes the file listed in the Win32Resource element to the C# compiler (csc.exe) using the /win32res:filename command-line parameter if it finds that tag in your .csproj file.

The resources can be referenced from markup using syntax such as:

res://[assemblyname.DLL]![resource_name]

To add RESX resources to a managed assembly:

  1. Right-click on the project in Solution Explorer and choose Properties.
  2. Click the Resources tab.
  3. Click the link to add a default resource file to the project.
  4. Use the Add Resource menu to add markup files and assets (such as images, videos, and sounds) to the resource file.
  5. Compile the application assembly.

The resources can be referenced from markup using syntax such as the following:

resx://[assemblyname]/[assemblyname].[resourcefilename]/[resourcename]

See Also