4

I have a multilanguage site running on ASP.NET Core 6 MVC.

The data annotation should be based on user language; I can make the site bilingual using sharedResource class.

The issue is how to make the model data annotation error bilingual; currently, I only got the data annotation ErrorMessage.

Program.cs

builder.Services.AddControllersWithViews()
             .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
              //.AddDataAnnotationsLocalization();// <--- for ERROR MSG -----
              .AddDataAnnotationsLocalization(
                 options => {
                     options.DataAnnotationLocalizerProvider = (type, factory) =>
                         factory.Create(typeof(DataAnnotationResource));
                 });// <---------- For ERROR MSG -----

FactoryData Model

public class FactoryData
{
    [Required(ErrorMessage = "General.RequiresMessageOOO")]
    public string NameInAr { get; set; }

    [Required(ErrorMessage = "General.RequiresMessageOOO")]
    [MaxLength(2, ErrorMessage = "General.MaxlengthExceededOOO")]
    public string NameInEn { get; set; }

    [Required]
    [Range(1,3)]
    public string Age { get; set; }
}

This is the localizationResource folder:

enter image description here

The output of this current code

enter image description here

2 Answers 2

3

I ran into the same problem and I have to do these following changes

from

builder.Services.AddControllersWithViews()
    .AddDataAnnotationsLocalization(opt =>
    {
        opt.DataAnnotationLocalizerProvider = (type, factory) => factory.Create(typeof(SharedResource));
    });

to

builder.Services.AddControllersWithViews()
.AddDataAnnotationsLocalization(opts =>
{
    opts.DataAnnotationLocalizerProvider = (type, factory) =>
    {
        var assemblyName = new AssemblyName(typeof(SharedResource).GetTypeInfo().Assembly.FullName!);
        return factory.Create(nameof(SharedResource), assemblyName.Name!);
    };
});
Sign up to request clarification or add additional context in comments.

3 Comments

That's great! That's the only way that worked for me too.
Worked for me out of the box.
Strange, for me it pulls only English resources. I put breakpoint into Designer.cs and it doesn't get hit. Not sure how it works.
1

You can use resources in DataAnnotations this way :

[Required(ErrorMessageResourceName = "General.RequiresMessageOOO", ErrorMessageResourceType = typeof(SharedResource))]
[MaxLength(2, ErrorMessageResourceName = "General.MaxlengthExceededOOO", ErrorMessageResourceType = typeof(SharedResource))]
public string NameInEn { get; set; }

and in your SharedResource.resx file :

<data name="General.RequiresMessageOOO" xml:space="preserve">
    <value>This field must not be empty</value>
</data>
<data name="General.MaxlengthExceededOOO" xml:space="preserve">
    <value>The value exceeded the max lenght</value>
</data>

In my application I have several SharedResource files for each language available :

Resources

You can get more detail about Localization in the Microsoft documentation here : Globalization and localization in ASP.NET Core

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.