【问题标题】:Maintaining localization using custom mvc routes使用自定义 mvc 路由维护本地化
【发布时间】:2013-11-02 03:19:23
【问题描述】:

我正在使用此博客条目中的代码在 asp.net mvc 5 中实现一些本地化代码

localization-in-asp.net-mvc

此代码是我发布的内容:

     routes.MapRoute(
          Constants.ROUTE_NAME, // Route name
         string.Format("{{{0}}}/{{controller}}/{{action}}/{{id}}", Constants.ROUTE_PARAMNAME_LANG), // URL with parameters
          new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
  :     );

它在常规 Register_Routes 方法之前调用的自定义 Register_Routes 方法中。

创建一个下拉菜单来更改区域设置以提供与当前 url 的上述路由匹配的 url 很容易:即从 /en-US/Admin 到 /ro-RO/Admin。

但我不明白的是,一旦使用给定的语言环境,如何维护这些 Url。

因此,如果我导航到 /en-US/Admin,然后将文化从我的下拉菜单切换到罗马尼亚语,它将回发,我将在 /ro-RO/Admin。很好,但是说管理页面上有一个指向 /Example 的链接。我现在希望它是 /ro-RO/Example 而不是 /en-US/Example 所以显然我不想使用他们需要动态生成的文字链接作为文化意识。

示例继续使用这个:

public class LocalizationControllerHelper
   {
        public static void OnBeginExecuteCore(Controller controller)
      {
          if (controller.RouteData.Values[Constants.ROUTE_PARAMNAME_LANG] != null &&
               !string.IsNullOrWhiteSpace(controller.RouteData.Values[Constants.ROUTE_PARAMNAME_LANG].ToString()))
            {
                // set the culture from the route data (url)
                var lang = controller.RouteData.Values[Constants.ROUTE_PARAMNAME_LANG].ToString();
              Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(lang);
           }
           else
           {
               // load the culture info from the cookie
               var cookie = controller.HttpContext.Request.Cookies[Constants.COOKIE_NAME];
               var langHeader = string.Empty;
               if (cookie != null)
               {
                   // set the culture by the cookie content
                   langHeader = cookie.Value;
                   Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(langHeader);
              }
               else
              {
                   // set the culture by the location if not speicified
                   langHeader = controller.HttpContext.Request.UserLanguages[0];
                   Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(langHeader);
              }
               // set the lang value into route data
               controller.RouteData.Values[Constants.ROUTE_PARAMNAME_LANG] = langHeader;
           }

           // save the location into cookie
          HttpCookie _cookie = new HttpCookie(Constants.COOKIE_NAME,     Thread.CurrentThread.CurrentUICulture.Name);
           _cookie.Expires = DateTime.Now.AddYears(1);
           controller.HttpContext.Response.SetCookie(_cookie);
       }
   }

这非常简单:一旦您切换给定 url 的语言环境,它会根据路由检查 url 的 lang 参数,如果不存在,则使用存储在 cookie 中的值。

我在其他本地化博客条目中看到过类似的路由表,即 {lang} 作为第一个参数,但问题是如果未提供它,则请求根本不会路由,因为例如“Home”不是已知的文化。此外,Google 似乎建议不要将文化存储在 cookie re: SEO 中。

所以我希望我的文化在 Url 中就像 MSDN 一样。

所以我似乎错过了一篇关于:将文化注入 Urls。我是否应该创建一个自定义 html 帮助程序扩展来生成将当前文化嵌入为第一个参数的操作链接?

这就是我要做的,但是对于示例代码已经到了这么远然后没有这样做,我想知道我是否滥用了它?而且由于我看到其他博客文章做了相同/相似的事情:re{lang} 作为第一个参数,但它们都没有提供在用户切换语言环境后生成链接的任何方法,我不得不怀疑我是否遗漏了一些明显的东西。

【问题讨论】:

    标签: asp.net asp.net-mvc localization asp.net-mvc-routing


    【解决方案1】:

    只需按原样使用 ActionLink 帮助程序,URL 中的当前语言就会自动包含在内。

    例子:

    Current URL: http://localhost/en-US
    ActionLink:  @Html.ActionLink("Create Account", "Register", "Account")
    Generated link: http://localhost/en-US/Account/Register
    
    Current URL: http://localhost:12751/es-MX/Account/Login
    ActionLink:  @Html.ActionLink("Create Account", "Register", "Account")
    Generated link: http://localhost/es-MX/Account/Register
    

    ActionLink 将当前值(在此之前的语言)保留在控制器之前。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      • 2011-04-02
      • 2020-04-15
      相关资源
      最近更新 更多