【问题标题】:Redirect to unique view depending on client locale根据客户端区域设置重定向到唯一视图
【发布时间】:2014-02-18 11:39:39
【问题描述】:

我正在向 C# MVC 5 Web 应用程序添加全球化。 在大多数视图中,我使用 Resources 并且效果很好。对于具有大量自定义的视图,我想将视图分开,每个视图用于不同的语言。 我关注了 Brian Reiter post。我在 Views 下添加 Globalization forlder、ISO 639-1 两字母宏语言代码,以及特定语言的 Home 文件夹和 Index 视图。

我了解我需要修改呈现视图的机制以考虑客户的语言环境。在 Brian 的帖子中,它在 Web 表单上进行了演示,在我的解决方案中,我似乎没有与他的示例中相同的 WebFormViewEngine。

如果您能指导我如何扩展 mvc 视图引擎,以便根据语言环境呈现正确的视图,我将不胜感激。

谢谢。

【问题讨论】:

  • 博客中的示例与 MVC 相关,与 Web 表单无关。但我想您使用的是 Razor,因此您可以使用与示例中相同的代码来扩展您的 RazorViewEngine。
  • 谢谢@Egor4eg,你是对的。

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


【解决方案1】:

这就是我修复项目的方式:

  1. 添加了一个将扩展 RazorViewEngine 的类(继承自它)。

我使用了 Brian Reiter 帖子(上面的链接)中的代码,稍作更改(从 WebFormsViewEngine 到 RazorViewEngine、我的应用程序文化 cookie 以及从文化中提取两个字母的语言):

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

命名空间 System.Web.Mvc { 公共类 GlobalizationViewEngine : RazorViewEngine { 受保护的覆盖 IView CreatePartialView(ControllerContext controllerContext, string partialPath) { partialPath = GlobalizeViewPath(controllerContext, partialPath); return new 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.
    

** 请注意,全球化视图文件的文件夹 arangemnet 与 Brian 的帖子中的层次结构完全相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 2016-08-24
    • 2023-01-29
    • 2020-02-20
    • 1970-01-01
    • 2016-09-02
    相关资源
    最近更新 更多