Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC switch language, how to implement?

Tags:

I have started converting my simple website to ASP.NET MVC, just to mess around with it. I have a switch language feature on there that basically sets the Session["language"] to another language and refreshes the page. Please correct me if this could be done better, but I have made two controllers for this and setting the session in there. The problem is the routing at the end. Could I refresh the page in some neat way, or can I get the current Action and re-route it to that? Or is this more a scenario for Ajax?

Thankful for advice!

like image 786
miccet Avatar asked May 29 '09 20:05

miccet


People also ask

How do I change the language of a resource file in ASP.NET MVC?

Now go to the browser and change the language for Tool -> Internet Options. Click the Add button. Select the language depending on your resource file language and click ok. Run you application and click the submit button.

How MVC is implemented in ASP NET?

ASP.NET MVC 2 Empty Web Application templateCreate only the files and folders required by every ASP.NET MVC application. Visual Studio provides us various templates for creating web applications. Here we will be creating a controller using the ASP.NET MVC 2 application template.


2 Answers

is there any reason why you are using the session variable? a more common solution is to include the language code in the route, i.e. blah.com/en/info or blah.com/jp/info (for english and japanese)

if you did this every page on the site could contain links to each language. if you are writing a publicly accessible site this would also make it easier for google to index all your content.

this article explains how to include the language in the domain, ie. en.blah.com or jp.blah.com: http://blog.maartenballiauw.be/post/2009/05/20/ASPNET-MVC-Domain-Routing.aspx

UPDATED: Here's a simple example of including the language code in the URL route.

Change the default route to include a language parameter:

routes.MapRoute( "Default",  "{language}/{controller}/{action}/{id}",  new { language = "en", controller = "Home", action = "Index", id = "" } ); 

Add links for each language to your masterpage:

<li><%= Html.ActionLink(     "Spanish",      ViewContext.RouteData.Values["action"].ToString(),      new { language = "es" })%></li> <li><%= Html.ActionLink(     "French",      ViewContext.RouteData.Values["action"].ToString(),      new { language = "fr" })%></li> <li><%= Html.ActionLink(     "English",      ViewContext.RouteData.Values["action"].ToString(),      new { language = "en" })%></li>     

These will render as links back to the page you are on - only with the language changed.

like image 192
russau Avatar answered Oct 15 '22 03:10

russau


Following approach works good for me:

I'm using cookies and my own engine for localization All you need to put some link on the page that will redirect to something like this:

public class LanguageController : Controller {     //     // GET: /Language/      public void Change(string id)     {         var cuka = new HttpCookie("lang", id + "");         cuka.Expires = DateTime.Now.AddYears(10);         System.Web.HttpContext.Current.Response.Cookies.Add(cuka);          if (Request.UrlReferrer.IsNotNull())             Response.Redirect(Request.UrlReferrer.AbsoluteUri);         else             Response.Redirect("/");     }  } 

}

If you're interested in this engine, let me know.

like image 29
omoto Avatar answered Oct 15 '22 02:10

omoto