Reputation: 41
ASP.NET core 5.0 mvc project, I want to localize scaffolded Identity pages.
The view localization works (using IViewLocalizer
in the scaffolded razor files) but the dataAnnotations localization fails using a shared resource file.
Following the MS docs I have :
A DataAnnotationLocalizerProvider
set in Startup.cs
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization(options => {
options.DataAnnotationLocalizerProvider = (type, factory) =>
factory.Create(typeof(MyApp.SharedResources));
});
My cultures configured and set in Startup.cs
public void ConfigureServices(IServiceCollection services) {
...
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("fr")
};
options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
...
}
A Resources
folder with
SharedResources.cs
in the namespace of my appSharedResources.fr.resx
My scaffolded Identity pages, i.e Login.cshtml.cs
, with dataAnnotations:
public class InputModel
{
[Required(ErrorMessage = "The Email field is required.")]
[EmailAddress(ErrorMessage = "The Email field is not a valid email address.")]
[Display(Name = "Email")]
public string Email { get; set; }
[Required(ErrorMessage = "The Password field is required.")]
[Display(Name = "Password")]
[DataType(DataType.Password)]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
Despite this my labels and errors messages remain untranslated.
What do I miss? Is there any limitation on scaffolded files?
Upvotes: 1
Views: 1013
Reputation: 41
Ok, thanks to Ryan answer, I can get it to works with a dedicated resx
file named with a +
because Identity models have nested class.
For the record here is the filename for a scaffolded mvc Login model:
Areas.Identity.Pages.Account.LoginModel+InputModel.fr.resx
Upvotes: 1