本地化ASP.NET MVC 4使用App_GlobalResources文件 [英] Localization in ASP.NET MVC 4 using App_GlobalResources

查看:2208
本文介绍了本地化ASP.NET MVC 4使用App_GlobalResources文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图完成两件事情:


  1. 本地化内置错误FieldMustBeDate和FieldMustBeNumeric的消息

  2. 本地化一些,你会遇到其他错误信息,例如PropertyValueRequired。

使用 http://forums.asp.net/t/1862672.aspx/1 针对问题1和<一个href=\"http://stackoverflow.com/questions/12545176/mvc-4-ignores-defaultmodelbinder-resourceclasskey/12545997#12545997\">MVC 4忽略了问题2我设法得到当地两个工作DefaultModelBinder.ResourceClassKey 。

但是当我发布到网站上,内置的错误消息默认回英文,而其他错误消息留本地化。

However as soon as I publish to a website, the "built-in" error messages defaults back to English while the other error messages stay localized.

我已阅读,使用App_GlobalResources文件应避免几个地方,但我无法完成的问题1不使用此。

I have read several places that using the App_GlobalResources should be avoided, however I am unable to accomplish problem 1 without using this.

我创建了一个.resx文件名为WebResources.resx,设置生成操作为嵌入复制设置输出目录不复制,设置自定义工具PublicResXFile codeGenerator,并设置自定义工具命名空间为资源。
本身被设置为仅计划发布所需的文件。

I have created a .resx file with the name "WebResources.resx", set the Build Action to "Embedded", set the Copy to Output Directory to "Do no Copy", set the Custom Tool to "PublicResXFileCodeGenerator" and set the Custom Tool Namespace to "Resources". The Project itself is set to only Publish files that are needed.

我的Global.asax.cs包含以下(相关)code:

My Global.asax.cs contains the following (relevant) code:

  ClientDataTypeModelValidatorProvider.ResourceClassKey = "WebResources";  
  DataAnnotationsModelValidatorProvider.RegisterAdapter(
  typeof(RequiredAttribute),
  typeof(MyRequiredAttributeAdapter));

和类MyRequiredAttributeAdapter包含以下code:

And the class MyRequiredAttributeAdapter contains the following code:

public class MyRequiredAttributeAdapter : RequiredAttributeAdapter
{
    public MyRequiredAttributeAdapter(
        ModelMetadata metadata,
        ControllerContext context,
        RequiredAttribute attribute
    )
        : base(metadata, context, attribute)
    {
        if (attribute.ErrorMessageResourceType == null)
        {
            attribute.ErrorMessageResourceType = typeof(Resources.WebResources);
        }
        if (attribute.ErrorMessageResourceName == null)
        {
            attribute.ErrorMessageResourceName = "PropertyValueRequired";
        }
    }
}

这是在本地工作却没有任何人对如何获得内置消息的任何想法来解决这个公布后?

This is working locally however does anyone have any idea on how to get the "built in" messages to work after this is published?

感谢您的帮助!

最好的问候,
安德烈亚斯

Best regards, Andreas

推荐答案

我想通了这出自己。如果您正试图实现以上必须分开本地化错误消息。

I figured this one out myself. If you are trying to accomplish the above you must separate the localized error messages.

创建其他错误信息FXPropertyValueRequired一个* .resx文件,并设置生成操作为嵌入,设置复制到输出目录不复制,设置自定义工具PublicResXFile codeGenerator,并设置自定义工具命名空间为资源。

Create a *.resx file for the other error messages fx "PropertyValueRequired" and set the Build Action to "Embedded", set the Copy to Output Directory to "Do no Copy", set the Custom Tool to "PublicResXFileCodeGenerator" and set the Custom Tool Namespace to "Resources".

在我来说,我已经感动PropertyValueRequired到一个叫LocalDanish.resx(依然在App_GlobalResources文件夹)文件和

In my case I have moved "PropertyValueRequired" to a file called LocalDanish.resx (still in the App_GlobalResources folder) and changed the line in my "MyRequiredAttributeAdapter" from

attribute.ErrorMessageResourceType = typeof(Resources.WebResources);

attribute.ErrorMessageResourceType = typeof(Resources.LocalDanish);

为了获得建的错误消息,您必须创建两个* .resx文件。我创建WebResources.resx和WebResources.da.resx。不要改变什么,他们留在默认设置(建设行动内容等)。我猜是因为我在我的WebConfig设置全球化的网站自动在我的情况下,* .da.resx文件:

In order to get the "built in" error messages to work, you must create two *.resx files. I have created WebResources.resx and WebResources.da.resx. Do NOT change anything, leave the settings on them on default (Build Action to "Content", etc.). I guess the website automatically looks for the *.da.resx files in my case because I have set the globalization in my WebConfig:

<globalization uiCulture="da-DK" culture="da-DK"/>

希望这可以帮助任何人。

Hope this helps anybody.

最好的问候,
安德烈亚斯

Best regards, Andreas

这篇关于本地化ASP.NET MVC 4使用App_GlobalResources文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆