本文介绍了如何在.NET 4.0 Web应用程序中更改System.Globalization.CultureInfo.CurrentUICulture?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎我无法根据用户首选项(.NET 4.0 Web应用程序)更改 System.Globalization.CultureInfo.CurrentUICulture 值。

It seems that I have problem changing System.Globalization.CultureInfo.CurrentUICulture value according to the user preferences (.NET 4.0 web application).

在我的Web应用程序中,我有两个按钮。一种用于希腊语,一种用于英语。单击按钮后,将向服务器发送请求,要求更改用户的首选语言。对于希腊语,我发送 el-GR,对于英语,我发送 en-US。

In my web application, I have two buttons. One for the Greek Language and one for the English language. When the buttons are clicked, a request goes to the server asking to change the user preferred language. For Greek, I send "el-GR" and for English, I send "en-US".

在服务器端,我使用以下代码来更改当前 CultureInfo

On the server side, I use the following piece of code to change the current CultureInfo.

 Dim languageChosen As String = Me.Context.Request.Params("langId")

 ' Code for CultureInfo
 Dim cultureInfo As System.Globalization.CultureInfo
 cultureInfo = New System.Globalization.CultureInfo(languageChosen)

 ' Code for Setting the CurrentCulture
 Thread.CurrentThread.CurrentCulture = cultureInfo
 Thread.CurrentThread.CurrentUICulture = cultureInfo

 Response.Redirect("Default.aspx", True)

现在,在对服务器的下一个请求中,我通过获取 CultureInfo.CurrentUICulture.TwoLetterISOLanguageName 中的值,并且即使用户选择了 el-GR,也始终显示 en。

Now, on the next requests to server, I check the current culture by getting the value from CultureInfo.CurrentUICulture.TwoLetterISOLanguageName, and this always shows "en", even if I the user has picked up "el-GR".

问题可能在哪里?

我必须保存吗请求之间(例如会话中)用户首选的语言?然后在每个页面的开头将其设置为 System.Thread.CurrentThread.CurrentCulture

Do I have to save user preferred language in between requests, for example in a session? And then set it to System.Thread.CurrentThread.CurrentCulture at the start of every page?

推荐答案

我找到了解决问题的方法。

I have found the solution to the problem.

1)我必须将用户首选项保存在Session中。
2)每次加载页面时,在构建 cultureInfo Thread.CurrentThread.CurrentCulture $ c>基于用户的会话保存语言首选项。我这样做的代码点是在InitializeCulture覆盖中。换句话说,我的页面按如下方式覆盖InitializeCulture:

1) I had to save the user preference in Session.2) Each time a page is loaded, I now set the Thread.CurrentThread.CurrentCulture after having built the cultureInfo based on the Session saved language preference of the user. The point of code that I do this is in the InitializeCulture override. In other words, my pages override the InitializeCulture as follows:

Protected Overrides Sub InitializeCulture()
    Dim l_languageChosen As String = Session("language")

    ' Code for CultureInfo
    Dim cultureInfo As System.Globalization.CultureInfo
    cultureInfo = New System.Globalization.CultureInfo(l_languageChosen)

    ' Code for Setting the CurrentCulture
    Thread.CurrentThread.CurrentCulture = cultureInfo
    Thread.CurrentThread.CurrentUICulture = cultureInfo

    MyBase.InitializeCulture()

End Sub

3)为了避免我的所有页面都覆盖此方法并避免重复代码中,我创建了一个超类 MyWebPage,它进行覆盖(并继承自 System.Web.UI.Page ),然后使所有页面继承从MyWebPage。
4)当然,在设置Thread.CurrentThread.CurrentCulture和CurrentUICulture
之后,响应用户更改语言请求的服务器代码也进行了相应更改,以在会话中保存语言首选项5)我也有在我的 web.config 中设置 globalization 标记,如下所示:

3) In order to avoid having all my pages override this method and avoid repetition of code, I have created a super class "MyWebPage" which does the override (and which inherits from System.Web.UI.Page) and then I made all my pages inherit from MyWebPage.4) Of course, the server code that responded to user change language requests changed accordingly to save the language preference in the session, after having set the Thread.CurrentThread.CurrentCulture and CurrentUICulture5) I have also set the globalization tag in my web.config as follows:

<globalization
  uiCulture="auto"
  culture="auto"
  enableClientBasedCulture="true" />

这使我能够检测用户在其浏览器中设置的语言偏好,以便最初可以设置首选语言。

This allowed me to detect the language preference of user set in his/her browser so that I can initially set the preferred language.

6)在我的 global.asax 中,保存从浏览器检测到的语言首选项,如下所示:

6) In my global.asax I save the language preference detected from browser as follows:

Session.Add("language", System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName)

现在一切正常。

这篇关于如何在.NET 4.0 Web应用程序中更改System.Globalization.CultureInfo.CurrentUICulture?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 11:50