3

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

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

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

4

2 回答 2

13

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

我只是喜欢它不起作用的问题描述!!!这就像对一个你不想为这份工作付钱的机械师说:我的车坏了,告诉我它出了什么问题,这样我就可以自己修理它,当然不用向他展示你的车。

无论如何,您的控制器操作中仍然有 HttpRequest。看UserLanguages楼盘:

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

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

于 2013-06-10T07:50:39.970 回答
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);
    }
于 2016-03-24T11:52:07.450 回答