Note
In .Net, culture applies to a thread. Each thread actually has two culture settings: culture and UI culture.
The culture is
automatically determined from your region format. In Windows, this is
set in Control Panel | Region and Language | Formats. This determines
the default formats of numbers, times, and currencies in string
formatting , but does not affect which localized resources are used.
The UI culture is
automatically determined from the computer’s native display language. To
view the Windows UI in other languages, you need a localized copy of
Windows, or you can install a language pack (only available with certain
editions of Windows). Windows 7 lets you change display language in
Control Panel | Region and Language | Keyboards and Languages | Display
language.
To ease testing for the
purposes of this section, the UI culture is manually set to be the same
as the non-UI culture in configuration files or in the application
startup code.
Note
All .Net applications,
regardless of platform, obey a hierarchy when looking for resource
files. They look from most specific to least specific, until they get to
the default resource repository (usually the application executable
itself).
Cultures can be
specified with either just the language (French: “fr”), or the language
and a region (French-Canada: “fr-CA”). You must create all of your
resource files using this standard naming scheme. If resources are
stored in a file called Resources.dll, .Net will look for files in this
order:
This general pattern holds true, even if resources are stored in separate folders.
Localize a Windows Forms Application
Windows Forms has the strongest support for localization in the Visual Studio IDE. To localize a form, follow these steps:
1. | Change the form’s Localizable property to true.
|
2. | Change the form’s Language
property to each language you wish to localize. This will generate new
language resource files for the form, such as Form1.en.resx,
Form1.it.resx, and so on.
|
3. | With
the appropriate language selected, modify the text and other properties
(such as location or size) of each element you wish to localize.
|
4. | To add a new control, you must set the language back to Default.
|
Following these steps changes the form’s InitializeComponent method to have a lot of code like this:
resources.ApplyResources(this.labelName, "labelName");
This will look in the
resources for the appropriate values, including text, location, size,
and other properties. Each culture will cause a new directory to be
created in the output directory containing the appropriate resource DLL.
To have a global resource, not tied to a specific form, follow these steps:
1. | (If
one doesn’t exist already) Right-click on the project, and select Add,
then Add New Item..., then Resource file. Name it whatever you want,
e.g. Resources.resx. Visual Studio will generate a class that
automatically reads the resx file for the current culture and returns
the right value with strongly typed properties. Only one class exists,
regardless of how many cultures you want to translate the file into.
|
2. | Add the desired strings and other resources (in the sample, it’s just a string called Message).
|
3. | Copy the resource file, or add a new one, called Resources.it.resx. Make sure it’s in the same folder as the default file.
|
4. | Make the necessary translations and changes.
|
5. | Wherever you need to use the resource, use code similar to the following:
//my resources are in the Properties folder/namespace:
this.labelMessage.Text = Properties.Resources.Message; |
Localize an ASP.NET Application
Localizing ASP.Net applications is conceptually similar to Windows Forms in many ways.
To localize a specific form, follow these steps:
1. | Create the form in the default language (e.g., Default.aspx).
|
2. | Go to the Tools menu and choose Generate Local Resource. Visual Studio will create the App_LocalResources folder, create the file Default.aspx.resx, and populate it with the keys and values from the ASPX file. It will also add meta:resourceKey properties to your ASPX file.
<asp:Label ID="LabelName" runat="server" Text="The Flag:"
meta:resourcekey="LabelNameResource1"></asp:Label>
The name of the resource for this Label's Text property will be LabelNameResource1.Text.
|
3. | Create
additional resource files for each target culture, e.g.
Default.aspx.it.resx, Default.aspx.fr-CA.resx. You can copy the original
file and just rename it to pre-populate it with all the keys and
values.
|
4. | Translate each localized resource file.
|
To create a global resource file (not for a specific ASPX file), follow these steps:
1. | Right-click the project, select Add ASP.Net Folder, then App_GlobalResources.
|
2. | In the App_GlobalResources, add a new resource file (e.g., GlobalResources.resx)
|
3. | Add appropriate values to the file (in this case, just a single string named Message).
|
4. | Copy the file to localized versions, GlobalResources.it.resx, for example.
|
5. | In your ASPX files, add code like this to reference the value Message:
<asp:TextBox ID="TextBox1" runat="server"
Text="<%$ Resources:GlobalResources, Message %>"
meta:resourceKey="TextBoxResource1" />
|
To test your web application, make sure that Culture and uiCulture values for the pages are set to Auto (the default):
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" meta:resourcekey="PageResource1"
uiCulture="Auto" Culture="Auto" %>
Then set your web
browser’s language to the one you want to test and make sure it’s at the
top of the list. Most web browsers allow you to specify desired
languages in order of preference.
In Internet Explorer 8, go to Tools | Internet Options | General | Languages.
In Firefox, go to Tools | Options | Content | Languages | Choose....