0

我有一个包含三个域 .com、.de 和 .it 的网站

每个域都需要默认为该国的当地语言/文化。我创建了一个基本页面并添加了一个 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

这可以。现在出现问题是因为我们还想要主页上的三个语言选择按钮,以便用户可以覆盖域语言。

所以在我的 Default.asp.vb 上,我们有三个像这样的按钮事件......

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

但当然,InitializeCulture 会覆盖所做的任何按钮选择。InitialCulture 有什么方法可以检查是否发生了按钮单击,如果是,则跳过该例程?

任何建议将不胜感激,谢谢。

4

3 回答 3

4
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
于 2010-03-08T15:27:00.397 回答
1

我的解决方案是将其分为两部分:

  1. 在会话开始时,我将源自域的文化存储在会话变量中
  2. 在 InitializeCulture 中,我从会话变量中获取值来初始化文化

要更改语言,我只需更改会话变量并重新初始化页面。

于 2010-03-08T15:13:35.277 回答
0

如果用户选择语言,您可以重定向到适当的域吗?如果用户将他的语言切换为“意大利语”,您可以 Response.Redirect 他到“www.domain.it”。之后,您的“InitializeCulture”方法将得到照顾。

于 2010-03-08T14:21:40.133 回答