ASP。NET MVC本地化的最佳实践?

时间:2022-09-18 08:10:19

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中有一个名为本地化的基本方法,但是本地化是视图还是控制器的任务?还是应该使用resx文件/或db表?

6 个解决方案

#1


14  

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

创建自己的Html助手,并像使用<%= Html. resource(“Name”)%>一样使用它

Details are in blog post.

详情在博客中。

#2


10  

There is good solution for this available here

这里有很好的解决方案

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

本文涵盖了本地化asp.net mvc应用程序的所有方面

#3


4  

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 Resource file. 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.

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

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.

第二,我考虑的是圣公会。它也类似于资源,但它提供了编辑程序集的*,并把它放回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 DisplayNameAttributre and 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; }
    }

#4


3  

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 ~ /视图/ pt-BR / Home /索引。aspx或此结构:~/ view /Home/Index.en-US。aspx ~ / / 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/

#5


0  

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.

如果字符串是由控制器生成的,那么它的定位也应该在那里。

#6


0  

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

我最好是创建自定义的元数据提供程序,并使用模型的约定。类似于按模型名称空间划分的一个资源文件,以及类似于ModelName的约定。PropertyName - >价值

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.

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

#1


14  

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

创建自己的Html助手,并像使用<%= Html. resource(“Name”)%>一样使用它

Details are in blog post.

详情在博客中。

#2


10  

There is good solution for this available here

这里有很好的解决方案

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

本文涵盖了本地化asp.net mvc应用程序的所有方面

#3


4  

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 Resource file. 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.

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

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.

第二,我考虑的是圣公会。它也类似于资源,但它提供了编辑程序集的*,并把它放回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 DisplayNameAttributre and 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; }
    }

#4


3  

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 ~ /视图/ pt-BR / Home /索引。aspx或此结构:~/ view /Home/Index.en-US。aspx ~ / / 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/

#5


0  

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.

如果字符串是由控制器生成的,那么它的定位也应该在那里。

#6


0  

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

我最好是创建自定义的元数据提供程序,并使用模型的约定。类似于按模型名称空间划分的一个资源文件,以及类似于ModelName的约定。PropertyName - >价值

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.

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