Using Localization in the Masterpage (VB-sample) - Part 1
Posted by BottOm on September 30, 2007
– This tutorial is based on several tutorials / workarounds found on the web. –
The Problem:
When using localization you must override the InitializeCulture() method of a page. As seen on the msdn-tutorial: How Do I: Create a Multi-Lingual Site with Localization? (Around the ninth minute).
Apparently you cannot override the InitalizeCulture() method in a MasterPage. Howcome? It derives from UserControl instead of the common WebForms’ System.Web.UI.Page and does not support the InitializeCulture() method.
The Solution:
I found my solution on www.codeproject.com . But it was a C# example so I adapted a bit.
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
”’ <summary>
”’ Custom base page used for all web forms.
”’ </summary>
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.
<asp:DropDownList ID=”ddlLanguage” runat=”server” AutoPostBack=”True” DataSourceID=”ObjectDataSource1″ DataTextField=”desc_language” DataValueField=”culture” />
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
End note:
All is set now to use resourcefiles for the masterpage and all other pages.
Continue with the second part of the tutorial were you can also download the vb-code.
October 10, 2007 at 10:34 am
Hi. I inherit a BasePage class, as you did, but now I can’t load the pages, because the controls are not loaded. In the Page_Load event of every page I set some properties for labels or buttons and I get the error that these controls don’t exist, even if they appear in the DesignView mode.
This happens only after I change each page to inherit from BasePage class and not from System.Web.UI.Page as before.
Do you have a clue ?
October 10, 2007 at 9:37 pm
Hi Andrei,
I implemented something in a testfile the way you described it in your post but didn’t have any problems.
Maybe you can try adding a new WebForm, inherit it from BasePage and adding your controls and Page_load event afterwards.
If that doesn’t work, maybe you can post some code?