oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Localization in ASP.NET 2.0
Pages: 1, 2, 3, 4

Explicit Localization

Besides automatically setting the culture of the application based on the preference of the web browser, you can also explicitly set a page to display in a specific Culture. To do so, you need to use the CultureInfo class from the System.Globalization namespace. You need to set the CurrentCulture and CurrentUICulture properties of the current thread in the Page_PreInit event. The following sets the page to display in Chinese:

Protected Sub Page_PreInit( _
   ByVal sender As Object, _
   ByVal e As System.EventArgs) Handles Me.PreInit

    Dim lang As System.Globalization.CultureInfo
    lang = New System.Globalization.CultureInfo("zh-CN")

    System.Threading.Thread.CurrentThread.CurrentCulture = lang
    System.Threading.Thread.CurrentThread.CurrentUICulture = lang

End Sub

You also need to remove the two Culture and UICulture attributes present in the Page directive.

However, you will notice something interesting when you press F5 to test the application. Regardless of what language you set in IE, you will always see the page shown in Figure 8. Notice that the Label controls are still displaying the text in English, while the Calendar control has been changed to Chinese.

figure 8
Figure 8. Viewing the page

Unfortunately, implicit localization only works when you set the preferred language in IE. To explicitly set the culture for a page, you need to perform explicit localization. To do so, right-click on project name in Solution Explorer and select Add Folder -> App_GlobalResources Folder (see Figure 9).

figure 9
Figure 9. Adding the App_GlobalResources folder

Right-click on newly created App_GlobalResources folder in Solution Explorer and then select Add New Item…. Select Assembly Resource File (see Figure 10) and use the default name of Resource.resx. Click Add.

figure 10
Figure 10. Adding an assembly resource file to the project

Pages: 1, 2, 3, 4

Next Pagearrow