2009年5月2日 星期六

The published solution uses the Session object as storage for the currently selected culture. This will be initialized during Session_Start method that is part of the global.asax file.

If a culture change is requested by the user (through a dropdownlist for example), the MasterPage changes the stored culture in the Session object.

In a BasePage that inherits from Page, the method InitializeCulture is overriden and sets the appropriate culture information stored in the session object to the current thread. Therefore, every Web Form needs to derive from this BasePage.

Step 1. Creating a base page

Under the App_code folder create a new codefile and name it BasePage. This is my code:

Imports Microsoft.VisualBasic
Imports System.Threading
Imports System.Globalization

”’


”’ Custom base page used for all web forms.
”’

Public Class BasePage
Inherits System.Web.UI.Page
Protected Overrides Sub InitializeCulture()

‘Set the UICulture and the Culture with a value stored in a Session-object. I called mine “MyCulture”
Thread.CurrentThread.CurrentUICulture = New CultureInfo(Session(”MyCulture”).ToString)
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Session(”MyCulture”).ToString)
End Sub
End Class

Step 2: Let all pages derive from the BasePage

For every page where you want the localization to work, change Inherits System.Web.UI.Page to Inherits BasePage in the codefile (ex. YourPage.aspx.vb).

For example:

Partial Class Your_Class
Inherits BasePage

End Class

Instead of:

Partial Class Your_Class
Inherits System.Web.UI.Page

End Class

Step 3: Create a Global.asax file

Create a file under the root-folder and name it: Global.asax
Remove all the code and type: <%@ Application CodeBehind=”Global.asax.vb” Inherits=”Global2″ Language=”VB” %>

Under the App_Code folder create a file and name it Global.asax.vb.
Some remarks about the code:
- Use Global2 as classname because Global is a reserved word.
- Only the Session_Start method is necessary.
- I use a cookie (otmData) to store the users’ preference which is put in a Sessionvariable called MyCulture

‘Global.asax.vb codebehind file

Imports System.Web
Imports System.Web.SessionState
Imports System.Threading
Imports System.Globalization

Public Class Global2
Inherits System.Web.HttpApplication

Sub Session_Start(ByVal Sender As Object, ByVal E As EventArgs)

If Not Request.Cookies(”otmData”) Is Nothing Then
‘If a cookie exists, set the session-object with the data from the cookie.
Session(”MyCulture”) = Server.HtmlEncode(Request.Cookies(”otmData”)(”languagePref”))
Else
‘If the cookie doen’t exist (user visits the website for the first time) set the session-object to the default value, in this case English. And ‘create the cookie.
Session(”MyCulture”) = “en”
Dim aCookie As New HttpCookie(”otmData”)
aCookie.Values(”languagePref”) = Session(”MyCulture”)
aCookie.Expires = System.DateTime.Now.AddDays(21)
Response.Cookies.Add(aCookie)
End If
End Sub
End Class

Step 4: Dropdownlist on the masterpage

In my masterpage I created a dropdownlist which is populated with data from a sql-database. You can of course add items to the ddl manualy.
Make sure that the AutoPostBack-value is set to True.

The needed code for the selectedIndexChanged method:

Protected Sub ddlLanguage_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlLanguage.SelectedIndexChanged
‘if the session differs from the value in the ddl, the session-object is changed and the cookie is created
If Not Session(”MyCulture”) = ddlLanguage.SelectedValue Then
Session(”MyCulture”) = ddlLanguage.SelectedValue
Dim aCookie As New HttpCookie(”otmData”)
aCookie.Values(”languagePref”) = Session(”MyCulture”)
aCookie.Expires = System.DateTime.Now.AddDays(21)
Response.Cookies.Add(aCookie)
End If

‘Reload the page
Response.Redirect(Request.Url.ToString)
End Sub

沒有留言: