【问题标题】:get current culture or browser locale on mvc 4在 mvc 4 上获取当前文化或浏览器区域设置
【发布时间】:2013-06-05 18:17:39
【问题描述】:

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

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

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

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

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

    【讨论】:

    • 该数组是因为浏览器可以发送多个项目,特别是当用户配置了备用语言时,参考这里:stackoverflow.com/questions/8552927/…
    • 是的,完全正确。这就是UserLanguages 属性返回字符串数组的原因。
    • 你真的可以拯救所有机械师的光顾的东西,也许教他如何正确地提出问题?建设性的反馈总是更好。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2011-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      相关资源
      最近更新 更多