1. Make the interface as before, just pay attention to use labels for all text that requires a multi-language interface
2. After finishing, select the file in Solution Explorer and select Tools->Generate Local Resource
3. You will find that a directory, App_LocalResources, is generated; there is an additional resx file in this directory. For example, if your aspx file is default.aspx, it will generate a file called default.aspx.resx.
4. Open this file and take a look. All the text in the label has gone here.
5. Open the original aspx file and look at the source. You will find that the source code has changed:
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="Default2" title="Untitled Page" Culture="auto" meta: resourcekey="PageResource1" UICulture="auto" %>
…………
<asp:Localize ID="Localize1" runat="server" meta:resourcekey="Localize1Resource1"></asp:Localize>
6. Note here: meta:resourcekey="PageResource1" and meta:resourcekey="Localize1Resource1" This means that the text here is read from the resource.
7. OK now make the resource file for another language. It's very simple, copy Default.aspx.resx and paste it, and then rename it to Default.aspx.fr-fr.resx. Note that fr-fr is the name of the language you want to use. If you watch a DVDrip, you should think it is very similar to a subtitle file, right?
8. Some people say how do I know the name of that language? It's very simple, open IE, tools->internet options->languages->add, it's in the middle of the box.
9. Now start editing the text of other resources. Open Default.aspx.fr-fr.resx, compare the original content, and change the content to French.
10. At this point, we have completed the first stage. ASP.Net will choose the displayed language based on the user's language preference. Change the settings in IE, change the place we just mentioned to fr-fr, open this page and take a look? If it's French, congratulations. If not, go back and do it again...
11. OK, next, we want to allow users to choose their own language instead of automatically. We use cookies to store user choices. At the Application level, we read this cookie and set the appropriate culture and uiculture. Of course, when we first come in, we still have to read the browser settings, which can be obtained from Request.UserLanguages.
Private SupportedLanguages As String() = {"en-us", "fr-fr"}
Public Const LANGUAGE_COOKIE_NAME As String = "UserLanguage"
Sub Application_AcquireRequestState(ByVal sender As Object, ByVal e As EventArgs)
Dim languageCookie As HttpCookie = Request.Cookies(LANGUAGE_COOKIE_NAME)
Dim language As String = String.Empty
If (languageCookie Is Nothing) Then
Dim userLanguages As String() = Request.UserLanguages
Dim index As Integer
For i As Integer = 0 To userLanguages.Length - 1
index = Array.IndexOf(SupportedLanguages, userLanguages(i))
If index >= 0 Then
language = SupportedLanguages(index)
Exit For
End If
Next
If language = String.Empty Then _
language = SupportedLanguages(0)
Response.Cookies.Add(New HttpCookie(LANGUAGE_COOKIE_NAME, language))
Else
language = languageCookie.Value
End If
Dim culture As CultureInfo = New CultureInfo(language)
System.Threading.Thread.CurrentThread.CurrentUICulture = culture
End Sub
12. Then we need to have a page that allows users to select the language. For example, we use two buttons to do it. After clicking this button, we want to set this cookie to the appropriate language:
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
SetLanguage("fr-fr")
End Sub
Protected Sub SetLanguage(ByVal language As String)
Response.Cookies(Global.ASP.global_asax.LANGUAGE_COOKIE_NAME).Value = language
Dim culture As CultureInfo = New CultureInfo(language)
System.Threading.Thread.CurrentThread.CurrentUICulture = culture
Server.Transfer( Request.Path)
End Sub
Note the Server.Transfer(Request.Path) at the end, this is to re-read the page so that the page will also display the newly selected language.
13. Finally we have to remove the automatic selection of ASP.Net. Remove from Default.aspx:
Culture="auto" UICulture="auto"
14. Open it and take a look!