ENTERPRISE

Application Patterns and Tips : Localize a Windows Forms Application, Localize an ASP.NET Application

8/6/2012 5:55:54 PM

Understand Localization

Scenario/Problem:You want your program to be translated into multiple languages.
Solution: To display your program’s UI in a different culture requires a bit more work, and the process can be quite different, depending on the technology you use.

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:

  1. Resources.fr-CA.dll

  2. Resources.fr.dll

  3. Resources.dll

  4. Application.exe

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....


Other  
  •  Application Patterns and Tips : Use Model-View-ViewModel in WPF
  •  Liquid-Metal
  •  Vigor 2850n
  •  Visual Studio 2010 : Introducing the Visual Studio Extensibility - Extending the Code Editor
  •  Visual Studio 2010 : Managing Extensions with the Extension Manager, Managing Add-Ins with the Add-In Manager
  •  Intel Xeon Phi: Coprocessor speeding at 1 teraflops in a PCIe Card
  •  Visual Studio Team System 2008 : Working with Test Results (part 2) - Build report and test result
  •  Visual Studio Team System 2008 : Working with Test Results (part 1) - Test as part of Team Foundation Server build
  •  Finance - Apple Versus Google
  •  Oracle Coherence 3.5 : Testing and debugging Coherence applications
  •  Oracle Coherence 3.5 : Accessing the data grid (part 6) - Using the Coherence API - Implementing CoherenceTarget, Testing the Cache loader
  •  Oracle Coherence 3.5 : Accessing the data grid (part 5) - Using the Coherence API - Loader design, Implementing CsvSource
  •  Oracle Coherence 3.5 : Accessing the data grid (part 4) - Using the Coherence API - The basics: NamedCache and CacheFactory
  •  Oracle Coherence 3.5 : Accessing the data grid (part 3) - Configuring Coherence
  •  Oracle Coherence 3.5 : Accessing the data grid (part 2) - Configuring the development environment
  •  Oracle Coherence 3.5 : Accessing the data grid (part 1) - Coherence console
  •  Oracle Coherence 3.5 : Installing Coherence, Starting up the Coherence cluster
  •  The Go-To Reference Design Map For The Cloud?
  •  Separating BPM and SOA Processes : SOA-Oriented Disputes with BEA
  •  Science! – Spintronics
  •  
    Top 10
    Nikon 1 J2 With Stylish Design And Dependable Image And Video Quality
    Canon Powershot D20 - Super-Durable Waterproof Camera
    Fujifilm Finepix F800EXR – Another Excellent EXR
    Sony NEX-6 – The Best Compact Camera
    Teufel Cubycon 2 – An Excellent All-In-One For Films
    Dell S2740L - A Beautifully Crafted 27-inch IPS Monitor
    Philips 55PFL6007T With Fantastic Picture Quality
    Philips Gioco 278G4 – An Excellent 27-inch Screen
    Sony VPL-HW50ES – Sony’s Best Home Cinema Projector
    Windows Vista : Installing and Running Applications - Launching Applications
    Most View
    Bamboo Splash - Powerful Specs And Friendly Interface
    Powered By Windows (Part 2) - Toshiba Satellite U840 Series, Philips E248C3 MODA Lightframe Monitor & HP Envy Spectre 14
    MSI X79A-GD65 8D - Power without the Cost
    Canon EOS M With Wonderful Touchscreen Interface (Part 1)
    Windows Server 2003 : Building an Active Directory Structure (part 1) - The First Domain
    Personalize Your iPhone Case
    Speed ​​up browsing with a faster DNS
    Using and Configuring Public Folder Sharing
    Extending the Real-Time Communications Functionality of Exchange Server 2007 : Installing OCS 2007 (part 1)
    Google, privacy & you (Part 1)
    iPhone Application Development : Making Multivalue Choices with Pickers - Understanding Pickers
    Microsoft Surface With Windows RT - Truly A Unique Tablet
    Network Configuration & Troubleshooting (Part 1)
    Panasonic Lumix GH3 – The Fastest Touchscreen-Camera (Part 2)
    Programming Microsoft SQL Server 2005 : FOR XML Commands (part 3) - OPENXML Enhancements in SQL Server 2005
    Exchange Server 2010 : Track Exchange Performance (part 2) - Test the Performance Limitations in a Lab
    Extra Network Hardware Round-Up (Part 2) - NAS Drives, Media Center Extenders & Games Consoles
    Windows Server 2003 : Planning a Host Name Resolution Strategy - Understanding Name Resolution Requirements
    Google’s Data Liberation Front (Part 2)
    Datacolor SpyderLensCal (Part 1)