To make a website internationalized and localized, you often need to place a series of language selection buttons on the page. If many languages are supported, you may need to place the following boxes for a language selection.
How to Use Code to implement language switching, Asp. net does not support session-level culture settings, but has application-level and page-level language and culture settings. However, switching requires support for session or profile-level language and culture settings.
of course, we can use the initializeculture function of each page in override or increase the page inheritance level. If every page has an override initializeculture function, it is definitely not a good solution. Although you can write the logic in one place by adding the hierarchy of pages, you still need to change the class of each ASP page. Many files need to be modified. Is there a way to switch the language only in one encoding.
I have obtained some information from the Internet. Some people suggest finding a solution on the httpapplication class, because there is only one httpapplication instance for a web site. In the global. ASAP file. However, this method is not feasible. Although many page events are exposed to instances of the httpapplication class,
however, httpapplication only provides a few events, and the event granularity is too large, there is no way to rewrite the implementation logic of page, especially initializeculture, through httpapplication. This method is different.
We know that initializeculture works based on the language and culture settings of IE. This information is obtained through the request object, but the request object reads the value at this time.
To switch the language, you can only use the overrride initializeculture function.
After resolving where to write the code for language switching, how can we obtain and save the language requirements for user requests. First of all, I think most people will think of getting it in the Click Event of the language switch button. This is a problem because the button click event occurs after initializeculture is called. If you save the language requested by the user in the session or ptofile, you need to access it again to switch to the selected language. Someone suggested that the page should be redirect at this time, but this will bring about another problem. If you access the page more once, your unsaved data will be lost.
In fact, the solution to this problem is very simple. Through the analysis of the ASP. NET mechanism, we can directly access the form variable when the system calls initializeculture to read the user request's user selection.
The following code is required for ASP. net2.0 language switch. I hope it will be helpful to you. 1 /**/ /// <Summary>
2 /// Please put language switch button in ASP page and set button name not ID according to configuration.
3 /// Please do not write any code for Click Event of language button.
4 /// Button can HTML sumbit or imagebttuon type.
5 /// </Summary>
6 Private String [] Languagebuttuonids = New String [] {"Chinesebutton","Englishbutton","Japanesebutton"} ;
7 Private String [] Requestpolicagnames = New String [] {"Zh-CN","En","Ja"} ;
8
9 /**/ /// <Summary>
10 /// Please put language switch combox in ASP page and set combox name not ID according to configuration.
11 /// Please do not write any code for event of selectedindexchanged of parameter agecultureselectcombox also.
12 /// </Summary>
13 Const String Languagecultureselectcombox = " Languagecultureselectcombox " ;
14
15 /**/ /// <Summary>
16 /// GET request language by user through Query Form variants ..
17 /// </Summary>
18 /// <Returns> </returns>
19 Private String Getrequestlanguage ()
20 {
21 For ( Int Indexer = 0 ; Indexer <= Languagebuttuonids. Length - 1 ; Indexer ++ )
22 {
23 // For submit buttoon
24 If ( String . Isnullorempty (system. Web. httpcontext. Current. Request. Form [using agebuttuonids [indexer]) = False )
25 Return Requestemediagnames [indexer];
26 // For imagebutton
27 If ( String . Isnullorempty (system. Web. httpcontext. Current. Request. Form [using agebuttuonids [indexer] + " . X " ) = False )
28 Return Requestemediagnames [indexer];
29 }
30 Return String . Empty;
31 }
32
33 /**/ /// <Summary>
34 /// Reset culture of page and request language will apply immediately without redirect.
35 /// You need override initializeculture function for all pages in whole web site.
36 /// You can use an inherit page class which only override initializeculture function once in base page also.
37 /// </Summary>
38 Protected Override Void Initializeculture ()
39 {
40 Base . Initializeculture ();
41
42 String Requestemediagename = Getrequestlanguage ();
43 If ( String . Isnullorempty (request1_agename) = False )
44 Session [ " Currentrequestlanguage " ] = Requestemediagename;
45 Else If (System. Web. httpcontext. Current. Session ! = Null )
46 Requestemediagename = ( String ) Session [ " Currentrequestlanguage " ];
47
48 If ( String . Isnullorempty (request1_agename) = False )
49 {
50 System. Threading. thread. currentthread. currentculture = System. Globalization. cultureinfo. createspecificculture (requestemediagename );
51 System. Threading. thread. currentthread. currentuiculture = System. Globalization. cultureinfo. createspecificculture (requestemediagename );
52 }
53 }
54