根据客户端区域设置重定向到唯一视图

时间:2014-02-18 11:39:40

标签: c# asp.net-mvc-4 internationalization asp.net-mvc-5 globalization

我正在将Globalization添加到C#MVC 5 Web应用程序中。 在大多数视图中,我使用的是资源,它运行良好。对于具有大量自定义的视图,我想分隔视图,每个视图用于不同的语言。 我跟着Brian Reiter post。我在Views下添加了Globalization forlder,ISO 639-1双字母宏语言代码,以及特定语言的Home文件夹和Index视图。

我知道我需要修改渲染视图以考虑客户端语言环境的机制。在Brian的帖子中,它在Web表单上进行了演示,在我的解决方案中,我似乎没有像他的示例中那样的WebFormViewEngine。

如果您可以指导我如何使用mvc视图引擎以便根据区域设置呈现正确的视图,我会恭维。

感谢。

1 个答案:

答案 0 :(得分:0)

这是我修复项目的方法:

  1. 添加了一个将扩展RazorViewEngine的类(继承它)。
  2. 我使用Brian Reiter帖子(上面的链接)中的代码略有变化(从WebFormsViewEngine到RazorViewEngine,我的应用程序文化cookie以及从文化中提取双字母语言):

    使用System; 使用System.Collections.Generic; 使用System.Linq; 使用System.Web; 使用System.Web.Mvc; 使用System.Text.RegularExpressions; 使用System.IO; 使用LeadsWize.Helpers; 使用System.Globalization;

    命名空间System.Web.Mvc {     公共类GlobalizationViewEngine:RazorViewEngine     {         protected override IView CreatePartialView(ControllerContext controllerContext,string partialPath)         {             partialPath = GlobalizeViewPath(controllerContext,partialPath);             返回新的RazorView(controllerContext,partialPath,null,false,FileExtensions,ViewPageActivator);         }

        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            viewPath = GlobalizeViewPath(controllerContext, viewPath);
            return base.CreateView(controllerContext, viewPath, masterPath);
        }
    
        private static string GlobalizeViewPath(ControllerContext controllerContext, string viewPath)
        {
            var request = controllerContext.HttpContext.Request;
            string cultureName = null;
            HttpCookie cultureCookie = request.Cookies["_culture"];
            if (cultureCookie != null)
                cultureName = cultureCookie.Value;
            else
                cultureName = request.UserLanguages != null && request.UserLanguages.Length > 0 ?
                        request.UserLanguages[0] :  // obtain it from HTTP header AcceptLanguages
                        null;
            // Validate culture name
            cultureName = CultureHelper.GetImplementedCulture(cultureName); // This is safe
            var lang = (CultureInfo.CreateSpecificCulture(cultureName)).TwoLetterISOLanguageName; // this is to extract the two languge letters from the culture
            if (lang != null &&
                !string.IsNullOrEmpty(lang) &&
                !string.Equals(lang, "en", StringComparison.InvariantCultureIgnoreCase))
            {
                string localizedViewPath = Regex.Replace(
                    viewPath,
                    "^~/Views/",
                    string.Format("~/Views/Globalization/{0}/",
                    lang
                    ));
                if (File.Exists(request.MapPath(localizedViewPath)))
                { viewPath = localizedViewPath; }
            }
            return viewPath;
        }
    }
    

    }

    1. 在Global.asxs.cs

      中的Application_Stat中添加了两行
          ViewEngines.Engines.Clear();
          ViewEngines.Engines.Add(new GlobalizationViewEngine()); //this is our customized extended view engine to support the globaliztion in view folder Globalization.
      
    2. **请注意全球化视图文件的文件夹arangemnet与Brian的帖子完全在同一层次结构中。