Asp.net internationalization-you can select an output language on the page.

Source: Internet
Author: User

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

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.