集文化一个ASP.Net MVC应用程序 [英] Set Culture in an ASP.Net MVC app
问题描述
什么是设置文化/ UI文化的ASP.net MVC应用程序的最佳场所。
What is the best place to set the Culture/UI Culture in an ASP.net MVC app
目前我有一个CultureController类看起来是这样的:
Currently I have a CultureController class which looks like this:
public class CultureController : Controller
{
public ActionResult SetSpanishCulture()
{
HttpContext.Session["culture"] = "es-ES";
return RedirectToAction("Index", "Home");
}
public ActionResult SetFrenchCulture()
{
HttpContext.Session["culture"] = "fr-FR";
return RedirectToAction("Index", "Home");
}
}
和上一个链接网页,如这每种语言的超链接:
and a hyperlink for each language on the homepage with a link such as this:
<li><%= Html.ActionLink("French", "SetFrenchCulture", "Culture")%></li>
<li><%= Html.ActionLink("Spanish", "SetSpanishCulture", "Culture")%></li>
这工作正常,但我想有一个更合适的方式来做到这一点。
which works fine but I am thinking there is a more appropriate way to do this.
我使用以下ActionFilter阅读文化
<一href=\"http://www.iansuttle.com/blog/post/ASPNET-MVC-Action-Filter-for-Localized-Sites.aspx\">http://www.iansuttle.com/blog/post/ASPNET-MVC-Action-Filter-for-Localized-Sites.aspx.我有点一个MVC小白,所以我没有信心,我在正确的位置设置此。我不希望在在web.config水平做,它必须根据用户的选择。我也不想检查他们的HTTP报头从他们的浏览器设置得到的文化。
I am reading the Culture using the following ActionFilter http://www.iansuttle.com/blog/post/ASPNET-MVC-Action-Filter-for-Localized-Sites.aspx. I am a bit of an MVC noob so am not confident I am setting this in the correct place. I don't want to do it at the web.config level, it has to be based on a user's choice. I also don't want to check their http-headers to get the culture from their browser settings.
编辑:
只是要清楚 - 我不是要决定是否使用会话与否。我很高兴与该位。我所试图找出是,如果它是最好做这一场文化控制器,具有操作方法为每一种文化来进行设置,或者是有一个在MVC管道一个更好的地方要做到这一点?
Just to be clear - I am not trying to decide whether to use session or not. I am happy with that bit. What I am trying to work out is if it is best to do this in a Culture controller that has an action method for each Culture to be set, or is there is a better place in the MVC pipeline to do this?
推荐答案
我用这个的定位方法并补充说,设置了文化和语言的路由参数,每当用户访问example.com/xx-xx /
I'm using this localization method and added a route parameter that sets the culture and language whenever a user visits example.com/xx-xx/
例如:
routes.MapRoute("DefaultLocalized",
"{language}-{culture}/{controller}/{action}/{id}",
new
{
controller = "Home",
action = "Index",
id = "",
language = "nl",
culture = "NL"
});
我有一个过滤器,做实际的文化/语言设置:
I have a filter that does the actual culture/language setting:
using System.Globalization;
using System.Threading;
using System.Web.Mvc;
public class InternationalizationAttribute : ActionFilterAttribute {
public override void OnActionExecuting(ActionExecutingContext filterContext) {
string language = (string)filterContext.RouteData.Values["language"] ?? "nl";
string culture = (string)filterContext.RouteData.Values["culture"] ?? "NL";
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", language, culture));
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", language, culture));
}
}
要激活属性国际化,简单地把它添加到您的类:
To activate the Internationalization attribute, simply add it to your class:
[Internationalization]
public class HomeController : Controller {
...
现在,每当访问者进入 http://example.com/de-DE/Home/Index 被显示在德国网站
Now whenever a visitor goes to http://example.com/de-DE/Home/Index the German site is displayed.
我希望这回答了点,你在正确的方向。
I hope this answers points you in the right direction.
I also made a small MVC 5 example project which you can find here
刚刚访问http:// {} yourhost:(港口)/ EN-US /家庭/指标看英语(美国)当前日期,或将其更改为http:// {} yourhost:{端口} /解德/德国诸如此类家/索引。
Just go to http://{yourhost}:{port}/en-us/home/index to see the current date in English (US), or change it to http://{yourhost}:{port}/de-de/home/index for German etcetera.
这篇关于集文化一个ASP.Net MVC应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!