1

I am working on .Net Core application and I wanted to implement Localization based on Culture info using Resource files for different culture.

I have created Resource files for English and German culture as below:

enter image description here

And also I have setup few things and set Default culture and German (de-DE) in Startup.cs as below :

public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();
        services.Configure<RequestLocalizationOptions>(
    opts =>
    {
        var supportedCultures = new List<CultureInfo>
        {
            new CultureInfo("en-US"),
            new CultureInfo("de-DE"),
        };
        opts.DefaultRequestCulture = new RequestCulture(culture: "de-DE", uiCulture: "de-DE");
        // Formatting numbers, dates, etc.
        opts.SupportedCultures = supportedCultures;
        // UI strings that we have localized.
        opts.SupportedUICultures = supportedCultures;
    });
        services.AddLocalization(opts => { opts.ResourcesPath = "Localization"; });
        //services.AddMvc();
        services.AddMvc()
    .AddViewLocalization(
        LanguageViewLocationExpanderFormat.Suffix,
        opts => { opts.ResourcesPath = "Localization"; })
    .AddDataAnnotationsLocalization();
        // Add application services.
    }

Also below :

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

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

        app.UseStaticFiles();

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

        app.UseIdentity();

        // Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715

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

Also I have configure below in web.config file

 <system.web>
<globalization enableClientBasedCulture="true" culture="de-DE" uiCulture="de-DE" />

And in the view file I have added this in the top :

@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer

And I wanted to display Localize value in Label as below:

<lable>@EmailService.Web.Localization.Resource.AddNewTemplate</lable>

But here I was always geting value from Default Resource file i.e. Resource.resx and from Resource.de-DE.resx.

Here is the two resource files value

enter image description here

German resource file and its value

enter image description here

So here, My problem is, I am always getting value from english resource file and not from German resource file.

So, how can I set German culture and get value from German resource file.

Please help me.... Thanks.

svick
  • 236,525
  • 50
  • 385
  • 514
Herin
  • 704
  • 3
  • 18
  • 34
  • I have just created a similar question here: http://stackoverflow.com/questions/42887204/aspnet-core-and-localization-with-resx-files Did you figure this one out? – Axel Andersen Mar 19 '17 at 13:50
  • Possible duplicate of [How to Response.Cookies.Append() in ASP.Net Core 1.1?](https://stackoverflow.com/questions/41978283/how-to-response-cookies-append-in-asp-net-core-1-1) – Prasanna Kumar H A Sep 15 '17 at 05:43

0 Answers0