asp.net mvc - Make URL language-specific (via routes) -
asp.net mvc - Make URL language-specific (via routes) -
its hard find reply specific question, sick pop here:
we want build our urls language specific, meaning
www.mysite.com/en
www.mysite.com/de
this done in routeconfig
url: "{language}/{controller}/{action}/{id}"
but tricky part:
www.mysite.com/en/categories
www.mysite.com/de/kategorien
i.e. create controllername appear in multiple languages, point same controller. possible?
well, partially possible (the language part only). controller name in different language interesting point, think hard accomplish that. think bidi languages standard arabic , hebrew. thought utilize controller in different languages create havoc , believe have alter underlying mvc construction allow this.
the language changing part easy , done below.
what might want @ globalization. language part corresponds current threads ui culture. need following:
define route, this:
var lang = system.threading.thread.currentthread.currentuiculture.name; routes.maproute( name: "default", url: "{language}/{controller}/{action}/{id}", defaults: new { language = lang, controller = "home", action = "index", id = urlparameter.optional } );
register application_acquirerequeststate
, define this:
protected void application_acquirerequeststate() { var routes = routetable.routes; var httpcontext = request.requestcontext.httpcontext; if (httpcontext == null) return; var routedata = routes.getroutedata(httpcontext); var language = routedata.values["language"] string; var cultureinfo = new cultureinfo(language); system.threading.thread.currentthread.currentculture = cultureinfo; system.threading.thread.currentthread.currentuiculture = cultureinfo; }
although currentuiculture
need switch languages load info resource file, should alter currentculture
same cultureinfo
;
last, create sure have corresponding resource files , fallback resource file.
i used name
property of cultureinfo
high german de-de, english language en-us, etc. should trick.
in case need more info upload sample mvc project examine.
update: 1 rough way of doing want invert order of route segments this:
routes.maproute( name: "newdefault", url: "{language}/{id}/{action}/{controller}", defaults: new { language = lang, controller = "home", action = "index", id = "category"} );
this way create next request http://www.your_url.com/de/kategorien. in case kategorien maps id
rather controller. controller stays in english language or high german (depending how named it) user sees different language. in background view might this:
public actionresult index(string id, string categoryname) { // fetch info based on category name (categoryname) homecoming view(); }
you pass additional info parameters, need adjust route like: {language}/{category}/{subcategory}/{action}/{controller}
just know can become pain in neck in long run , if seek this, create sure document well.
asp.net-mvc localization asp.net-mvc-routing globalization
Comments
Post a Comment