问题描述
在 ASP.net MVC 应用程序中设置文化/UI 文化的最佳位置是什么
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 阅读文化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 ActionFilterhttp://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/索引显示德语站点.
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.
我还制作了一个小型 MVC 5 示例项目,您可以在此处
I also made a small MVC 5 example project which you can find here
只需转到 http://{yourhost}:{port}/en-us/home/index 以查看英语(美国)当前日期,或将其更改为 http://{yourhost}:{port}/de-de/home/index 用于德语等.
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 应用程序中设置文化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!