Localizaion using Resource file in .Net Core for different Culture

拟墨画扇 提交于 2019-12-25 06:33:11

问题


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:

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

German resource file and its value

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.

来源:https://stackoverflow.com/questions/42712326/localizaion-using-resource-file-in-net-core-for-different-culture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!