mare
mare

Reputation: 13083

Suggestions for supporting multilingual routes in ASP.NET MVC

There were questions about multilingual apps in MVC here on SO but they were mostly answered by giving details about implementing Resource files and then referencing those Resource strings in Views or Controller. This works fine for me in conjunction with the detection of user's locale.

What I want to do now is support localized routes. For instance, we have some core pages for each website like the Contact Us page.

What I want to achieve is this:

1) routes like this

/en/Contact-us (English route)

/sl/Kontakt (Slovenian route)

2) these two routes both have to go to the same controller and action and these will not be localized (they will be in English because they are hidden away from the user since they are part of the site's core implementation):

My thought is to make the Controller "Feedback" and Action "FeedbackForm"

3) FeedbackForm would be a View or View User control (and it would use references to strings in RESX files, as I said before, I already have set this up and it works)

4) I have a SetCulture attribute attached to BaseController which is the parent of all of my controllers and this attribute actually inherits FilterAttribute and implements IActionFilter - but what does it do? Well, it detects browser culture and sets that culture in a Session and in a cookie (forever) - this functionality is working fine too. It already effects the Views and View User Controls but at this time it does not effect routes.

5) at the top of the page I will give the user a chance to choose his language (sl|en). This decision must override 4). If a user arrives at our website and is detected as Slovenian and they decide to switch to English, this decision must become permanent. From this time on SetCulture attribute must somehow loose its effect.

6) After the switch, routes should immediately update - if the user was located at /sl/Kontakt he should immediately be redirected to /en/Contact-us.

These are the constraints of the design I would like. Simply put, I do not want English routes while displaying localized content or vice-versa.

Suggestions are welcome.

EDIT:

There's some information and guidance here - Multi-lingual websites with ASP.NET MVC, but I would still like to hear more thoughts or practices on this problem.

Upvotes: 1

Views: 1134

Answers (4)

Fernando JS
Fernando JS

Reputation: 4317

I just used a simple solution with "Globalization Resources", like this:

routes.MapRoute(
          "nameroute", // Route name
          App_GlobalResources.Geral.Route_nameroute+"/{Obj}", // URL with parameters
          new { controller = "Index", action = "Details", Obj = UrlParameter.Optional } // Parameter defaults
      );

But, you could customize as needed.

Upvotes: 0

John Farrell
John Farrell

Reputation: 24754

Localization with ASP.NET MVC using Routing

Preview:

For my site the URL schema should look like this in general:

/{culture}/{site}

Imagine there is a page called FAQ, which is available in different languages. Here are some sample URLs for these pages:

/en-US/FAQ /de-DE/FAQ /de-CH/FAQ

Upvotes: 0

takepara
takepara

Reputation: 10433

Translating routes (ASP.NET MVC and Webforms)

How about this? Create custom translate route class.

Upvotes: 3

Tahbaza
Tahbaza

Reputation: 9548

Why not create the action names desired and simply RedirectToAction for the single, real implementation?

    public ActionResult Kontakt() {
        return RedirectToAction("Contact");
    }

    public ActionResult Contact() {
        return View();
    }

Upvotes: 0

Related Questions