Click here to Skip to main content
15,916,949 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I've got a standard asp.net web app which uses globalization. All my .aspx pages inherit my "globalisation" (yes thats my class with UK English spelling to avoid any confusion) fine but I cannot get my generic handlers to inherit it. I've tried a number of variants but without success. I'm not sure where in the .Net framework I should be looking.

A little bit of background the generic handler renders html type of content pages from a database along with some static values derived from the resourse files. At the moment it always goes for the default resource file and no globalization gets inherited from my globalisation class.

My globalisation class below

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

Public Class globalisation

    Inherits System.Web.UI.Page

    'Overriding the InitializeCulture method to set the user selected 
    'option in the current thread. Note that this method is called much 
    'earlier in the Page lifecycle and we don't have access to any controls 
    'in this stage, so have to use Form collection. 

    Protected Overloads Overrides Sub InitializeCulture()

        'Get the culture from the session if the control is tranferred to a 
        'new page in the same application. 

        If Session("MyUICulture") IsNot Nothing AndAlso Session("MyCulture") IsNot Nothing Then

            Thread.CurrentThread.CurrentUICulture = DirectCast(Session("MyUICulture"), CultureInfo)
            Thread.CurrentThread.CurrentCulture = DirectCast(Session("MyCulture"), CultureInfo)

        End If

        MyBase.InitializeCulture()

    End Sub

    Protected Sub SetCulture(ByVal name As String, ByVal locale As String)

        Thread.CurrentThread.CurrentUICulture = New CultureInfo(name)
        Thread.CurrentThread.CurrentCulture = New CultureInfo(locale)

        'Saving the current thread's culture set by the User in the Session 
        'so that it can be used across the pages in the current application. 
        Session("MyUICulture") = Thread.CurrentThread.CurrentUICulture
        Session("MyCulture") = Thread.CurrentThread.CurrentCulture

    End Sub

    Public Sub InitializeCulture_from_session()

        Select Case HttpContext.Current.Session("default_local").ToString
            Case "1"
                SetCulture("en-US", "en-US")
                Exit Select
            Case "2"
                SetCulture("en-GB", "en-GB")
                Exit Select
            Case "3"
                SetCulture("zh-CN", "zh-CN")
                Exit Select
            Case Else
                Exit Select
        End Select

        MyBase.InitializeCulture()

    End Sub

End Class


In my aspx pages I simply state which works great

VB
Partial Class my_aspx_page
    Inherits globalisation



To get globalization working in a generic handler what should my gloalisation class be inheriting instead of System.Web.UI.Page?

Any feedback much appreciated.

M:)
Posted

1 solution

I found my own answer rather than mucking around with inheritance I created a simple class with;

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

Public Class globalisation_simple

    Public Sub set_thread()

        If HttpContext.Current.Session("MyUICulture") IsNot Nothing AndAlso HttpContext.Current.Session("MyCulture") IsNot Nothing Then
            Thread.CurrentThread.CurrentUICulture = DirectCast(HttpContext.Current.Session("MyUICulture"), CultureInfo)
            Thread.CurrentThread.CurrentCulture = DirectCast(HttpContext.Current.Session("MyCulture"), CultureInfo)
        End If

    End Sub

End Class


Then from the generic handler I called it by;

VB
Dim globalisation_simple As New globalisation_simple
        globalisation_simple.set_thread()


It seems to do the job fine.

M:)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900