簡易流程 : 簡單說就是利用cookie紀錄瀏覽器的預設語系,然後在server端切換對應的語系檔(.resource)
1.在controller 加入切換語系的action
public ActionResult ChangeLang(String langCode, String bakCtrl, String bakAct) { //use Cookies save lang code HttpCookie LangCookie = Request.Cookies["lang"]; if (LangCookie == null) { LangCookie = new HttpCookie("lang"); } LangCookie.Value = langCode; LangCookie.Expires.AddDays(1); Response.Cookies.Add(LangCookie); if (String.IsNullOrEmpty(bakCtrl) || String.IsNullOrEmpty(bakAct)) { return RedirectToAction("Index", "Home"); } else { return RedirectToAction(bakAct, bakCtrl); } }
2.在html加入切換語系
…<a href="@Url.Action("ChangeLang", "Home", new { langCode = "zh-TW" })">中文</a>
3.在Global.asax 設定cookie語言的資訊
protected void Application_BeginRequest(Object sender, EventArgs e) { HttpCookie myLang = Request.Cookies["lang"]; if (myLang != null) { System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(myLang.Value); System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(myLang.Value); } }
4.新增resource資源檔 增加欄位即可參考 : https://goo.gl/5gh9gw
編輯多國語系套件 : http://resxmanager.codeplex.com/
沒有留言:
張貼留言