ASP.NET 2.0 provides multi-language conversion and multi-style theme conversion functions. The two implementation forms are relatively similar, so they will be explained together.
1. Language switcher Multi-language switching In the Quick Start Tutorial, it is introduced how to store and apply the language selected by the user. Generally, a DropDownList is used to display the supported languages for the user to choose. It is usually placed in the masterpage and the language selected by the user is stored. The Profile of ASP.NET 2.0 is used here. Of course, it can also be stored in a cookie session or querystring. Override the InitializeCulture method in the page to use the language previously selected by the user. Because the operation of setting the language (here is the SelectedIndexChanged event) occurs after the InitializeCulture time, in order to make the current page effective immediately after the setting operation is completed, a redirection is required to reload this page and trigger the InitializeCulture event. Below is part of the code in quickstart, pay attention to the red part. Because there may be a querystring behind some page addresses, I personally think that the red code part is best replaced by Response.Redirect(Request.Url.PathAndQuery);.
protected void DropDownLanguage_SelectedIndexChanged(object sender, EventArgs e)
{
string SelectedLanguage = DropDownLanguage.SelectedValue.ToString();
//Save selected user language in profile
Profile.SetPropertyValue("PreferredCulture", SelectedLanguage);
//Force re-initialization of the page to fire InitializeCulture()
Response.Redirect(Request.Url.LocalPath);
}
protected override void InitializeCulture()
{
// override virtual method InitializeCulture() to check if profile contains a user language setting
string UserCulture = Profile.GetPropertyValue("PreferredCulture").ToString();
if (UserCulture != "")
{
// there is a user language setting in the profile: switch to it
Thread.CurrentThread.CurrentUICulture = new CultureInfo(UserCulture);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(UserCulture);
}
}
In order to reduce code duplication, a customer base page class is generally customized, making it inherit the Page class, and then re-initializing the InitializeCulture method in the customized page base class. Finally, inherit each of your pages from your custom page base class. This way you don't need to override the InitializeCulture method on every page.
But the above method is still not very satisfying, because every time you add a page, you have to modify the postcode to inherit the custom page base class.
We noticed that in the InitializeCulture method, only the Culture and UICulture of the current thread are actually modified. So can these two attributes be modified in a global event, such as an event of Application? I tried this a long time ago, implementing the details of InitializeCulture when the BeginRequest event of Application is triggered, similar to the following code:
void Application_BeginRequest(object sender, EventArgs e)
{
string lang = string.Empty;//default to the invariant culture
lang = Profile.PreferredCulture;
if (string.IsNullOrEmpty(lang))
{
lang = string.Empty;
}
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
}
Note that the red part should be replaced by other methods, because the profile object has not yet been created by asp.net during the beginrequest triggering phase. Can be replaced by cookies.
I remember that after doing this, it didn't work after setting the language. At that time, I thought that it would be handled in a global event and might be overwritten later, so it might not work. So the InitializeCulture method was still used at that time. Today I saw someone in the asp.net forum doing this.
void Application_BeginRequest(Object sender, EventArgs e){
string lang = string.Empty;//default to the invariant culture
HttpCookie cookie = Request.Cookies["DropDownName"];
if (cookie != null && cookie.Value != null)
lang = Request.Form[cookie.Value];
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
}
So I thought something might not be set properly at that time, so I tried again. It turned out to be the reason for the page header command <%@ Page UICulture="auto" Culture="auto" %>. If UICulture and Culture are set in the page, They will override the global settings. After removing it, the global settings took effect. It seems that the culture settings in the page will override the global settings, and the settings of the InitializeCulture method in the page (to be precise, all controls that support this method) will override the page settings. In fact, the default implementation of the InitializeCulture method in the Page class is empty, so after removing the page header instruction UICulture="auto" Culture="auto", the settings in Global will take effect.
In addition, if you really want to use Profile (like me) to store the user's choices, you can't handle it in the beginrequest stage. I handle it when the PreRequestHandlerExecute event is triggered:
void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
string lang = string.Empty;//default to the invariant culture
lang = Profile.PreferredCulture;
if (string.IsNullOrEmpty(lang))
{
lang = string.Empty;
}
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
}
At this time Profile has been created, so it can be used.
2. Multi-style theme switching Theme switcher
This article talks about Theme switching, and I think the form is very similar to language switching. He uses HttpModule. I think it can be placed directly under the corresponding event processing and distribution in the Global.asax file. In the end, it is the same. It uses cookies for storage. I still think it is better to use Profile. Since it is provided, use it. Profile should be cached, so performance should not be a problem.
Source: Thick accumulation and flourishing BLOG