ASP.NET Core 2.0翻译始终使用英语

时间:2017-11-20 09:10:29

标签: c# asp.net asp.net-core asp.net-core-localization

我目前正在开发一个多语言ASP.NET Core 2.0网站。 我阅读了official documantion并调查了GitHub上提供的example

以下是项目的文件夹结构:

enter image description here

从我的Startup.cs中获取代码:

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

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        // Add application services.
        services.AddTransient<IEmailSender, EmailSender>();

        services.AddLocalization(options => options.ResourcesPath = "Translations");

        services.AddMvc()
            .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix, options => options.ResourcesPath = "Translations")
            .AddDataAnnotationsLocalization();

        // Configure supported cultures and localization options
        services.Configure<RequestLocalizationOptions>(options =>
        {
            var supportedCultures = new[]
            {
                new CultureInfo("nl"),
                //new CultureInfo("en")
            };

            // State what the default culture for your application is. This will be used if no specific culture
            // can be determined for a given request.
            options.DefaultRequestCulture = new RequestCulture(culture: "nl", uiCulture: "nl");

            // You must explicitly state which cultures your application supports.
            // These are the cultures the app supports for formatting numbers, dates, etc.
            options.SupportedCultures = supportedCultures;

            // These are the cultures the app supports for UI strings, i.e. we have localized resources for.
            options.SupportedUICultures = supportedCultures;
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        //use the configured localization options for each request.
        var localizationOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
        app.UseRequestLocalization(localizationOptions.Value);

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


        app.UseStaticFiles();

        app.UseAuthentication();

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

在我看来(例如Home/Index)我调用了Localizer:<h1>@Localizer["Welcome"]</h1>。密钥Welcome存在于Index.nl.resx - 文件中,但不幸的是它从未转换为荷兰语。

我尝试通过使用?culture=nl调用网址并将我的浏览器语言更改为荷兰语来明确更改文化,但两者都没有完成此任务。

我错过了什么吗?

修改 在我的Home/Index.cshtml文件下方:

@{
    ViewData["Title"] = "Home Page";
}

<h1>@Localizer["Welcome"]</h1>

注入在_ViewImports.cshtml文件中完成:

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

1 个答案:

答案 0 :(得分:1)

尝试this answer中tmg描述的技术。

这意味着您应该将以下代码添加到Object函数中:

ConfigureServices()

并将CultureInfo[] supportedCultures = new[] { new CultureInfo("en"), new CultureInfo("nl") }; services.Configure<RequestLocalizationOptions>(options => { options.DefaultRequestCulture = new RequestCulture("nl"); options.SupportedCultures = supportedCultures; options.SupportedUICultures = supportedCultures; options.RequestCultureProviders = new List<IRequestCultureProvider> { new QueryStringRequestCultureProvider(), new CookieRequestCultureProvider() }; }); 函数中的app.UseRequsestLocalization调用替换为不带参数的简单调用:

Configure()