在我的Web.config我有:
<globalization uiCulture="auto" culture="auto" />
这是我如何改变线程文化:
<li>@HTML.Actionlink("Eng","ChangeCulture","Home",new { lang="en-US"},new { @class = "languageSelectorEnglish" })</li>
第一种方法
我有以下控制器:
public voID ChangeCulture(string lang) { Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(lang); Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang); Response.cookies.Remove("Language"); var langcookie = new httpcookie("Language"); langcookie["Language"] = lang; langcookie.Value = lang; langcookie.Expires = System.DateTime.Now.AddDays(21); Response.cookies.Add(langcookie); Response.Redirect(Request.UrlReferrer.ToString()); }
第二种方法
public ActionResult ChangeCulture(string lang) { Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(lang); Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang); // Split the url to url + query string var fullUrl= Request.UrlReferrer.ToString(); var questionMarkIndex = fullUrl.IndexOf('?'); string queryString = null; string url = fullUrl; if (questionMarkIndex != -1) // There is a queryString { url = fullUrl.Substring(0,questionMarkIndex); queryString = fullUrl.Substring(questionMarkIndex + 1); } // Arranges var request = new httpRequest(null,url,queryString); var response = new httpResponse(new StringWriter()); var httpContext = new httpContext(request,response); var routeData = Routetable.Routes.GetRouteData(new httpContextwrapper(httpContext)); // Extract the data var values = routeData.Values; var controllername = values["controller"]; var actionname = values["action"]; var areaname = values["area"]; return RedirectToAction(actionname.ToString()); }
我在哪里错了?谢谢.
解决方法 看来我需要覆盖我的资源字符串而不是线程的文化.所以我的最终方法是这样的:public voID ChangeCulture(string lang) { Resources.Resources.Culture = new CultureInfo(lang); Response.Redirect(Request.UrlReferrer.ToString()); }
希望这可以帮助.
更新:
当您的应用程序被多个用户使用时,上面的代码不是很好,因为它为每个用户设置相同的文化,无论他们使用什么浏览器.
这样做的好方法是在浏览器中设置一个cookie:
public voID ChangeCulture(string lang) { Response.cookies.Remove("Language"); httpcookie languagecookie = System.Web.httpContext.Current.Request.cookies["Language"]; if (languagecookie == null) languagecookie = new httpcookie("Language"); languagecookie.Value = lang; languagecookie.Expires = DateTime.Now.AddDays(10); Response.setcookie(languagecookie); Response.Redirect(Request.UrlReferrer.ToString()); }
之后(棘手的方法),您需要使每个控制器从一个BaseController继承.这是棘手的,因为你需要重写Initialize.
protected overrIDe voID Initialize(System.Web.Routing.RequestContext requestContext) { httpcookie languagecookie = System.Web.httpContext.Current.Request.cookies["Language"]; if (languagecookie != null) { Thread.CurrentThread.CurrentCulture = new CultureInfo(languagecookie.Value); Thread.CurrentThread.CurrentUICulture = new CultureInfo(languagecookie.Value); } else { //other code here } base.Initialize(requestContext); }总结
以上是内存溢出为你收集整理的c# – 根据链接MVC4改变文化全部内容,希望文章能够帮你解决c# – 根据链接MVC4改变文化所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)