asp.net-mvc ASP.NET MVC 本地化最佳实践?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/381070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:13:21  来源:igfitidea点击:

ASP.NET MVC localization best practice?

asp.net-mvclocalization

提问by Amr Elsehemy

I need help with the best practice to localize asp mvc apps, I saw Oxite having a base method named Localize in the BaseController, but is the Localization a task for the view or the Controller? Or should I use resx files / or use db tables?

我需要有关本地化 asp mvc 应用程序的最佳实践的帮助,我看到 Oxite 在 BaseController 中有一个名为 Localize 的基本方法,但是本地化是视图还是控制器的任务?或者我应该使用 resx 文件/还是使用 db 表?

采纳答案by Ihar Voitka

Create your own Html helper and use it like <%= Html.Resource("Name") %>

创建您自己的 Html 助手并像使用它一样使用它 <%= Html.Resource("Name") %>

Details are in blogpost.

详细信息在博客文章中。

回答by Michael Vald

There is good solution for this available here

没有针对此提供很好的解决方案在这里

this article covers all aspects of localization asp.net mvc app

这篇文章涵盖了本地化asp.net mvc app的方方面面

回答by kbvishnu

Since this a one year question and I don't know the scope of my answer over here. Recently I faced a situation like this , ie I need to implement the localization for different languages in my mvc site.

由于这是一个为期一年的问题,我不知道我在这里回答的范围。最近我遇到了这样的情况,即我需要在我的 mvc 站点中实现不同语言的本地化。

I considered using Resourcefile. its very easy to implement, but the issue is that during the phase of development, we need to specify the localized strings. So if it a multi language support, we need to make the resource file for every language. If client what to change or add a new language, its very difficult and we need to provide a build.

我考虑过使用Resource文件。它很容易实现,但问题是在开发阶段,我们需要指定本地化的字符串。所以如果是多语言支持,我们需要为每种语言制作资源文件。如果客户要更改或添加新语言,这非常困难,我们需要提供构建。

Second I consider the Satelite Assemblies. Its also similar to Resource, but it gives a freedom to edit the assemblies out side and put it back to bin folder. This also requires a lot of effort to client/developer.

其次,我认为Satelite Assemblies. 它也类似于 Resource,但它可以自由地编辑组件并将其放回 bin 文件夹。这也需要客户/开发人员付出很多努力。

Third I considered storing in db. This approach is fine and we have some mechanism to read data from the server. This requires one time effort and client doesn't have any dependable .

第三,我考虑存储在 db 中。这种方法很好,我们有一些机制可以从服务器读取数据。这需要一次努力,客户没有任何可靠的 .

I override a custom DisplayNameAttributreand from the constructor I will pass DB and get the data to render

我覆盖了一个自定义,DisplayNameAttributre并从构造函数我将传递 DB 并获取要呈现的数据

Based on your requirement it should display the view to you.

根据您的要求,它应该向您显示视图。

Resource Manager

资源管理器

/// <summary>
    ///  Extended display attribute which will handles the request
    ///  It will call every time when the property is rendered (return View() - from controller)
    /// </summary>
    public class ResourceManagerAttribute : DisplayNameAttribute
    {
        public ResourceManagerAttribute(string resourceKey, string resourceNameSpace = "")
            : base(GetDisplayName(resourceKey, resourceNameSpace))
        { }

        private static string GetDisplayName(string resourceKey, string resourceNameSpace = "")
        {
            // get the browser's prefered language.

            string browserLanguage = HttpContext.Current.Request.UserLanguages.First();

            // Get the locale data for that property and displays.
            switch (browserLanguage)
            {
                case "en-US": return "Eng " + resourceKey;
             // calls db based on resource key
                case "hi": return "Hin " + resourceKey;

            }
            return "-- Not Implemented Now -- ";
        }


ViewModel

视图模型

public class HomeViewModel
    {
        //calls the resource
        [ResourceManager("MID")]
        public int MID { get; set; }
        [ResourceManager("Name")]
        public string Name { get; set; }
        [ResourceManager("Addess")]
        public string Addess { get; set; }
    }

回答by Vlad

MVC is really more about using the right view for the right job. Putting everything in a resource file is extremely paintfull. It's good to use resource files for small things, but for larger pages like your description pages, its better to have a view in each culture with a lot of content. For example using the following structure: ~/Views/en-US/Home/Index.aspx ~/Views/pt-BR/Home/Index.aspx or this structure: ~/Views/Home/Index.en-US.aspx ~/Views/Home/Index.en-US.aspx

MVC 实际上更多的是关于为正确的工作使用正确的视图。把所有的东西都放在一个资源文件中是非常完美的。对于小东西使用资源文件是好的,但是对于像描述页面这样的较大页面,最好在每种文化中都有一个包含大量内容的视图。例如使用以下结构:~/Views/en-US/Home/Index.aspx ~/Views/pt-BR/Home/Index.aspx 或此结构:~/Views/Home/Index.en-US.aspx ~/Views/Home/Index.en-US.aspx

read the blog for how to do it: http://blog.oimae.com/2011/02/20/cultured-view-engine-for-mvc/

阅读博客了解如何操作:http: //blog.oimae.com/2011/02/20/cultured-view-engine-for-mvc/

回答by Paul Lalonde

If the string to be localized is generated by the view (eg a label in front of a text field), then its localization should be in the View.

如果要本地化的字符串是由视图生成的(例如文本字段前面的标签),那么它的本地化应该在视图中。

If the string is generated by the Controller, its localization should be there as well.

如果字符串是由控制器生成的,它的本地化也应该在那里。

回答by Aridane álamo

I would better go for creating a custom MetadataProvider and using a convention for the models. Something like 1 resource file by model namespace and a convention like ModelName.PropertyName -> value

我最好去创建一个自定义的 MetadataProvider 并使用模型的约定。类似于模型命名空间的 1 个资源文件和 ModelName.PropertyName -> value 之类的约定

For validators, common buttons and so a resource file.

对于验证器、常用按钮等资源文件。

For views text i am actually trying to find a good way. Maybe a view pre-process before compilation and a custom Scope for localized texts, so the pre-process can create the resource file for each view with the default language.

对于视图文本,我实际上是在尝试找到一种好方法。也许编译前的视图预处理和本地化文本的自定义范围,因此预处理可以使用默认语言为每个视图创建资源文件。