Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Localizing ASP.NET 2.0 Applications : Page 2

As we compete in the global economy, the challenges in globalizing include understanding the language and culture of the local audience. An application written for the American market may not be useable in the Asian market. Learn to localize your application for the world market.


advertisement
Implicit Localization
ASP.NET 2.0 supports implicit localization where the values of controls are checked at run time against a particular resource file based on the specified culture. To see how to use implicit localization in ASP.NET 2.0, I will create a simple Web application and then rename the default Web Form as Registration.aspx. In the Registration.aspx Web Form, populate it with the controls as shown in Figure 1.

 
Figure 1: Populating the Registration.aspx page.
The Calendar control has the following code-behind:

Sub Calendar1_SelectionChanged( _ ByVal sender As Object, _ ByVal e As System.EventArgs) lblDate.Text = Calendar1.SelectedDate. _ ToShortDateString End Sub

Once the form is populated, go to Tools then select Generate Local Resource and Visual Studio 2005 will generate a resource file containing all the text resources used by your controls (see Figure 2).


Figure 2: Generating the local resource in Visual Studio 2005.
 
Figure 3: The LocalResources folder in Solution Explorer.
In Solution Explorer, a new folder named LocalResources will be automatically created and within this folder you will find the resource file generated by Visual Studio 2005. The name of the resource file follows that of the Web Form and ends with a .resx extension (see Figure 3).

To view the resource file generated, double-click on the resource file to invoke the Resource Editor (see Figure 4).

 
Figure 4: Invoking the Resource Editor.
If you now switch the Registration.aspx Web form to Source View, you will see that each control now has an additional attribute called meta:resourcekey. Its value corresponds to each field in the resource file:

<asp:Label ID="lblName" Runat="server" Text="Name" meta:resourcekey="LabelResource1"> </asp:Label> <asp:TextBox ID="txtName" Runat="server" meta:resourcekey="TextBoxResource1"> </asp:TextBox>

For my application, I want it to display in the Chinese language, besides the default English language. To do so, I first go to Solution Explorer and make a copy of the Registration.aspx.resx file. Rename it to Registration.aspx.zh-CN.resx (see Figure 5). The names of resource files use the culture code. In general, you should name your resource files in the following format: filename.aspx.culturecode.resx


Figure 5: The resource file for the Chinese culture.
 
Figure 6: The content of the resource file for Chinese culture.
Open the new resource file and add in the values as shown in Figure 6 using the Resource Editor. Refer to the sidebar Configuring Windows XP for Chinese Language Input on how to configure Windows XP for Chinese language input.

 
Figure 7: Making the page localizable.
To make the ASP.NET Web application localizable, simply add two new attributes to the Page directive (see Figure 7):

These following two attributes automatically map information received in the Accept-language headers to the CurrentCulture and CurrentUICulture properties of the current thread, thus allowing controls on the page which are culture-aware to localize.



Culture="auto" UICulture="auto"

Press F5 to debug the application. You should see the Web Form displayed in English. To display the Web application in Chinese, launch Internet Explorer. From the Tools menu select Internet Options... then click the Languages... button. In the Language Preference window (see Figure 8), click the "Add" button to add a new language. For the sample project I selected "Chinese (China) [zh-cn]." Move the desired language to the top of the list and then click OK.


Figure 8: Changing the language preference in Internet Explorer.
 
Figure 9: Displaying the page in English and Chinese.
To see the page in Chinese, refresh Internet Explorer. Figure 9 shows the page displayed in both English and Chinese.

So how does all this work? At run time, ASP.NET will automatically detect the culture settings of the requester. Based on the culture setting, it then looks for the relevant resource file. If one is found, the values defined in the resource file is used. The Registration.aspx.resx resource file is the resource for the default culture. It will also be used if the requester specifies a culture that is not defined in the LocalResources folder.

It is worth noting that some controls, like the Calendar control, already support localization. In my case, I do not need to do any work on the Calendar control and it is able to display the date in Chinese. Also note the format of the date displayed is different in the English culture and the Chinese culture.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date