ASP.NET Setting Culture with InitializeCulture
        Posted  
        
            by Helen
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Helen
        
        
        
        Published on 2010-03-08T09:01:07Z
        Indexed on 
            2010/03/08
            9:06 UTC
        
        
        Read the original article
        Hit count: 723
        
I have a website with three domains .com, .de and .it
Each domain needs to default to the local language/culture of the country. I have created a base page and added an InitializeCulture
  Protected Overrides Sub InitializeCulture()
    Dim url As System.Uri = Request.Url
    Dim hostname As String = url.Host.ToString()
    Dim SelectedLanguage As String
    If HttpContext.Current.Profile("PreferredCulture").ToString Is Nothing Then
        Select Case hostname
            Case "www.domain.de"
                SelectedLanguage = "de"
                Thread.CurrentThread.CurrentUICulture = New CultureInfo(SelectedLanguage)
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(SelectedLanguage)
            Case "www.domain.it"
                SelectedLanguage = "it"
                Thread.CurrentThread.CurrentUICulture = New CultureInfo(SelectedLanguage)
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(SelectedLanguage)
            Case Else
                SelectedLanguage = "en"
                Thread.CurrentThread.CurrentUICulture = New CultureInfo(SelectedLanguage)
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(SelectedLanguage)
        End Select
    End If
End Sub
This is fine. The problem now occurs because we also want three language selection buttons on the home page so that the user can override the domain language.
So on my Default.asp.vb we have three button events like this...
Protected Sub langEnglish_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles langEnglish.Click
    Dim SelectedLanguage As String = "en"
    'Save selected user language in profile 
   HttpContext.Current.Profile.SetPropertyValue("PreferredCulture", SelectedLanguage)
    'Force re-initialization of the page to fire InitializeCulture()
    Context.Server.Transfer(Context.Request.Path)
End Sub
But of course the InititalizeCulture then overrides whatever button selection has been made. Is there any way that the InitialCulture can check whether a button click has occurred and if so skip the routine?
Any advice would be greatly appreciated, thanks.
© Stack Overflow or respective owner