vb.net 如何更改 .NET 4.0 Web 应用程序中的 System.Globalization.CultureInfo.CurrentUICulture?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5919432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:29:34  来源:igfitidea点击:

How can I change the System.Globalization.CultureInfo.CurrentUICulture in my .NET 4.0 web application?

vb.net.net-4.0internationalizationglobalization

提问by p.matsinopoulos

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

我似乎在System.Globalization.CultureInfo.CurrentUICulture根据用户偏好(.NET 4.0 Web 应用程序)更改值时遇到问题。

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".

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

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

在服务器端,我使用以下代码更改当前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)

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".

现在,在对服务器的下一个请求中,我通过从 获取值来检查当前区域性CultureInfo.CurrentUICulture.TwoLetterISOLanguageName,并且它始终显示“en”,即使我用户选择了“el-GR”。

Where might the problem be?

问题可能出在哪里?

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

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

回答by p.matsinopoulos

I have found the solution to the problem.

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

1) I had to save the user preference in Session. 2) Each time a page is loaded, I now set the Thread.CurrentThread.CurrentCultureafter having built the cultureInfobased 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:

1)我必须在会话中保存用户首选项。2) 每次加载页面时,我现在都会根据用户的 Session 保存的语言偏好设置Thread.CurrentThread.CurrentCulture构建后的页面cultureInfo。我这样做的代码点在 InitializeCulture 覆盖中。换句话说,我的页面覆盖 InitializeCulture 如下:

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) 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 CurrentUICulture 5) I have also set the globalizationtag in my web.configas follows:

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

<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) In my global.asaxI save the language preference detected from browser as follows:

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

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

Now everything works as expected.

现在一切都按预期进行。