Environment: vs2012 ASP. Mvc4.
Implementation method: Resource resource file, according to the route rules in the lang parameter to determine which language mode is loaded
Find the relevant information on the Internet, the way to do a practice, the new project and other steps to avoid, the attention of the place, remember that convenient next use.
1: Add the resource file, remember to set the access mode of the resource file to public, otherwise the default is internal, outside will not access:
2: Add a routing rule, remember to precede the default routing rule, otherwise the new rule is useless, for detailed reasons please refer to this article
1 routes. ADD (NewRoute (2"{Lang}/{controller}/{action}/{id}",3New RouteValueDictionary (New{4 Lang ="En-US ", // 5 controller = "account ", 6 action = "logon" , 7 ID = Urlparameter.optional 8 }), new Multilangroutehandler () //10));
1PublicClassMultilangroutehandler:mvcroutehandler {2ProtectedOverrideIHttpHandler Gethttphandler (RequestContext requestcontext) {3string lang = requestcontext.routedata.values["Lang "". ToString (); 5 //thread.currentthread.currentculture = new CultureInfo (lang); 6 Thread.CurrentThread.CurrentUICulture = new CultureInfo (lang); 8 //return new Mvchandler (RequestContext); 9 return base. Gethttphandler (RequestContext); 10 }11}
The CurrentUICulture in the code can refer to my previous article, which contains a detailed explanation.
3: Chinese and English switch the entrance.
1<Div>2@{3String controller = ViewContext.Controller.ValueProvider.GetValue ("Controller"). Rawvalue.tostring ();4 String action = ViewContext.Controller.ValueProvider.GetValue ("action"). Rawvalue.tostring (); 5 //string lang = ViewContext.Controller.ValueProvider.GetValue ("lang"). Rawvalue.tostring (); 6 }7 @Html. ActionLink ("Chinese", action, new {controller = controller, lang = "ZH-CN"}, new {@class = "btn-a" })8 @Html. ActionLink ("中文版", action, new {controller = controller, lang = "en-US"}, new {@class = "btn-a"}) C10>9 </div>
4: Multi-language of plain text on interface
In fact, I have to follow the above article to write the test program, but the death of the resource files are not based on the Chinese and English to switch, I do not know if there is a problem, stuffy ~, then the MVC group inside Friends (ruthless water) introduced another article, I refer to rewrite the next, in this said sound ths ...
public static class Langhelper {//interface plain text multi-language public static string Getlangbykey (this HtmlHelper htmlhelper, String key) {Type ResourceType = (Thread.CurrentThread.CurrentUICulture.Name = = "en-us")? typeof (Resources.en _us): typeof (RESOURCES.ZH_CN); PropertyInfo p = resourcetype.getproperty (key); if (P! = null) return p.getvalue (null, NULL). ToString (); else return "undefined"; }//JS defines the multilingual popup public static string Langoutjsvar (this htmlhelper htmlhelper, string key) {Type R Esourcetype = (Thread.CurrentThread.CurrentUICulture.Name = = "en-us")? typeof (Resources.en_us): typeof (RESOURCES.ZH_CN); PropertyInfo p = resourcetype.getproperty (key); if (P! = null) return string. Format ("var {0} = ' {1} '", Key, p.getvalue (null, NULL). ToString ()); else return string. Format ("var {0} = ' {1} '", Key, "undefined"); } }
The view page calls directly with the function. such as:
5:displayname Multi-lingual
Redefine a Localdisplayname attribute that he inherits from Displaynameattribute
public class Localdisplayname:displaynameattribute { private string _defaultname = ""; Public Type ResourceType { get {return (Thread.CurrentThread.CurrentUICulture.Name = = "en-us")? typeof ( Resources.en_us): typeof (RESOURCES.ZH_CN); } } public string resourcename { get; Set; } Public Localdisplayname (String defaultname) { _defaultname = defaultname; } public override string DisplayName { get { PropertyInfo p = resourcetype.getproperty (resourcename); if (P! = null) return p.getvalue (null, NULL). ToString (); else return _defaultname;}} }
Change the display property to Localdisplayname on the model class
public class Account { [Required] [Localdisplayname ("User name", resourcename = "Userdisplay")] public String UserName {get; set;} [Required] [Localdisplayname ("password", resourcename = "Pwddisplay")] public string Pwd {get; set;} [Required] [Compare ("Pwd", Errormessageresourcename= "Confirmpwderrordisplay")] [Localdisplayname ("Confirm password", resourcename = "Confirmpwddisplay")] public string Confirmpwd {get; set;} [Required] [Localdisplayname ("Remember", resourcename = "Remdisplay")] public bool RememberMe {get; set;} }
Call mode:<div> @Html. labelfor (M = m.username) </div>
Implementation results:
Probably this basic realization, there is a errormessage is not good, have time to write. There are wrong places to correct everyone, thank you!
Reference Documentation:
Http://www.cnblogs.com/codehunter008/archive/2008/09/01/1281565.html
Http://www.cnblogs.com/lionking/articles/1894277.html
If it is to write the text of the database, how should you implement multi-lingual? What a good way to consult under.
Download link: Click to download
Source: http://www.cnblogs.com/Joans/archive/2012/08/16/2640473.html
The example of multi-language station in MVC4