MVC 读配置文件 多语言

配置文件:

  <appSettings>
    <add key="Language" value="sv"/>
  </appSettings>

 

在路由里面加入一下代码:

protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            var langName = WebConfigurationManager.AppSettings["Language"];

            if (string.IsNullOrEmpty(langName))
            {
                if (HttpContext.Current.Request.UserLanguages != null && HttpContext.Current.Request.UserLanguages.Length != 0)
                {
                    langName = HttpContext.Current.Request.UserLanguages[0];
                }
            }

            var cultureInfo = new CultureInfo(langName);

            System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;
            System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo;
            System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);
            System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);
        }

posted @ 2012-11-15 10:48  BeierWu  阅读(172)  评论(0编辑  收藏  举报