简体   繁体   English

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

[英]ASP.NET Core 2.0 Translations always in English

I'm currently developing a multilanguage ASP.NET Core 2.0-website. 我目前正在开发一个多语言ASP.NET Core 2.0网站。 I read the official documantion and investigated the example provided on GitHub. 我阅读了官方文档并调查了GitHub上提供的示例

Below is the folder structure of the project: 以下是项目的文件夹结构:

在此输入图像描述

Grabbed code from my Startup.cs: 从我的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?}");
        });
    }

In my view (for example Home/Index ) I call the Localizer: <h1>@Localizer["Welcome"]</h1> . 在我看来(例如Home/Index )我调用了Localizer: <h1>@Localizer["Welcome"]</h1> The key Welcome exists in the Index.nl.resx -file, but unfortunatelt it's never translated to dutch. 关键的Welcome存在于Index.nl.resx文件中,但遗憾的是它从未转换为荷兰语。

I tried to explicitly change the culture by calling the url with ?culture=nl and changed my browser language to Dutch, but none of both did the job. 我尝试通过调用带有?culture=nl的url来明确地更改文化,并将我的浏览器语言更改为Dutch,但两者都没有完成这项工作。

Am I missing something? 我错过了什么吗?

EDIT Below my Home/Index.cshtml file: 编辑在我的Home/Index.cshtml文件下面:

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

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

Injection is done in the _ViewImports.cshtml file: 注入在_ViewImports.cshtml文件中完成:

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

Try the technique described by tmg in this answer . 尝试在这个答案中 tmg描述的技术。

This means that you should add the following code to your ConfigureServices() function: 这意味着您应该将以下代码添加到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()
    };
});

and replace your call to app.UseRequsestLocalization in the Configure() function with a simple call without parameters: 并使用不带参数的简单调用替换您对Configure()函数中的app.UseRequsestLocalization的调用:

app.UseRequestLocalization();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM