将全球化应用于通用处理程序? [英] applying globalization to generic handler?

查看:92
本文介绍了将全球化应用于通用处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个使用全球化的标准asp.net Web应用程序.我所有的.aspx页面都继承了我的全球化"(是的,这是我的班级使用英国英语拼写以避免任何混乱),但是我无法让我的通用处理程序继承它.我尝试了多种变体,但没有成功.我不确定应该在.Net框架中的哪个位置.

通用处理程序仅提供一些背景知识,可呈现数据库中html类型的内容页面以及一些从资源文件派生的静态值.目前,它始终用于默认资源文件,并且没有从我的全球化类继承任何全球化.

我在下面的全球化课程

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



在我的aspx页面中,我只是简单地说出效果很好



In my aspx pages I simply state which works great

Partial Class my_aspx_page
    Inherits globalisation




为了使全球化在通用处理程序中工作,我的语言化类应该继承什么而不是System.Web.UI.Page?

任何反馈,不胜感激.

M:)




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:)

推荐答案

我找到了自己的答案,而不是搞砸继承,而是使用以下方法创建了一个简单的类;

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()



似乎做得很好.

M:)



It seems to do the job fine.

M:)


这篇关于将全球化应用于通用处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆