views:

618

answers:

3

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.

A: 

If the user selects the language, you could just redirect to the appropriate domain? If the user switches his language to "italian", you could Response.Redirect him to "www.domain.it". After that your "InitializeCulture" method will take care.

Robert
+1  A: 

My solution is to split this in two parts:

  1. At session start I store the culture derived from the domain in a session variable
  2. In InitializeCulture I take the value from the session variable to initialize the culture

To change the language I just have to change the session variable and reinitialize the page.

Dirk
+2  A: 
Protected Overrides Sub InitializeCulture()

    'Set the PreferredCulture as the SelectedLanguage by default'
    Dim SelectedLanguage As String = HttpContext.Current.Profile("PreferredCulture").ToString()

    'If there is no PreferredCulture, use these defaults'
    If [String].IsNullOrEmpty(SelectedLanguage) = True Then

        Select Case Request.Url.Host.ToString()
            Case "www.domain.de"
                SelectedLanguage = "de"

            Case "www.domain.it"
                SelectedLanguage = "it"

            Case Else
                SelectedLanguage = "en"

        End Select
    End If

    'Finally set the culture'
    Thread.CurrentThread.CurrentUICulture = New CultureInfo(SelectedLanguage)
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(SelectedLanguage)
End Sub
Greg
Perfect, and such a clean solution. Thank you.
Helen