ASP.NET 使用 InitializeCulture 设置区域性

发布于 2024-08-23 19:41:15 字数 1926 浏览 5 评论 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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘦怹 2024-08-30 19:41:15
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
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
海之角 2024-08-30 19:41:15

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

  1. 在会话开始时,我存储文化
    从会话中的域派生
    变量
  2. 在 InitializeCulture 我采取
    会话变量的值
    初始化文化

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

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.

如日中天 2024-08-30 19:41:15

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文