Search code examples
asp.net-mvcasp.net-mvc-4internationalizationlocale

get current culture or browser locale on mvc 4


How do you get current culture or browser locale on MVC 4.

I find some samples which gets it from HttpContext and HttpRequest but this doesnt work on MVC 4.

How do you do it on MVC 4?


Solution

  • I find some samples which gets it from HttpContext and HttpRequest but this doesnt work on MVC 4.

    I just love the it doesn't work problem description!!! It's like saying to a mechanic whom you don't want to pay for the job: my car doesn't work, tell me what is wrong with it so that I can fix it myself, without showing him your car of course.

    Anyway, you still got the HttpRequest in your controller action. Look at the UserLanguages property:

    public ActionResult SomeAction()
    {
        string[] userLanguages = Request.UserLanguages;
        ...
    }
    

    Remark: the value of this property will be null if the user agent didn't send the Accept-Languages request header. So make sure you check whether it is not null before accessing its value to avoid getting NREs.