在mvc 4上获取当前文化或浏览器区域设置

时间:2013-06-10 06:28:42

标签: asp.net-mvc asp.net-mvc-4 internationalization locale

如何在MVC 4上获得当前文化或浏览器区域设置。

我找到一些从HttpContext和HttpRequest获取它的示例,但这对MVC 4不起作用。

你如何在MVC 4上做到这一点?

2 个答案:

答案 0 :(得分:13)

  

我找到一些从HttpContext和HttpRequest获取它的示例,但这对MVC 4不起作用。

我只是喜欢它不起作用问题描述!!!这就像对一个你不想为这份工作买单的机械师说:我的车无法工作,告诉我它有什么问题,这样我就可以自己修理,而不是给他看当然是你的车。

无论如何,你仍然在你的控制器动作中得到了HttpRequest。查看UserLanguages属性:

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

备注:如果用户代理未发送Accept-Languages请求标头,则此属性的值将为null。因此,请确保在访问其值之前检查它是否为空,以避免获取NRE。

答案 1 :(得分:0)

我们在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;
    }
}

用法:

    /// <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);
    }