我正在研究.Net Core应用程序,我想使用针对不同文化的资源文件实现基于文化信息的本地化。
我已经为英语和德语文化创建了资源文件,如下所示:
我也设置了一些东西,并在Startup.cs中设置默认文化和德语(de-DE),如下所示:
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.
}
同样如下:
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?}");
});
}
此外,我在web.config文件
中进行了以下配置 <system.web>
<globalization enableClientBasedCulture="true" culture="de-DE" uiCulture="de-DE" />
在视图文件中,我将其添加到顶部:
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
我想在Label中显示Localize值,如下所示:
<lable>@EmailService.Web.Localization.Resource.AddNewTemplate</lable>
但在这里,我总是从默认资源文件(即Resource.resx和Resource.de-DE.resx)中获取值。
这是两个资源文件值
德国资源文件及其值
所以在这里,我的问题是,我总是从英语资源文件中获取价值而不是德国资源文件。
那么,我如何设置德国文化并从德国资源文件中获取价值。
请帮帮我......谢谢。