我的网站有三个域.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检查是否发生了按钮点击,如果是,则跳过例程?
非常感谢任何建议,谢谢。
答案 0 :(得分: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
答案 1 :(得分:1)
我的解决方案是将其分为两部分:
要更改语言,我只需更改会话变量并重新初始化页面。
答案 2 :(得分:0)
如果用户选择了该语言,您只需重定向到相应的域即可?如果用户将他的语言切换为“意大利语”,则可以将Response.Redirect转到“www.domain.it”。之后,您的“InitializeCulture”方法将非常谨慎。