ASP.NET 使用 InitializeCulture 设置区域性
我有一个包含三个域 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的解决方案是将其分为两部分:
从会话中的域派生
变量
会话变量的值
初始化文化
要更改语言,我只需更改会话变量并重新初始化页面。
My solution is to split this in two parts:
derived from the domain in a session
variable
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.
如果用户选择语言,您可以重定向到适当的域吗?如果用户将其语言切换为“意大利语”,您可以 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.