属性路由和本地化的问题

时间:2022-10-20 18:22:11

I am using the newest version of the AttributeRouting package on Nuget to setup my routing for my ASP.Net MVC Project. I am creating a website that has two languages, English (primary) and Spanish (Secondary). The urls for the two languages are different. For instance, the about us for english would be like this: www.root.com/en/about-us whereas the Spanish version might be this: www.root.com/es/sobre-nosotros.

我正在使用Nuget上的最新版本的AttributeRouting包来为我的ASP设置路由。净MVC项目。我正在创建一个有两种语言的网站,英语(初级)和西班牙语(中级)。这两种语言的url是不同的。例如,英语中的about us是这样的:www.root.com/en/aboutus,而西班牙语的版本可能是:www.root.com/es/sobre-nosotros。

I have a Route Prefix setup as below: [RoutePrefix("en", TranslationKey = "Home")]

我有一个路由前缀设置如下:[RoutePrefix(“en”,TranslationKey =“Home”)]

Then I have a program that I created that reads values from an XML file into the FluentTranslationProvider. The code for register my routes looks like this:

然后我创建了一个程序,将XML文件中的值读入FluentTranslationProvider中。注册我的路线的代码是这样的:

var translations = new FluentTranslationProvider();
        translations
            .AddTranslations()
            .FromFile();

routes.MapAttributeRoutes(
            config =>
                {
                    config.AddRoutesFromControllersOfType<BaseController>();
                    config.AddTranslationProvider(translations);
                    config.CurrentUICultureResolver =
                        (httpContext, routeData) =>
                        (string) routeData.DataTokens["cultureName"] ??
                        Thread.CurrentThread.CurrentUICulture.Name;
                });

And it seems to be working because I can visit my Routes.axd page and see the following: http://imm.io/nm7Z

它似乎在起作用,因为我可以参观我的路线。axd页面,请查看以下内容:http://imm.io/nm7Z

Later in my page, my code shows that my CurrentCulture is set to es-AR, but when I call the URLHelper class to build a url, it only builds the default english version and will not give me the spanish version. Can anyone give me insight into why this might be the case? I cannot for the life of my figure this out.

稍后在我的页面中,我的代码显示我的CurrentCulture被设置为es-AR,但是当我调用URLHelper类来构建url时,它只构建默认的英语版本,不会给我西班牙语版本。有人能告诉我为什么会这样吗?我不能把我的生活弄明白。

1 个解决方案

#1


1  

Have you tried updating the RouteValueDictionary and passing that as a parameter to your url helper? I do something similar for toggling ssl.

您是否尝试过更新RouteValueDictionary并将其作为参数传递给url helper?我做了一些类似于切换ssl的操作。

Here's some sample code to consider, as a helper function:

下面是一些需要考虑的示例代码,作为辅助函数:

@functions {

  public static string LanguageUrl(WebViewPage page, string actionName, string controllerName, string desiredCulture)
  {
    // translate action name here, if needed.
    return page.Url.Action(actionName, controllerName, new { cultureName = desireCulture } );
  }
}

#1


1  

Have you tried updating the RouteValueDictionary and passing that as a parameter to your url helper? I do something similar for toggling ssl.

您是否尝试过更新RouteValueDictionary并将其作为参数传递给url helper?我做了一些类似于切换ssl的操作。

Here's some sample code to consider, as a helper function:

下面是一些需要考虑的示例代码,作为辅助函数:

@functions {

  public static string LanguageUrl(WebViewPage page, string actionName, string controllerName, string desiredCulture)
  {
    // translate action name here, if needed.
    return page.Url.Action(actionName, controllerName, new { cultureName = desireCulture } );
  }
}