隐式本地化"Required"注释(法语) [英] Localize `Required` annotation in French implicitly

查看:93
本文介绍了隐式本地化"Required"注释(法语)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TLDR; 如何获得

TLDR; How to get the behaviour of

[Required(ErrorMessage = "Le champ {0} est obligatoire")]

仅写作

[Required]


据我了解,文档没有提供隐式本地化给定的DataAnnotations集的方法.


As I understand it the documentation does not provide a way to implicitly localize a given set of DataAnnotations.

我想使诸如RequiredStringLength的注释的错误消息是可重写的,而无需触及诸如Display的其他消息,并且不需要使用ErrorMessage属性来明确指定翻译.

I would like to have the error messages for annotations such as Required and StringLength be over-rideable without touching others such as Display and without the need to explicitly specify the translation using the ErrorMessage attribute.

注意:我只需要将消息翻译成法语,因此不需要将解决方案绑定到请求的语言.

note: I only need to have the messages translated in French, so there is no need for the solution to be bound to the request's language.

我尝试了以下操作:

来自 GitHub线程

From this GitHub thread

Startup.cs

services.AddMvc(options => options.ModelBindingMessageProvider.AttemptedValueIsInvalidAccessor =
    (value, name) => $"Hmm, '{value}' is not a valid value for '{name}'."));

给我以下错误

属性或索引器'DefaultModelBindingMessageProvider.AttemptedValueIsInvalidAccessor'不能分配给它-只读的

Property or indexer 'DefaultModelBindingMessageProvider.AttemptedValueIsInvalidAccessor' cannot be assigned to -- it is read only

而且我找不到任何可以用作此对象的设置器的属性.

And I could not find any property that might work as a setter for this object.

来自 SO答案

Startup.cs services.AddSingleton();

In the Startup.cs services.AddSingleton();

并创建一个类似于follow的类

and create a class like follow

public class LocalizedValidationAttributeAdapterProvider : IValidationAttributeAdapterProvider
{
    private readonly ValidationAttributeAdapterProvider _originalProvider = new ValidationAttributeAdapterProvider();

    public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
    {
        /* override message */
    }
}

但这仅捕获了DataType批注

推荐答案

在.Net Core 2中,ModelBindingMessageProvider中的Accessor属性是只读的,但是您仍然可以使用Set...Accessor()方法进行设置.这与我正在使用的代码相似,这要感谢 ASP.NET核心模型绑定错误消息本地化.

In .Net Core 2, the Accessor properties in ModelBindingMessageProvider are read-only, but you can still set them by using the Set...Accessor() methods. Here's code similar to what I'm using, with thanks to the answer to ASP.NET Core Model Binding Error Messages Localization.

public static class ModelBindingConfig
{
    public static void Localize(MvcOptions opts)
    {
        opts.ModelBindingMessageProvider.SetMissingBindRequiredValueAccessor(
            x => string.Format("A value for the '{0}' property was not provided.", x)
        );

        opts.ModelBindingMessageProvider.SetMissingKeyOrValueAccessor(
            () => "A value is required."
        );
    }
}


// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    // ...

    services.AddMvc(
        opts =>
        {
            ModelBindingConfig.Localize(opts);
        })
        .AddViewLocalization()
        .AddDataAnnotationsLocalization();
}

这篇关于隐式本地化"Required"注释(法语)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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