问题
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