Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 407
DarthVader Avatar asked Nov 30 '25 19:11

DarthVader


1 Answers

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.

like image 150
Darin Dimitrov Avatar answered Dec 03 '25 10:12

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!