Piyush Khatri Piyush Khatri - 7 months ago 44
Vb.net Question

Set UICulture for whole application instead of individual pages in ASP.NET

I have below code in my page,

Dim ci As New CultureInfo("en-US")
Thread.CurrentThread.CurrentCulture = ci
Thread.CurrentThread.CurrentUICulture = ci
MyBase.InitializeCulture()


On page this works fine. When I change culture from dropdown, it sets different culture. Problem is, this works for single page. As soon as I move to next page, culture is set to default. I don't want to write above code on each page.

If anyone can help?

Answer

Simply set the culture inside the AcquireRequestState method in your global.asax

Occurs when ASP.NET acquires the current state (for example, session state) that is associated with the current request.

Sub Application_AcquireRequestState(ByVal sender As Object, ByVal e As EventArgs)
    If Not HttpContext.Current Is Nothing AndAlso Not HttpContext.Current.Session Is Nothing Then
        If (Session("culture") IsNot Nothing) Then
            Dim ci As New System.Globalization.CultureInfo(Session("culture").ToString())
            System.Threading.Thread.CurrentThread.CurrentCulture = ci
            System.Threading.Thread.CurrentThread.CurrentUICulture = ci
        End If
    End If
End Sub

More Information

MSDN: AcquireRequestState Event