Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I set the language (CurrentThread.CurrentCulture)?

In older asp.net - projects we used to set the language usually within the Application_BeginRequest - Handler (Global.asax), something like this:

System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cookie.Lang)

Now I am switching to MVC 2 and decided to keep the language as a fix route within the URL. The URL looks like this: {lang}/{controller}/{action}

How and where should I read the language from the URL and set the CurrentCulture? How is it best done the MVC - way?

Thx for any tipps!

like image 940
sl3dg3 Avatar asked Feb 08 '11 17:02

sl3dg3


1 Answers

Something like this in global.asax should work

protected void Application_AcquireRequestState(object sender, EventArgs e)
{
    MvcHandler handler = Context.Handler as MvcHandler;
    if (handler == null)
        return;

    string lang = handler.RequestContext.RouteData.Values["lang"] as string;

    CultureInfo culture = CultureInfo.GetCultureInfo(lang);

    Thread.CurrentThread.CurrentUICulture = culture;
    Thread.CurrentThread.CurrentCulture = culture;
}
like image 173
Lukáš Novotný Avatar answered Nov 20 '22 10:11

Lukáš Novotný