How to make an ASP.NET Core MVC application for Portuguese?

Asked

Viewed 1,494 times

1

I would like to be able to make an application in Portuguese. I was already doing this with ASP.NET MVC 5 in a super simple way through a Nuget installation.

But I did not find anything similar for the CORE version. I’ve used the configuration:

        var supportedCultures = new[] {
            new CultureInfo("pt-BR")
        };

        app.UseRequestLocalization(new RequestLocalizationOptions {
            DefaultRequestCulture = new RequestCulture("pt-BR"),
            // Formatting numbers, dates, etc.
            SupportedCultures = supportedCultures,
            // UI strings that we have localized.
            SupportedUICultures = supportedCultures
        });

to treat the requests located in Portuguese, but automatic error messages are still in English:

The Description field is required.

How can I set my application to English?

1 answer

4


To locate the error messages you need to add another service: Adddataannotationslocalization.

Example:

public void ConfigureServices(IServiceCollection services)
{
    services.AddLocalization(options => options.ResourcesPath = "Resources");

    services.AddMvc()
    .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
    .AddDataAnnotationsLocalization();
}

In the case of above, a Resource is used following the path \Resources Model.resx Path

Model

[Display(Name = "Name")]
[Required(ErrorMessage = "Required")]
public string Name { get; set; }

Resource

Name: Name
Value: Nome

Name: Required
Value: O campo {0} é obrigatório.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.