Asp.net MVC Resource 本地多语言设置
流程:
1、在项目中右键添加 App GlobalResources 如图一。

图一
2、在右击刚才添加的APP——GlobalResources 添加新项目如图二,选择图三中的 Resources File , 并命名为 Res.resx。

图二

图三
3、接着继续添加你用到的语言 比如 中文 Res.zh-cn.resx 、英文 Res.en-us.resx等等。如图四
4、在各个语言项目中设置你所需要的变量即可。如图五所示。

5、在Global.asax.cs 中添加设置语言的方法。
1 protected void Application_BeginRequest(Object sender, EventArgs e) 2 { 3 try 4 { 5 if (Request.Cookies["Lang_Business"] != null) 6 { 7 System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(Request.Cookies["Lang_Business"].Value.ToString()); 8 System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Request.Cookies["Lang_Business"].Value.ToString()); 9 } 10 else 11 { 12 System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("zh-cn"); 13 System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-cn"); 14 15 } 16 } 17 catch (Exception) 18 { } 19 }
6、前端页面设置语言
View:
1 @using Resources 在此添加应用 2 @{ 3 Layout = null; 4 } 5 6 7 8 @using (Html.BeginForm()) 9 { 10 @Html.ValidationSummary(true) 11 <fieldset style="text-align: center"> 12 <legend>@Res.SettingLanguage</legend> 13 14 15 <table> 16 <tr> 17 <td>@Res.SettingLanguage</td> 18 <td> 19 @Html.DropDownList("LanguageList") 20 </td> 21 </tr> 22 23 </table> 24 25 <p> 26 <input type="submit" value= "@Res.Confirm" /> @Res.Confirm 调用的Res 中的变量 27 </p> 28 29 </fieldset> 30 }
Controller:如果Controller 中调用Res中的变量 也需要 添加引用 using Resources;
public ActionResult SettingLanguage() { string lang = string.Empty; if (Request.Cookies["Lang_Business"] != null) { lang = Request.Cookies["Lang_Business"].Value.ToString(); } List<SelectListItem> item = new List<SelectListItem>(); item.Add(new SelectListItem { Value = "zh", Text = Res.Chinese, Selected = lang == "zh-cn" ? true : false }); item.Add(new SelectListItem { Value = "en", Text = Res.English, Selected = lang == "en-us" ? true : false }); this.ViewData["LanguageList"] = item; return View(); } /// <summary> /// 修改语言 /// </summary> /// <param name="fc">获取用户选择信息</param> /// <returns></returns> [HttpPost] public ActionResult SettingLanguage(FormCollection fc) { if (fc["LanguageList"] != null) { string value = fc["LanguageList"].ToString(); switch (value) { case "zh": HttpCookie cookie = Request.Cookies["Lang_Business"]; if (cookie == null) { cookie = new HttpCookie("Lang_Business"); } cookie.Value = "zh-cn"; Response.SetCookie(cookie); break; case "en": HttpCookie cookie1 = Request.Cookies["Lang_Business"]; if (cookie1 == null) { cookie1 = new HttpCookie("Lang_Business"); } cookie1.Value = "en-us"; Response.SetCookie(cookie1); break; } } return RedirectToLocal(Convert.ToInt32(Session["RoleID"])); }
C# winform 控件使用及属性

浙公网安备 33010602011771号