Resources

Release 1.0 - ...

We need to go through two phases to internationalize code: First we must make the code localizable. Then we must localize it. To make the code localizable, we need to remove resources (strings) from the source code and place them in some other container that can be changed at runtime.

The .NET Framework and Visual Studio have a ready-made solution to this problem: .resx files. To create a .resx file in Visual Studio 2005, right-click the project in Solution Explorer; select Add, New Item… and select Resource File.

Visual Studio 2003 and 2005 automatically add .resx files for Windows Forms (e.g. Form1.cs already has an associated Form1.resx file), and this .resx is considered to be under the control of Visual Studio.

Visual Studio 2005 can automatically create a strongly typed class corresponding to a .resx file, and this class is given a name that corresponds to the .resx file. For example, if resources are placed in Messages.resx, Visual Studio 2005 would create a corresponding strongly-typed resources class called Messages.cs.

Visual Studio 2005 Windows Forms applications have a default resource file called Resources.resx. This resource file is for your own use; Visual Studio 2005 will not add or remove resources from it.

The Visual Studio Resource Editor enables you to enter string, image, icon, audio, file, and other resources. For Name enter a name by which this string resource will be identified (e.g., “InsufficientFunds”). You can find resource key name guidelines in the .NET Framework 2.0 help by looking for “Resource Names” in the index, but essentially, names should use Pascal case and should be descriptive but not verbose. For Value, enter the string itself (i.e, “Insufficient funds for the transfer”). Comment is used to hold additional information that the translator or other developers can use. Be aware, however, that the comment is maintained solely within the .resx file.

Visual Basic and Visual C# resource files have four properties: FileName, BuildAction, CustomTool, and CustomToolNamespace.

The BuildAction property indicates what Visual Studio does with a file when a build is executed. BuildAction can have one of several values:

  • None
    The file is not included in the project output group and is not compiled in the build process. An example is a text file that contains documentation, such as a Readme file.
  • Compile
    The file is compiled into the build output. This setting is used for code files.
  • Content
    The file is not compiled, but is included in the Content output group. For example, this setting is the default value for an .htm or other kind of Web file.
  • Embedded Resource
    This file is embedded in the main project build output as a DLL or executable. It is typically used for resource files.

By default, resource files have their Build Action property set to Embedded Resource. This means that Visual Studio will identify the correct compiler for this resource type, compile it, and embed the result in the generated assembly. The correct compiler for .resx files is a built-in version of resgen.exe. This compiler has many purposes, but one of them is to compile XML .resx files into “.resources” files. A .resources file is a binary version of an XML .resx file. Visual Studio then embeds the compiled output in the generated assembly using a build task that is a wrapper for the Assembly Linker (al.exe).

Although there is little value in doing so, you could perform the same steps as Visual Studio manually by maintaining the .resx files outside Visual Studio, compiling them into binary resources manually using resgen.exe, and embedding the resources in the generated assembly using the Assembly Linker tool.