Internationalization is a program that enables us to write with multi-language display. It is a bit like seeing what people say.
Haha!
The following is an example:
Step 1:
Create a new website, add a class in this project, Inherit System. Web. UI. Page, the main code is as follows:
Protected override void initializeculture ()
{
If (request. Cookies ["language"]! = NULL & request. Cookies ["language"]. value! =
Null & request. Cookies ["language"]. value! = "")
{
Setculture ();
}
Else
{// Set cookie
Response. Cookies ["language"]. value =
(Request. headers ["Accept-language"]. Split (",". tochararray () [0]);
Response. Cookies ["language"]. expires = datetime. Now. adddays (1000 );
Setculture ();
}
Base. initializeculture ();
}
Private void setculture ()
{
Try
{
String selectedlanguage = request. Cookies ["language"]. value;
Uiculture = selectedlanguage;
Culture = selectedlanguage;
System. Threading. thread. currentthread. currentculture =
System. Globalization. cultureinfo. createspecificculture (selectedlanguage );
System. Threading. thread. currentthread. currentuiculture = new
System. Globalization. cultureinfo (selectedlanguage );
}
Catch
{
Response. Cookies. Clear ();
}
}
Step 2: Add a webpage with the following code:
Protected void page_load (Object sender, eventargs E)
{
Response. Cookies ["language"]. value = request ["language"];
Response. Cookies ["language"]. expires = datetime. Now. adddays (1000 );
Response. Redirect (server. urldecode (request ["url"]);
}
Step 3:
Add the page to display internationalization:
Note: This page inherits the class we wrote in the first step.
Main Code:
<Asp: Label id = "label1" runat = "server" text = "<% $ resources: resourceglobal, test %>"> </ASP:
Label>
In addition, we also need to perform international testing on this page, so we also need to add a dropdownlist as the selection language.
<Asp: dropdownlist id = "dropdownlist1" runat = "server" autopostback = "true"
Onselectedindexchanged = "dropdownlist1_selectedindexchanged">
<Asp: listitem text = "English version" value = "En-us"> </ASP: listitem>
<Asp: listitem text = "Chinese version" value = "ZH-CN"> </ASP: listitem>
</ASP: dropdownlist>
Add the following code to the code file:
Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
If (request. Cookies ["language"] = NULL | request. Cookies ["language"]. value =
"")
{
Foreach (listitem Li in dropdownlist1.items)
{
If (Li. value = "")
Li. Selected = true;
}
}
Else
{
Foreach (listitem Li in dropdownlist1.items)
{
If (Li. value. tolower () = request. Cookies ["language"]. value. tolower ())
Li. Selected = true;
}
}
}
}
Protected void dropdownlistincluselectedindexchanged (Object sender, eventargs E)
{
Response. Redirect ("~ /Changelanguage. aspx? Language = "+ dropdownlist1.selectedvalue +
"& Url =" + server. urlencode (request. url. pathandquery), true );
}
Step 4: add the resource file resourceglobal. resx and resourceglobal. En. resx. Here, only test in Chinese and English,
Therefore, only two are available. If other languages are displayed, add them as needed.
You must add a test key value to the resource file. This is a test data.
Okay, now you can test it.
Good things:
Zh-CN Simplified Chinese
Zh-tw traditional Chinese
Da-DK Danish
NL-nl Dutch
En-US English
Fi-fi Finnish
Fr-fr French
De-de German
It-It Italian
Ja-JP Japanese
Ko-Kr (Korean)
Nb-No Norwegian
Pt-Br Portuguese
Es-es Spanish
Es-US Spanish (United States)
SV-se Swedish