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
System.Globalization namespace. You need to set the
CurrentUICulture properties of the current thread in the
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
attributes present in the
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. 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. 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. Adding an assembly resource file to the project