2013-11-02 34 views
0

我使用代码从这个博客条目维护本地化使用自定义的MVC路线

localization-in-asp.net-mvc

这个代码是实现一些代码本地化在asp.net mvc的5什么IM张贴有关:

 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方法中。

它很容易创建一个下拉菜单来更改区域设置,以便为当前网址匹配以上路径:即从/ en-US/Admin到/ ro-RO/Admin。

但我没有得到的是如何保持这些Url曾经使用给定的语言环境。

因此,如果我导航到/ en-US/Admin,然后将文化从我的下拉列表切换到罗马尼亚语,它会回发,我将在/ RO-RO/Admin。很棒,但是说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); 
     } 
    } 

这是非常简单的:一旦切换区域设置为某个网址,它会检查根据路线郎PARAM链接,如果不是有它使用存储在cookie中的值。

我在其他本地化博客条目中看到了类似的路由表,即{lang}作为第一个参数,但问题是如果它没有提供,那么请求根本没有路由,因为例如“Home”不是已知的文化。此外谷歌似乎建议反对将文化存储在cookie中:搜索引擎优化。

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

所以它似乎我错过了一件重新:注入文化到Url。我是否应该创建一个自定义html助手扩展来生成嵌入为第一参数的当前文化的操作链接?

那我要做什么,但对于示例代码来到目前为止,然后不这样做,我不知道我是否滥用它?因为我看到其他博客文章做相同/类似的事情:re {lang}作为第一param,但他们都没有提供任何方式来在用户切换语言环境后生成链接,我不知道我是否缺少明显的东西。

回答

0

只需使用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的控制器之前保持(在此之前郎)的当前值。