Anyone knows a nice solution to localize routes in ASP.NET MVC? What I'd like to achieve is that these two urls point to the same action/resource:
Also there should be the possibility to generate the routes according to the current culture (or the default, if there are no translations available). Alternately, if I was able to specify just one culture so that only one of the two links above would work, that'd be also viable.
I tried a very nice approach by Maarten Balliauw, but his solution unfortunately doesn't work with Html.RenderAction(...)
.
Of course I could just add routes for all translations like
routes.MapRoute(
"Products_Categories",
"Produkte/Kategorien",
new { controller = "Products", action = "Categories" }
);
but that would end up in an enormous amount of routes and it'd be very unflexible. Any better solution would be appreciated :-) The more flexible the better.
You can try the awesome AttributeRouting project that I just found! You can get it through NuGet.
This might be a viable way to manage all your routes - or some variation of it such as defining the routes in an XML file
http://www.iansuttle.com/blog/post/ASPNET-MVC-Store-Routes-in-the-Database.aspx
you'll still end up with a large number of routes but managing them would be a hell of a lot easier
A simple solution for both attribute and conventional-based routing can be found at https://github.com/boudinov/mvc-5-routing-localization
You can get these working with /de prefix, as it is the preferred schema. You need to have 'Products' and 'Categories' translated to German in a resource file:
http://example.org/de/Produkte/Kategorien
[LocalizedRoute("~/Products/Categories", translateUrl: true]
ActionResult Index(...)
Or working answer to the original question, without the language prefix, you can get like this:
http://example.org/Produkte/Kategorien
[LocalizedRoute("~/Products/Categories", translateUrl: false, explicitCulture: "en")]
[LocalizedRoute("~/Produkte/Kategorien", translateUrl: false, explicitCulture: "de")]
ActionResult Index(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With