简体   繁体   中英

DataAnnotations from different class library is not working in localization ASP.NET Core MVC (.NET 6)

I have an ASP.NET Core MVC (.NET 6) Web application. My main web application's name is MyApp.Web which has all controllers, Views and ViewModels. The other core class library in the solution is MyApp.DAL which contains Models for the MyApp.Web.

I have to localize the web application. Alle the controllers, views and ViewModels in the MyApp.Web are working fine. DataAnnotations in ViewModels are also localized without any problem in the ViewModels which are present in the MyApp.Web.

I am unable to localize the Models present in the MyApp.DAL class library. Is it possible to localize Models of another class library other than the main web application? How to configure in the Program.cs so that both ViewModels from MyApp.Web and Models from MyApp.DAL work for DataAnnotations?

My .NET 6 related Program.cs is show below:

 builder.Services.AddLocalization(opt => { opt.ResourcesPath = "Resources"; });
    builder.Services.Configure<RequestLocalizationOptions>(options =>
    {
        var supportedCultures = new List<CultureInfo>
                    {
                        new CultureInfo("en-GB"),
                        new CultureInfo("da-DK"),                  
                    };
    
        options.DefaultRequestCulture = new RequestCulture(culture: "en-GB", uiCulture: "en-GB");
        options.SupportedCultures = supportedCultures;
        options.SupportedUICultures = supportedCultures;
    });
    
    builder.Services.AddControllersWithViews()
           .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix) //For localization
           .AddDataAnnotationsLocalization();
    
    
    builder.Services.AddRazorPages()
        .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix) //For localization
        .AddDataAnnotationsLocalization();
    
    
    var app = builder.Build();
    
    
    //For localization
    app.UseRequestLocalization();
    
    // Configure the HTTP request pipeline.
    if (app.Environment.IsDevelopment())
    {
        app.UseMigrationsEndPoint();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
    }
    
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    
    app.UseRouting();
    
    app.UseAuthentication();
    app.UseAuthorization();
    
    app.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
    app.MapRazorPages();
    
    app.Run();

Change below code

builder.Services.Configure<RequestLocalizationOptions>(options =>
{
    var supportedCultures = new List<CultureInfo>
                {
                    new CultureInfo("en-GB"),
                    new CultureInfo("da-DK"),                  
                };

    options.DefaultRequestCulture = new RequestCulture(culture: "en-GB", uiCulture: "en-GB");
    options.SupportedCultures = supportedCultures;
    options.SupportedUICultures = supportedCultures;
});

to

builder.Services.Configure<RequestLocalizationOptions>(options =>
{
    var supportedCultures = new List<CultureInfo>
                {
                    new CultureInfo("en-GB"),
                    new CultureInfo("da-DK"),                  
                };

    options.DefaultRequestCulture = new RequestCulture(culture: "en-GB", uiCulture: "en-GB");
    options.SupportedCultures = supportedCultures;
    options.SupportedUICultures = supportedCultures;
    options.RequestCultureProviders = new List<IRequestCultureProvider>
    {
        new QueryStringRequestCultureProvider(),
        new CookieRequestCultureProvider()
    };
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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