Localizing your C# project

There are many posts all over the internet asking how to create language files and what the steps are.  I’ve had to do this myself more than once and have found myself searching multiple times for the same information because I don’t do it often enough to remember it. The problem is nowhere have I been able to find step by step instructions on what to do, so I’ve always had to piece things together.

So the next time I need to do this, as long as I remember the that I’ve posted this, it hopefully won’t be a long search until I find the answer!

When the main form loads, set the culture to the current culture of the OS:

//set culture
if (Properties.Resources.Culture == null || Properties.Resources.Culture.Name != System.Globalization.CultureInfo.CurrentCulture.Name)
{
Properties.Resources.Culture = new System.Globalization.CultureInfo(System.Globalization.CultureInfo.CurrentCulture.Name);
}

The Localization property of any form that needs to be affected by this needs to be set to true.

Project properties tabs

Project properties tabs

It is important to note that the strings that will be used in your project must be retrieved using Properties.Resources and NOT Properties.Settings.Default. These resource strings are added using the Resource tab in the project properties. Use these strings to assign text to controls:

button1.Text = Properties.Resources.strClose;

To create files in other languages, use a text file which has name=value pairs, one per line, like this:

strClose=Close
strSave=Save

The values above are still in English but in your text file you’ll use values that are in the language for which you are creating the file. When the text file is complete, create a resource file using resgen.exe. Resgen.exe can be found in: c:\program files\microsoft sdks\windows\v6.0a\bin\ if you have installed the Microsoft SDK. I don’t remember which SDK or where to get it from. I’ll have to search for that again.

If the values will have accented characters, ensure that the application allows saving the text file using UTF-8 formatting or the accented characters will be lost or corrupted.

Use the command like this: resgen my_text_file.txt Resources.es-MX.resx.

A list of culture names can be found here.

Add the newly created resource file to your porject. Place the newly added file in the Properties folder.

List of resource files

List of resource files

Build your project.

You’re done!

Some points to note:

  • If the culture of the OS doesn’t exactly match the culture file you have created, then it will not be used. To create a file that will be used by the appropriate language regardless of the country, when naming the file using the resgen command, simply omit the country code. An example for french is shown in the image above.
  • Regarding the first point, this isn’t necessary for the default language of the project because it will of course be used if an exact match can’t be found.

So to recap:

  1. Load the OS culture
  2. Set the Localization property of the form to True
  3. Create your string variables using the Resources tab of the project properties
  4. Create a text file with name/value pairs, one per line, in the form name=value
  5. Using resgen create a resource file
  6. Add the resource file to the project and move it to the Properties folder
  7. Build your project

I’ve just recently discovered that rather than using resgen, you can simply add a resource file using Visual Studio. Make sure the name of the file follows the mentioned convention though, and you’ll have to move the file to the Properties folder. This makes it much easier since you can simply copy/paste from other resource files and then change the values in the new file.

This entry was posted in Tech and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *