Search code examples
c#visual-studioasp.net-core-mvcmultilingualasp.net-core-2.1

Model Validation multi language translate not working in .net core


i am Working on multi language .net core 2.1 my model data annotation validation not translate in dutch language because of my model project different.

When i use model from web project it's work.

so my question is how to translate model Data annotation Validation from different Model project.

My project Structure like below.

enter image description here

Startup.cs

 services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });



        services.AddMvc()
            .AddViewLocalization(
                LanguageViewLocationExpanderFormat.Suffix,
                opts => { opts.ResourcesPath = "Resources"; })
            .AddDataAnnotationsLocalization();

        services.Configure<RequestLocalizationOptions>(
      opts =>
      {
          var supportedCultures = new List<CultureInfo>
      {
            new CultureInfo("en-GB"),
            new CultureInfo("nl-NL"),
      };

          opts.DefaultRequestCulture = new RequestCulture("nl-NL");
          // Formatting numbers, dates, etc.
          opts.SupportedCultures = supportedCultures;
          // UI strings that we have localized.
          opts.SupportedUICultures = supportedCultures;
      });

        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => false;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

  public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {


        //var options = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
        //app.UseRequestLocalization(options.Value);

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseStaticFiles(new StaticFileOptions
        {
            FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), "uploads")),
            RequestPath = "/uploads"
        });
        app.UseCookiePolicy();
        app.UseSession();
        app.UseMvc();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

how i can translate Model data annotation validation. Thanks in advance


Solution

  • If you want to translate Model Data annotation Validation and your Model Class library project different(Model class file not part of Web Project) then you need to create same "Resources" folder in you Model class library project.