Principles and code examples of ASP. NET localization/internationalization Solutions

Source: Internet
Author: User
Tags set cookie
Solution Principle:
ASP. NET Localization support
You can use the cultureinfo class to access the attributes set by the region.
In addition, ASP. NET has two attributes based on the default culture of each thread and request tracking: it is used to set currentculture,
And currentuiculture used for region-specific resource data search.

The solution consists of the following parts:
1: The control of the language set by the customer is generally included on each page, so that users can switch the region at any time
Control ascxCode

<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>

Control CS Event code

Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback ){
If (request. Cookies [settings. Default. cookies_language] = NULL | request. Cookies [settings. Default. 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 [settings. Default. 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 );
}
Changelanguage intermediate Page code:

Public partial class changelanguage: system. Web. UI. Page
{
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"]);
}
}

2: When loading each page, the area selected by the current user is determined. Generally, the base class of the page is implemented, for example, Class Name: basepage.
When a user first enters the website identification client browser,
When you have selected a region, load the selected region

/** // <Summary>
/// Localization
/// </Summary>
Protected override void initializeculture ()
{
If (request. Cookies [settings. Default. cookies_language]! = NULL & request. Cookies [settings. Default. cookies_language]. value! = NULL & request. Cookies [settings. Default. cookies_language]. value! = "")
{
Setculture ();
}
Else
{// Set cookie
Response. Cookies [settings. Default. cookies_language]. value =
(Request. headers ["Accept-language"]. Split (",". tochararray () [0]);
Response. Cookies [settings. Default. cookies_language]. expires = datetime. Now. adddays (1000 );
Setculture ();
}
Base. initializeculture ();
}

Private void setculture ()
{

try
{< br> string selectedlanguage = request. cookies [settings. default. 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);
}< br> catch // (exception ex)
{< br> response. cookies. clear (); // [settings. default. cookies_language]. d
// throw;
}< BR >}

3: Of course, it is to prepare resource files for each page.
This is a physical activity,
ASPnet. ascx. En. resx
ASPnet. ascx. Zh. resx, which indicates Chinese characters, can be further divided into finer areas, such as ZH-CN/ZH-HK/ZH-TW.

4: Of course, remember to inherit all your pages from your base class.
Public partial class index: pagebase

 

 

From: http://asp.51aspnet.net/showtopic-103.aspx

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.