Question

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.

I have a Route Prefix setup as below: [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:

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

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.

Was it helpful?

Solution

Have you tried updating the RouteValueDictionary and passing that as a parameter to your url helper? I do something similar for toggling 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 } );
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top