talk about the shortcomings of traditional practices
1, do too much language know this thing too spend time
2. Multi-language architectures are typically implemented using resource files, XML, or storage databases. This reduces performance on a certain program
3, the readability of the page is poor, need to and the resource file to switch back and forth
4, change the trouble
5, style compatibility difficult to adjust
6, JS how to deal with
Alternative practices
The traditional approach seems to be really hard to protect on the big, so some people use the alternative approach to do two sets of pages directly. In general, the above one and the following one can be said Dora.
The above a kind of obvious level, but the maintenance cost is not low, the page readability is poor, the style is difficult to adjust, the only advantage is that the page code logic only a set, only at this point accounted for the advantage.
Alternative approach upgrade version
For rapid development and ease of maintenance I made a 2-point upgrade to the alternative approach.
1. Add view engine switch machine dynamic load view engine
Add a custom view engine to Gobal Application_Start to see only the red loop content
Bestviewengine specific contents are as follows:
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Mvc;using Best.widgetfactory.cache;using syntacticsugar;namespace best.site.models{///<summary>//Custom MVC View engine/// </summary> public sealed class Bestviewengine:razorviewengine {public Bestviewengine () { }//<summary>//rewrite Findview//</summary>/<param name= "Controllercon Text "></param>//<param name=" ViewName "></param>//<param name=" Mastername ">& lt;/param>//<param name= "UseCache" ></param>///<returns></returns> Pub LIC override Viewengineresult Findview (ControllerContext controllercontext, String viewName, string mastername, bool UseCache) {var key = "Languagekey";//cookie key var cm = Cookiesmanager<string>. GetInstance (); if (cm. ContainsKey (Key))Verify that the multi-language cookie has the value {var cacheval = Cm[key];//Get a multilingual suffix (e.g. en) if (cacheval.isval Uable ()) ViewName + = "_{0}". Toformat (Cm[key]);//Add the original view name suffix such as index add suffix is index_en} return base. Findview (ControllerContext, ViewName, Mastername, UseCache);//Call Base's Findview method after parameter processing}}}
2, add extension function let a JS support multiple cshtml
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using system.web.mvc;using syntacticsugar;using system.text.regularexpressions;public static class htmlhelperextensions{public static mvchtmlstring pagelanguage (this htmlhelper helper, object obj) { String Str=obj. Modeltojson ();//Convert the object to JSON StringBuilder sb = new StringBuilder (); Sb. Append ("<script> var $pageLanguage ="); Sb. Append (str); Sb. Append ("</script>"); mvchtmlstring mvcstring = new mvchtmlstring (sb.) ToString ()); return mvcstring; } }
How to use this extension function
JS Inside call Pagelanguage Property
We are looking at our project directory, when the user accesses/role/index, our cookie is in en so return view originally found is index.cshtml after our rewrite will find cshtml
If you want to add more languages we just add the corresponding cshtml, JS and the background code are all the same, the Index_en code is as follows
All code
Index_en.cshtml
Index.cshtml
Unified JS (only see the red circle part of the Can)
Is it better to maintain than a resource file? Performance is the highest, isn't it?
The simplest, easiest to maintain, and fastest-to-develop multi-language implementation skills of ASP.