【问题标题】:Best place to change CurrentCultureUI with URL approach?使用 URL 方法更改当前文化 UI 的最佳位置?
【发布时间】:2014-07-10 19:12:36
【问题描述】:

我想要完成的是让用户通过 Web 应用程序中的几个漂亮的标志来更改它。放置代码以让用户更改当前线程的最佳位置在哪里?以及实现代码的最佳方式?

这是我目前所拥有的。

我有一个本地化网站。我选择让 URL 路由指定这样的语言:

http://localhost:59293/es/Reports/Performance
http://localhost:59293/en/Reports/Performance

如您所见,“us”和“es”是 UI 语言。我遵循了路由+过滤策略:

  public class InternationalizationAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {

        var language = (string)filterContext.RouteData.Values["language"] ?? "en";
        Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(string.Format("{0}", language));
    }
}

然后我像这样在 Global.Asax 中注册这个动作过滤器:

        protected void Application_Start()
    {

        AreaRegistration.RegisterAllAreas();
        GlobalFilters.Filters.Add(new InternationalizationAttribute());
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        //Creo los mappings de entidades.
        AutoMapperHelper.Configure();

    }

现在的路线:

     routes.MapRoute(
        name: "DefaultLocalizedLanguage",
        url: "{language}/{controller}/{action}/{id}",
        defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional, language = "en" });

到目前为止一切顺利。如果我将我的 URL 更改为指定的语言,将 URL“es”更改为“en”,我可以在这些语言中看到我的应用程序。

现在如何实现线程文化UI的改变?

【问题讨论】:

  • @m.casey 即使这样,我也在阅读很多问题。但我想知道根据我的情况这是否是正确的方法。

标签: c# asp.net-mvc localization globalization


【解决方案1】:

如果我错了,请纠正我 - 更改不是链接到正确的 url 的问题吗?

所以你链接到(伪)@String.Join("es", Request.Url.Segments.Skip(1)) ??

您可能还想让语言是可选的,然后使用 Request Accept-Language 作为后备。

以前我也使用引用来让用户保持在正确的语言环境中。

【讨论】:

  • 调制解调器。感谢回复。这就是我要问的。我已经建立了这个基础,现在我想知道让用户改变cultureUI的最佳方法。之后我打算保留一个 cookie 来记住该选项,但从现在起我有一个过滤器,我要求最佳实践。
猜你喜欢
  • 2023-04-10
  • 2015-04-18
  • 2023-02-03
  • 2012-08-22
  • 1970-01-01
  • 2011-07-24
  • 2013-03-17
相关资源
最近更新 更多