Asp.net MVC internationalization (1) Basics of Internationalization

Source: Internet
Author: User
ArticleDirectory
    • Internationalization mainly involves the following aspects:
    • Resource files in Asp.net MVC
    • Set the culture and UI culture of ASP. NET web pages in declarative Mode
    • Set the culture and UI culture of ASP. NET web pages by programming
Internationalization mainly involves the following aspects:

1. common text internationalization. solved through Asp.net resource file.

2. frontend internationalization. solve this problem through JS Language Pack.

3. Internationalization of the prompt language output in the background (for example, "this user name does not exist ")

4. Internationalization of status enumeration (for example, "normal", "STOPPED)

5. Internationalization of database data (for example, level-1 directory and level-2 Directory)

Resource files in Asp.net MVC

Local files: Within the views folder. Used to internationalize aspx, ascx, template pages, etc. Named for example. login. aspx. en-US.resx

 
<% =This. Getlocalresourceobject ("First") %>

Global File: site root directory. Naming rules: Class Name. LanguageCode. Resx.

 
// Page
 
<% =This. Getglobalresourceobject ("Test","String1") %>
// Background
 
Class Name. Field name

Set the culture and UI culture of ASP. NET web pages in declarative Mode
 
To set the culture and UI culture of all pages, add a globalization section to the Web. config file and set the uiculture and culture attributes, as shown in the following example:
 
 
<Globalization uiculture ="Es"Culture ="Es-mx"/>
 
To set the culture and UI culture of a single page, set the culture and uiculture attributes of the @ page Command, as shown in the following example:
 
 
 
<% @ Page uiculture ="Es"Culture ="Es-mx"%>
 
To enable ASP. NET to set the culture and UI culture to the first language specified in the current browser settings, set uiculture and culture to auto. You can also set this value to Auto: culture_info_name, where culture_info_name is the regional name. For a list of regional names, see cultureinfo. You can perform this setting in the @ page command or the web. config file.

Set the culture and UI culture of ASP. NET web pages by programming
Set the culture and uiculture attributes of the page to a language and a regional string (such as en-US ). These two properties are the internal properties of the page and can only be used in the page.
 
Set the currentuiculture and currentculture attributes of the current thread to the UI culture and culture respectively. The currentuiculture attribute uses a linguistic and regional information string. To set the currentculture attribute, create an instance of the cultureinfo class and call its createspecificculture method.

// On the aspx page
 
Protected Override VoidInitializeculture ()
 
{
 
If(Request. Form ["Listbox1"]! =Null)
 
{
 
String selectedlanguage = request. Form ["Listbox1"];
 
Uiculture = selectedlanguage;
Culture = selectedlanguage;
 
 
 
Thread. currentthread. currentculture =
 
Cultureinfo. createspecificculture (selectedlanguage );
 
Thread. currentthread. currentuiculture =New 
 
Cultureinfo (selectedlanguage );
 
}
 
Base. Initializeculture ();
 
}

 
// Reset the currentuiculture and currentculture attributes of the current thread in httpmodule

 
Public VoidInit (httpapplication context)

 
{

Context. postauthenticaterequest + =NewEventhandler (context_postauthenticaterequest );

 
}

 
 

 
VoidContext_postauthenticaterequest (ObjectSender, eventargs E)

 
{

 
VaR httpcontext = httpcontext. Current;

 
Try

 
{

 
If(Httpcontext. Request. Cookies ["Lang"]! =Null)

{

 
System. Threading. thread. currentthread. currentculture = system. Globalization. cultureinfo. createspecificculture (httpcontext. Request. Cookies ["Lang"]. Value. tostring ());

 
System. Threading. thread. currentthread. currentuiculture =NewSystem. Globalization. cultureinfo (httpcontext. Request. Cookies ["Lang"]. Value. tostring ());

 
}

 
}

 
Catch(Exception)

 
{}

 
}

With the above code, we can simply complete internationalization.

For more information, see here.

To be continued

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.