實現動態切換語言,Demo 作了三種語言庫能夠切換,包括資源文件的定義,實體對象屬性設置,後臺代碼Controller,IAuthorizationFilter,HtmlHelper的實現,作法比較簡單易學易用,配合我以前發佈的# MVC Scaffolding SmartCode-Engine 更新 模板中新增了多語言資源文件的生成功能,發現個人這個框架和目前很流行的ABP框架是相似更有點像收費版的Asp.net Zero,只是我作的更加輕量級,更方便,更快速,惋惜Asp.net Core 下的Scaffolding這塊擴展不想MVC5那麼容易.這塊還須要研究,下一步就準備升級到asp.net core.javascript
定義實體類
經過Display屬性定義Name ResourceType,須要讀取的語言庫資源文件html
生成資源文件
經過mvc scaffolding擴展工具會自動生成對應實體對象的3個資源文件默認中文,繁體,英文
繁體須要本身翻譯,英文內容根據字段名定義後已大寫字母分割 DateTime 現實 Date Time前端
頁面功能按鈕語言資源文件庫java
先後端代碼實現語言切換功能git
-選擇切換語言github
Js代碼後端
/* multiple lang dropdown */ $('#dropdownlang-dropdown-menu').on('click', 'a', ()=> { const lang = this.text; const flag = this.firstElementChild.className; const culture = this.firstElementChild.getAttribute("culture"); $('#dropdownlang').children()[0].className = flag; $('#dropdownlang').children()[1].innerHTML = lang; localStorage.setItem('lang-text', lang); localStorage.setItem('lang-css', flag); localStorage.setItem('lang-culture', culture); $.get('/Account/SetCulture?lang=' + culture).then(res => { if (res.success) { location.reload(); } }); }); $(()=> { const lang = localStorage.getItem('lang-text'); const css = localStorage.getItem('lang-css'); const culture = localStorage.getItem('lang-culture');
//這段代碼也多餘根本沒有效果 //scripttag = document.createElement("script"); //scripttag.type = "text/javascript"; //scripttag.src = src; //document.body.appendChild(scripttag); //$.parser.parse(); }; if (lang && css && culture) { $('#dropdownlang').children()[0].className = css; $('#dropdownlang').children()[1].innerHTML = lang; } });
後端代碼cookie
[HttpGet] public ActionResult SetCulture(string lang) { //這裏設置CultureInfo是多餘的 switch (lang.Trim()) { case "en": CultureInfo.CurrentCulture = new CultureInfo("en-US"); CultureInfo.CurrentUICulture = new CultureInfo("en-US"); break; case "cn": CultureInfo.CurrentCulture = new CultureInfo("zh-CN"); CultureInfo.CurrentUICulture = new CultureInfo("zh-CN"); break; case "tw": CultureInfo.CurrentCulture = new CultureInfo("zh-TW"); CultureInfo.CurrentUICulture = new CultureInfo("zh-TW"); break; } //這裏設置CultureInfo是多餘的 var cookie = new HttpCookie("culture", lang) { Expires = DateTime.Now.AddYears(1) }; Response.Cookies.Add(cookie); return Json(new { success = true }, JsonRequestBehavior.AllowGet); }
CultureFilter 這是關鍵 這是沒有使用RouteData,經過修改url來保存當前語言要改的地方不少還要修改路由規則,因此我就用Cookies來保存mvc
public class CultureFilter : IAuthorizationFilter { private readonly string defaultCulture; public CultureFilter() { this.defaultCulture = "cn"; } public void OnAuthorization(AuthorizationContext filterContext) { var culture = filterContext.HttpContext.Request.Cookies["culture"]; var lang = defaultCulture; if (culture != null && culture.Value != null) { lang = culture.Value; filterContext.HttpContext.Response.Cookies.Set(culture); } switch (lang.Trim()) { case "en": CultureInfo.CurrentCulture = new CultureInfo("en-US"); CultureInfo.CurrentUICulture = new CultureInfo("en-US"); //Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US"); //Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); break; case "cn": CultureInfo.CurrentCulture = new CultureInfo("zh-CN"); CultureInfo.CurrentUICulture = new CultureInfo("zh-CN"); //Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN"); //Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN"); break; case "tw": CultureInfo.CurrentCulture = new CultureInfo("zh-TW"); CultureInfo.CurrentUICulture = new CultureInfo("zh-TW"); //Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-TW"); //Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-TW"); break; } } }
HtmlHelper 代碼實現語言的輸出
/// <summary> /// 多語言切換 /// </summary> /// <param name="helper"></param> /// <param name="name"></param> /// <returns></returns> public static HtmlString L(this HtmlHelper helper, string name) { var resource = new System.Resources.ResourceManager(typeof(WebApp.resource.Global)); var text = resource.GetString(name); return new HtmlString(text ?? name); } /// <summary> /// 前端easyui或是其它js相關的好比提示信息也須要轉換必須在這裏加載不一樣的語言文件 /// </summary> public static HtmlString LangScriptTag(this HtmlHelper helper,string defaultsrc) { var src = defaultsrc; var lang = CultureInfo.CurrentCulture.Name; switch (lang) { case "en-US": src = "/Scripts/easyui/locale/easyui-lang-en.js"; break; case "zh-CN": src = "/Scripts/easyui/locale/easyui-lang-zh_CN.js"; break; case "zh-TW": src = "/Scripts/easyui/locale/easyui-lang-zh_TW.js"; break; default: src = defaultsrc; break; } return new HtmlString($"<script src=\"{ src }\"></script>"); } //html代碼 <div class="btn-group btn-group-sm"> <button onclick="append()" class="btn btn-default"> <i class="fa fa-plus"></i> @Html.L("Add") </button> </div> <div class="btn-group btn-group-sm"> <button onclick="removeit()" class="btn btn-default"> <i class="fa fa-trash-o"></i> @Html.L("Delete") </button> </div> <div class="btn-group btn-group-sm"> <button onclick="accept()" class="btn btn-default"> <i class="fa fa-floppy-o"></i> @Html.L("Save") </button> </div>