Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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
 
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
 
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.
 
MSmile | :)
Posted 16-Sep-11 13:46pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I found my own answer rather than mucking around with inheritance I created a simple class with;
 
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;
 
Dim globalisation_simple As New globalisation_simple
        globalisation_simple.set_thread()
 
It seems to do the job fine.
 
MSmile | :)
  Permalink  
v2

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



Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 18 Sep 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100