ASP. net2.0languageswithcerandthemeswicher multi-language and multi-style theme Conversion

Source: Internet
Author: User

The multi-language and multi-style theme conversion functions are provided in ASP. NET 2.0. The two implementation modes are similar.

1. Language switcher multi-language conversion

In quick start tutorial, This section describes how to store and apply the language selected by the user. Generally, a dropdownlist is used to display Supported languages for users to choose from. It is usually stored in masterpage, storing the language selected by users. asp is used here. NET 2.0 profile, of course, can also exist in Cookie Session or querystring. Override the initializeculture method on the page and use the language you have selected. Because the setting language operation (here is the selectedindexchanged event) occurs after the initializeculture time, you need to perform a redirection to make the current page take effect immediately after the setting operation is complete, to re-load the page and trigger the initializeculture event. The following shows some code in Quickstart. Pay attention to the red part. Some page addresses may still have queystring, so I personally think the red code should be replaced by response. Redirect (request. url. pathandquery.

Protectedvoid dropdown1_age_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 );
}

Protectedoverrisponid 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 );
}
}

To reduce code duplication, a customer base page class is generally customized to inherit the page class, And the initializeculture method is re-initiated in the Custom page base class. Finally, inherit each page from your custom page base class. In this way, you do not need to rewrite the initializeculture Method on every page.

 

However, the above method is not very good, because every time you add a page, you must modify the post code to inherit the custom page base class.

We noticed that the initializeculture method only modifies the current thread's culture and uiculture. Can we modify these two attributes in a global event, such as an application event? I tried this a long time ago to implement the details of initializeculture when the beginrequest event of the 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 is replaced by other methods, because the profile object has not been created by Asp.net in the inrequest Trigger phase. Cookies can be replaced.

I remember that after doing so, the language settings didn't work. At that time, I thought that processing in global events may be overwritten later, so it may not work. So the initializeculture method was used at the time. Today, I saw someone doing this on the Asp.net forum,

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 it was not set at the time, so I tried again. It turned out to be the reason why the page header command <% @ page uiculture = "Auto" Culture = "Auto" %>, if uiculture and culture are set on the page, they will overwrite the global settings. After removal, the global settings take effect. It seems that the settings of the Culture on the page will overwrite the global settings, and the setting of the initializeculture method (which is exactly a control that supports this method) on the page will overwrite the settings on the page. In fact, the default implementation of the initializeculture method in the page class is empty. Therefore, after the page header command uiculture = "Auto" Culture = "Auto" is removed, the setting in global takes effect.

In addition, if you really want to use profile (like me) to Store Users' choices, you cannot handle them in the beginrequest phase. I handle them 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, the profile has been created, so you can use it.

2. Simplified theme conversion theme switcher

This article describes theme switching and finds that the format is similar to the language switching. He used the httpmodule. I think it is okay to issue the corresponding event processing in the global. asax file directly. It is the same in the end. His storage uses cookies. I still think it is good to use profile. Since it is provided, profile should be cached, so performance should not be a problem.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.