views:

294

answers:

3

I need multi language url route of existing controller. let's explain more:

I have a controller with name "Product" and View with name "Software", therefore by default if user enter "http://mysite.com/en/Product/Software" ,get right content (that really exist in http://mysite.com/Product/Software),

but i need if other user (french user) type "http://mysite.com/fr/Produits/logiciels", must get above controller and show with right content (same http://mysite.com/Product/Software but with french text).

Note: I set route table with "{language}/{controller}/{action}/{id}"

any other invalid url must show 404 page

Is it possible?

Help me.

Thanks

+1  A: 

You should have url something like "http://mysite.com/en/Product/Software" for English and "http://mysite.com/fr/Product/Software" for French, which would make much sense.

Use the same view for both.

Happy coding.

Ravia
This means non-friendly URLs, since not everyone in the world speaks English
erikkallen
By default you should use English erikkallen
Ravia
why did you down vote man
Ravia
@erikkallen: So should the OP aim to provide URLs in all possible languages? The URL should identify a resource, and in doing so be as descriptive as possible of the nature of that resource. This does *not* mean that there should be multiple URLs to cater for multiple languages. The language in which the resource is rendered is what matters. Upvoted.
Will Vousden
Many thanks for fast response, http://mysite.com/en/Product/Software is ok for me, but how to do it ??? how to find right controller and view and set it???Note: i using "{language}/{controller}/{action}/{id}" for route
Hamid
Hamid please check this link http://stackoverflow.com/questions/725220/language-for-controllers-names-in-asp-net-mvc answered by Fred
Ravia
+2  A: 

As has been suggested before, this does depart from convention where the website urls (and routes) use English.

Nevertheless, it is possible, but in order to do it, you'll probably have to look at generating one route per action for every foreign language. So for a website with 20 actions and three languages (English, French, and German), you'll need 41 routes (20 French, 20 German and 1 English). Not the most efficient system, I admit, but it works as you want it to.

//You'll only need one of these, which is the default.
routes.MapRoute(
  "English route",
  "en/{controller}/{action}/{id}"
  new { controller = "Home", action = "Index", language = "en" },
);

routes.MapRoute(
  "FrenchHome",
  "fr/Demarrer/Index/{id}",
  new { controller = "Home", action = "Index", language = "fr" }
);

routes.MapRoute(
  "GermanHome",
  "de/Heim/Index/{id}", //'Heim' is, I believe the correct usage of Home in German.
  new { controller = "Home", action = "Index", language = "de" }
);

//Some more routes...

routes.MapRoute(
  "FrenchSoftware",
  "fr/Produit/Logiciels/{id}",
  new { controller = "Product", action = "Software", language = "fr" }
);

routes.MapRoute(
  "GermanSoftware",
  "de/Produkt/Software/{id}", //In this instance, Software should be the same in German and English.
  new { controller = "Product", action = "Software", language = "de" }
);

//And finally, the 404 action.
routes.MapRoute(
  "Catchall",
  "{language}/{*catchall}",
  new { controller = "Home", action = "PageNotFound", language = "en" },
  new { language = "^(en|fr|de)$" }
);

//This is for the folks who didn't put a language in their url.
routes.MapRoute(
  "Catchall",
  "{*catchall}",
  new { controller = "Home", action = "PageNotFound", language = "en" }
);

In your actions, for example Product/Software...

public ActionResult Software(string language, int id)
{
  //This would go off to the DAL and get the content in whatever language you want.
  ProductModel model = ProductService.GetSoftware(language, id);

  return View(model);
}

I would LOVE it if somebody came along and said that there's a better way of doing this, because I agree that having the url in a foreign language isn't good, and given that the Internet itself is moving towards allowing non-Roman characters in urls, the sooner we look at solutions to this, the better.

Not only that, but I know proud French people don't like to see their website urls contain English. :)

Dan Atkinson
+4  A: 

Here's the way to go: Maarten Balliauw's blog
Hope it helps

chester89
This is an interesting solution. What happens though if you have a url that has and action and controller that is identical between two languages?
Dan Atkinson
thanks a lot. nice solution.
Hamid