DarthVader
DarthVader

Reputation: 55022

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?

Upvotes: 3

Views: 10603

Answers (2)

Julian
Julian

Reputation: 36710

We use the following code in NuGetGallery

 /// <summary>
/// Extensions on <see cref="HttpRequest"/> and <see cref="HttpRequestBase"/>
/// </summary>
public static class HttpRequestExtensions
{
    /// <summary>
    /// Retrieve culture of client. 
    /// </summary>
    /// <param name="request">Current request.</param>
    /// <returns><c>null</c> if not to be determined.</returns>
    public static CultureInfo DetermineClientCulture(this HttpRequest request)
    {
        return DetermineClientCulture(new HttpRequestWrapper(request));
    }

    /// <summary>
    /// Retrieve culture of client. 
    /// </summary>
    /// <param name="request">Current request.</param>
    /// <returns><c>null</c> if not to be determined.</returns>
    public static CultureInfo DetermineClientCulture(this HttpRequestBase request)
    {
        if (request == null)
        {
            return null;
        }

        string[] languages = request.UserLanguages;
        if (languages == null)
        {
            return null;
        }

        //first try parse of full langcodes. Stop with first success.
        foreach (string language in languages)
        {
            string lang = language.ToLowerInvariant().Trim();
            try
            {
                return CultureInfo.GetCultureInfo(lang);
            }
            catch (CultureNotFoundException)
            {
            }
        }

        //try parse again with first 2 chars.  Stop with first success.
        foreach (string language in languages)
        {
            string lang = language.ToLowerInvariant().Trim();
            if (lang.Length > 2)
            {
                string lang2 = lang.Substring(0, 2);
                try
                {
                    return CultureInfo.GetCultureInfo(lang2);
                }
                catch (CultureNotFoundException)
                {
                }
            }
        }

        return null;
    }
}

usage:

    /// <summary>
    /// Called before the action method is invoked.
    /// </summary>
    /// <param name="filterContext">Information about the current request and action.</param>
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (!filterContext.IsChildAction)
        {
            //no need to do the hassle for a child action
            //set the culture from the request headers
            var clientCulture = Request.DetermineClientCulture();
            if (clientCulture != null)
            {
                //and/or CurrentUICulture 
                Thread.CurrentThread.CurrentCulture = clientCulture;
            }
        }

        base.OnActionExecuting(filterContext);
    }

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

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.

Upvotes: 13

Related Questions