views:

65

answers:

2

Hi,

I have a site that supports localization. I would like to be able to switch between english and french.

Let say the user is currently at URL: http://www.mysite.com/en/Home

I would like to redirect to: http://www.mysite.com/fr/Home

If the user click on a "French" link how to change the URL part to "fr" yet not change the "Home" part of the URL (basically I want preserve the current location of the user)

Hope my question makes sense! I'm probably missing something very basic?

EDIT: Kind of found a solution.

<%= Html.ActionLink("Français", ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), new { culture = "fr" }, null)%>
<%= Html.ActionLink("English", ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), new { culture = "en" }, null)%>

This maintains the action/controller of the current URL. Maybe there's a cleaner solution?

A: 

I would suggest making the urls in this format:

http://www.mysite.com/Home/en

Then you can make actions as simple as this:

<%= Html.ActionLink("Francais", "Home", "MyController", new { id = "fr" }), null %>
<%= Html.ActionLink("English", "Home", "MyController", new { id = "en" }), null %>

Then in your controller have an action:

public ActionResult Home(string id)
{
    if(id == "en"){ // do something } 
    else if(id == "fr") { // do something else }
    return View();
}

Global.asax Route

routes.MapRoute(
   "HomeLanguageRoute", // Route name
   "MyController/Home/{id}", // URL with parameters
   new { controller = "MyController", action = "Home", id = "" }    // Parameter defaults
);
gmcalab
I think its better to keep the language in the beginning. Lets say you have deeper links like: /Catalogue/en/Electronics/Radio_a. This doesnt look nice. There should be a new Parameter in the routing for the language.
Malcolm Frexner
+1  A: 

In your Global.asax

 routes.MapRoute(
               "LocalizedRoute",                                              // Route name
               "{language}/{controller}/{action}/{id}",                           // URL with parameters
               new { language= "fr", controller = "Home", action = "Index", id = string.Empty }, // Parameter defaults
               new { language= @"[a-z]{2}" });    // Constraints       

And you can have access to the language with the variable language in your controller

To generate a link:

<%= Html.ActionLink("French", ViewContext.RouteData.Values["action"], new { language = "fr" }) %>

And you can make a base class controller with this property:

public string Language { get { return this.Routedata["language"]; } }
Gregoire
this is good but what if they are not currently in "Home" but want to change languages? They could be anywhere on the site when they decide to change languages so I don't want to force them back to "Home"
vidalsasoon
having the catchall route using the language parameter will make it work anywhere your user is when changing language. And you should of course have the language parameter on all your route with a default value.
Stephane
@vidalsasoon: Home and index are just default values, so this route is valable for all other actions
Gregoire
I think there's a misunderstanding. <%= Html.ActionLink("French", "Home", new { language = "fr" }) %> takes me to the "Home" page because it's hardcoded. What if the user is currently at "Services" and not "Home"? Clicking that ActionLink would take them away from "Services" which is not good.
vidalsasoon
@vidalsasoon I have updated the link generation in order to match your requirements
Gregoire