ASP. NET MVC Multi-language

Source: Internet
Author: User
Tags actionlink

. NET MVC Multi-language web site

Change the language of MVC through browser language preferences, and modify the scripting language with browser language options.

I. Adding a resource file

1. Add App_GlobalResources folder.

2. Add the default resource file and the corresponding language code resource file. such as ZH-CN on behalf of mainland China, en-us to represent U.S. English. Details: http://www.lingoes.cn/zh/translator/langcode.htm

3. Set the resource file to public. The default is internal when new, so it cannot be accessed.

Right-click the resource file, and in its properties, change the custom tool from the Resource Agent Builder (Globalresourceproxygenerator) to the public file (Publicresxfilecodegenerator). The build action is then changed from content to Embedded resource.

4. Add the language option in Web. config. This will determine which resources to use based on the browser language preference.

    <!-- depending on the language preference of the browser, the language item is determined  - <  uiCulture= "Auto"  culture= "Auto"  enableclientbasedculture = "true" />

5. Add key-value pairs in the corresponding file.

Second, the View+control+model common text multi-language

1. View

2. Control

3. Model

// [Required (errormessage = "Password cannot be empty")]  typeof"passwordnotnull")]

Third, the displayname of the multi-language. Overriding the DisplayName method

   Public classLocaldisplayname:displaynameattribute {Private string_defaultname ="";  PublicType ResourceType {Get;Set; }
Public stringresourcename {Get;Set; } PublicLocaldisplayname (stringDefaultName) {_defaultname =DefaultName;} Public Override stringDisplayName {Get{PropertyInfo P=Resourcetype.getproperty (resourcename); if(P! =NULL) { returnP.getvalue (NULL,NULL). ToString (); } Else { return_defaultname; } } } }

Model use:

[Localdisplayname (" release time ""publishdatetime"typeof (Resource))]

Iv. Scripting Multi-language

My approach is to reference a common script file that introduces the corresponding language resource script through the current language. If the current Browse area language does not have a corresponding resource script, the default resource script is introduced.

1, through the browser language to introduce the corresponding language resource script.

//List all language resource script filenames again, with & Splitvarlanguageresources = "EN-US&AMP;ZH-CN"//verifies that the language script exists based on the current browser language and loads the default zh-cn.js if it is loadedvarThislanguagere =navigator.language.toLowerCase ();varScript = document.getElementsByTagName (' HEAD '). Item (0); script= document.createelement ("Script"); Script.type= "Text/javascript"; varLanguage = Languageresources.split ("&")varFlag =false; for(varIteminchlanguage) {    if(Thislanguagere = =Language[item]) {        varFlag =true; SCRIPT.SRC= "/scripts/controls/" + Thislanguagere + ". js";    Document.body.appendChild (script); }}if(Flag = =false) {script.src= "/scripts/controls/zh-cn.js"; Document.body.appendChild (script);}

2, corresponding resources script writing. Set key-value pairs

3, specific page script use, reference. You need to reference the language script first.

Additional: Can be modified in multiple languages, that is, through language hyperlinks to modify the current language items.

1. In the Global.asax file:

   protected voidapplication_acquirerequeststate (Object sender, EventArgs e) {authentication.attachauthticketbyurl< /c0><WebAuthentication>(Context); //Multi-language switchable            if(HttpContext.Current.Session! =NULL) {System.Globalization.CultureInfo ci= (System.Globalization.CultureInfo) This. session["Currentlanguage"]; if(CI = =NULL) {ci=NewSystem.Globalization.CultureInfo ("ZH-CN");  This. session["Currentlanguage"] =CI; } System.Threading.Thread.CurrentThread.CurrentUICulture=CI; System.Threading.Thread.CurrentThread.CurrentCulture=System.Globalization.CultureInfo.CreateSpecificCulture (CI.            Name); }        }

2. In the control:

         Public ActionResult changelanguage (string  language)        {            session["currentlanguage  "] =new  System.Globalization.CultureInfo (language);             return Redirect ("Login");        }

3. In the page:

@Html. ActionLink ("中文版","Changelanguage"," Account",New{language ="en -US"},New{@class ="hex-a"}) @Html. ActionLink ("Chinese","Changelanguage"," Account",New{language ="ZH-CN"},New{@class ="hex-a"})

Resources:

Implementation of the. NET MVC 2 Multilingual site:

http://blog.163.com/xu_shuhao/blog/static/52577487201092402610920/

Examples of multi-lingual stations in MVC4:

Http://www.cnblogs.com/Joans/archive/2012/08/16/2640473.html

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.