views:

313

answers:

3

In my master page, I have a language dropdown menu. When the user selects a language from the dropdown, a submit sends the currently selected language to the "Translate" method in my controller. After which it should redirect to the url it was before the translation submit so it can show the exact same page, but now in the newly selected language.

How would I best go about this? Should I send the current url somehow in a hidden field? Or can I maybe send the current routevaluedictionary, so my translate method can redirectToRoute directly?

Or maybe there is an entirely better way?

--EDIT--
Because I want my bookmarks to include the site language too, all my exposed actions have a siteLanguage parameter too. If I could somehow efficiently show the user a bunch of regular (GET) links where the siteLanguage parameter is filled in with the relevant value, that would be even better. But as far as I know, there is no way to put links in a dropdown except with java maybe.

A: 
public ActionResult Translate(string _lang){
      switch(_lang){
             case "English":
                   return View("English");
             case: "French":
                   return View("French");
             default:
                   return View("English");
}

I would personally do it like this

Ayo
but what would this view be?I don't have 1 page that should be translated, I have many and their URL varries from case to case.
borisCallens
A: 

I would put the return url in the querystring, just like forms authentication does when it redirects to the login page.

Include the returnUrl in the routeValues when you do your Translate request.

BC
+1  A: 

I have a similar situation, and I solved it slightly differently.

Because my master page had functionality in it, I created a new base controller class (that inherits from Controller) and all my real controllers inherit from my custom class.

Then, I implement OnActionExecuting in the base class to do some common work.

Then, in your masterpage, if you have a form like this, it will submit to the current URL with a GET request and add the language as a querystring parameter:

<form id="language" method="get" >
    <select name="language">
        <option value="en">English</option>
        <option value="es">Spanish</option>
        ...
    </select>
</form>

Use jQuery to wire it up to autosubmit, etc.

You could look for a language parameter in the querystring in your base controller class and set the flag that tells the real controller method which language to use. In the model, you directly go to the real controller to regenerate the page and avoid a redirect.

Note, this only works universally, if you are not already using querystring parameters.

If this doesn't work for you, you could also use your current method, but include the URL to be redirected to in a hidden field like this:

<%= Html.Hidden("redirect", Request.Url %>
Erv Walter
I already have a base custom controller, so I might check this out
borisCallens
I didn't know you could leave the action empty. Great stuff.
borisCallens