简体   繁体   English

在 Asp.net 内核中本地化 DataAnnotations

[英]Localize DataAnnotations in Asp.net core

In my asp.net core (.net5) application I have a form with a required field.在我的 asp.net 核心 (.net5) 应用程序中,我有一个带有必填字段的表单。 When the field is empty I have当该字段为空时,我有

在此处输入图像描述

"The XX field is required"... In English... I want to translate it in French. “XX 字段是必填项”……英文……我想翻译成法语。 I mean, I don't really want to translate , I want to use the French version of the message.我的意思是,我真的不想翻译,我想使用法语版本的消息。 I don't want to add Resource files, because I have any custom strings to translate, I just want to use existing messages, but in French.我不想添加资源文件,因为我有任何自定义字符串要翻译,我只想使用现有的消息,但使用法语。

I started to read here but did't really get the point if the article really proposes me to translate each message manually by myself.我开始在这里阅读,但如果这篇文章真的建议我自己手动翻译每条消息,我并没有真正明白这一点。

I added this one in the Configure我在配置中添加了这个

var supportedCultures = new[] { "fr-FR" };
var localizationOptions = new RequestLocalizationOptions().SetDefaultCulture(supportedCultures[0])
    .AddSupportedCultures(supportedCultures)
    .AddSupportedUICultures(supportedCultures);

app.UseRequestLocalization(localizationOptions);

however this didn't change the message...但这并没有改变信息......

nor setting the culture params via URL, like this也不通过 URL 设置文化参数,像这样

在此处输入图像描述

If you just want one french version, you just need to define the error message in [Required]如果你只想要一个法语版本,你只需要在 [Required] 中定义错误信息

attribute, like:属性,例如:

public class MyModel
{
    [Required(ErrorMessage = "Le champ Nom est obligatoire")]
    public string Nom { get; set; }
}

And if your shared code refers to the documention you shared, you need to add Resource file to如果您的共享代码引用了您共享的文档,则需要将资源文件添加到

define the translated message, or the message will not changed.定义翻译的消息,否则消息不会改变。

Edit:编辑:

If you have other properties, you can define a customRequired attribute:如果你有其他属性,你可以定义一个 customRequired 属性:

 public class CustomRequiredAttribute : ValidationAttribute
{
    public CustomRequiredAttribute()
    {         
    }
    
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (value == null)
        {
            ErrorMessage = $"Le champ {validationContext.DisplayName} est obligatoire";
            return new ValidationResult(ErrorMessage);
        }
        return ValidationResult.Success;
    }
}

Model: Model:

public class MyModel
{   
    [CustomRequired]  //custom attribute
    public string Nom { get; set; }        
    [CustomRequired]
    public string PreNom { get; set; }
}

To use your custom attribute, you should add ModelState.IsValid in your post action, so if invalid, it will return the input view to show the errorMessages,like:要使用您的自定义属性,您应该在您的 post 操作中添加ModelState.IsValid ,因此如果无效,它将返回输入视图以显示错误消息,例如:

[HttpPost]
    public IActionResult Privacy(MyModel myModel)
    {
        if (!ModelState.IsValid)
            return View("Index");
        return View();
    }

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM