MVC中实现多语言环境
1.Global.cs
代码
1 /// <summary>
2 /// 每个请求开始切换语言
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 protected void Application_BeginRequest(Object sender, EventArgs e)
7 {
8 string culture = "en-US";
9 HttpCookie cookie = Request.Cookies["lang"];
10 if (cookie == null)
11 {
12 if ((Request.UserLanguages != null) && (Request.UserLanguages.Length > 0))
13 {
14 culture = Request.UserLanguages[0];
15 }
16 }
17 else
18 {
19 culture = cookie.Value;
20 }
21 System.Globalization.CultureInfo cultureInfo = null;
22 try
23 {
24 cultureInfo = new System.Globalization.CultureInfo(culture);
25 }
26 catch
27 {
28 cultureInfo = new System.Globalization.CultureInfo("en-US");
29 }
30 System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;
31 System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo;
32 }
2 /// 每个请求开始切换语言
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 protected void Application_BeginRequest(Object sender, EventArgs e)
7 {
8 string culture = "en-US";
9 HttpCookie cookie = Request.Cookies["lang"];
10 if (cookie == null)
11 {
12 if ((Request.UserLanguages != null) && (Request.UserLanguages.Length > 0))
13 {
14 culture = Request.UserLanguages[0];
15 }
16 }
17 else
18 {
19 culture = cookie.Value;
20 }
21 System.Globalization.CultureInfo cultureInfo = null;
22 try
23 {
24 cultureInfo = new System.Globalization.CultureInfo(culture);
25 }
26 catch
27 {
28 cultureInfo = new System.Globalization.CultureInfo("en-US");
29 }
30 System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;
31 System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo;
32 }
2.语言切换
代码
1 /// <summary>
2 /// 选择语言
3 /// </summary>
4 /// <param name="lang"></param>
5 /// <returns></returns>
6 public ActionResult SelectLanguage(string lang)
7 {
8 HttpCookie cookie = new HttpCookie("lang");
9 cookie.Value = string.IsNullOrEmpty(lang)? "zh-CN" : lang.Trim();
10 cookie.Expires.AddDays(1);
11 Response.Cookies.Remove("lang");
12 Response.Cookies.Add(cookie);
13
14 string redirectUrl = "/";
15 if (Request.UrlReferrer != null)
16 {
17 redirectUrl = Request.UrlReferrer.PathAndQuery;
18 }
19
20 return Redirect(redirectUrl);
21 }
2 /// 选择语言
3 /// </summary>
4 /// <param name="lang"></param>
5 /// <returns></returns>
6 public ActionResult SelectLanguage(string lang)
7 {
8 HttpCookie cookie = new HttpCookie("lang");
9 cookie.Value = string.IsNullOrEmpty(lang)? "zh-CN" : lang.Trim();
10 cookie.Expires.AddDays(1);
11 Response.Cookies.Remove("lang");
12 Response.Cookies.Add(cookie);
13
14 string redirectUrl = "/";
15 if (Request.UrlReferrer != null)
16 {
17 redirectUrl = Request.UrlReferrer.PathAndQuery;
18 }
19
20 return Redirect(redirectUrl);
21 }
3.添加App_GlobalResources
Resource.resx
Resource.zh-CN.resx
4.在代码中使用
CS:Resources.Resource.Name
View:<%=Resources.Resource.Name%>
5.在JS中查询当前用户选定的语言
1 function getClientLang() {
2 var l = getCookie("lang") || navigator.browserLanguage || navigator.language || "zh-CN";
3 return l.toLowerCase();
4 }
5
2 var l = getCookie("lang") || navigator.browserLanguage || navigator.language || "zh-CN";
3 return l.toLowerCase();
4 }
5