i'd like to create localized URL's for my site. They should obviously point to the same controller actions, but I want the first routevalues to -always- be the location/language specification. Is this possible?
http://www.website.com/
en/us
/controller/actionhttp://www.website.com/
en/gb
/controller/action
I understand it can be done by defining {language}
and {location}
in every route, but i'm looking for a slick, non-hacky solution.
To offer a website in multiple languages using ASP.Net we simply need to add some resource. resx files to our project and voilà. Based on the language of the browser, IIS will match the localization resource.
App localization involves the following: Make the app's content localizable. Provide localized resources for the languages and cultures you support. Implement a strategy to select the language/culture for each request.
Configure a Route Every MVC application must configure (register) at least one route configured by the MVC framework by default. You can register a route in RouteConfig class, which is in RouteConfig. cs under App_Start folder.
You can create a route that has the culture built into it like this...
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{culture}/{controller}/{action}/{id}", // URL with parameters new { culture="en-US", controller = "Home", action = "Index", id = "" } // Parameter defaults ); }
You can get the culture by adding a culture parameter to all your actions like this...
public ActionResult Index(string culture) { ViewData["Message"] = "Welcome to ASP.NET MVC! (" + culture + ")"; return View(); }
You can also probably parse the URL in the Application_BeginRequest method in Global.asax and set the threads culture there (code sample below shows how to set the culture, the parsing I leave to you).
If you do this you will probably not be able to use the RedirectToAction and HTML.ActionLink type of methods since those don't know anything about cultures. Of course you could always write your own.
The downside to using the url to store the culture is that if you miss a link somewhere on your website or the user leaves the website and then comes back, you could lose the users culture and they will have to set it again (not the end of the world, but annoying. Possibly a good side of using the url to store the culture is that Google will index all the different languages.
If you are more concerned about user experience or ease of development over Google indexing different cultures (really depends on what kind of site you are building), I would suggest storing the culture in a cookie or session state.
Check out How to localize ASP .Net MVC application?. The accepted answer points to a blog post that shows how you can localize an ASP.Net application.
If you store the culture the user selects in a cookie, session state, or query parameter and then set the threads culture in the BeginRequest method in the Global.asax file. Then localization is done using the standard Microsoft localization assemblies.
The following code will allow you to change the culture at any time by simply adding culture=?? to the query string (MyPage?culture=es-MX). It will then be added to a cookie so that you don't need to add it to the end of every link in your system.
protected void Application_BeginRequest() { var culture = Request["culture"] ?? Request.Cookies["culture"]?.Name; if (culture == null) culture = "en-US"; var ci = CultureInfo.GetCultureInfo(culture); Thread.CurrentThread.CurrentCulture = ci; Thread.CurrentThread.CurrentUICulture = ci; var cookie = new HttpCookie("culture", ci.Name); Response.Cookies.Add(cookie); }
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