DateTime.Compare(date1, date2) > 0) 表示 date1 > date2
DateTime.Compare(date1, date2) == 0) 表示 date1 == date2
DateTime.Compare(date1, date2) < 0) 表示 date1 < date2
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>
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