问题
It seems that I have problem changing System.Globalization.CultureInfo.CurrentUICulture
value according to the user preferences (.NET 4.0 web application).
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".
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)
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".
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.CurrentCulture
at the start of every page?
回答1:
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.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) 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 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) In my global.asax
I save the language preference detected from browser as follows:
Session.Add("language", System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName)
Now everything works as expected.
来源:https://stackoverflow.com/questions/5919432/how-can-i-change-the-system-globalization-cultureinfo-currentuiculture-in-my-ne