MVC Validation message internationalization

前端 未结 2 1949
广开言路
广开言路 2021-01-19 02:57

For example, I would like this default ASP.NET MVC 4 validation message: The value \'qsdqsdqs\' is not valid for Montant to be displayed in french.

I f

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-19 03:44

    Actually using ResX Manager. Visual Studio menu Tools -> Extensions and Updates -> search for "resx" in Online tools.

    With its helpers all my strings are accessed like "Res.SomeTranslatedString". Now with credits to everybody above, let's translate a registration viewmodel message for a boolean property to check if user accepted terms and conditions. With the above tool i have put the string into Res.YouMustAcceptTermsAndConditions. Then we modify the viewmodel code:

    Was:

    public class RegisterViewModel
    {
        [Required]
        [Range(typeof(bool), "true", "true", ErrorMessage = "You must accept terms and conditions.")]
        [Display(Name = "Agree to terms.")]
        public bool AgreeTerms { get; set; }
    

    Became:

    public class RegisterViewModel
    {
        [Required]
        [Range(typeof(bool), "true", "true", ErrorMessageResourceType = typeof(Res), ErrorMessageResourceName = "YouMustAcceptTermsAndConditions")]
        [Display(Name = "Agree to terms.")]
        public bool AgreeTerms { get; set; }
    

    Now you see that we have the [Display] still untranslated.. The solution is here: https://stackoverflow.com/a/3877154/7149454

提交回复
热议问题