C# ASP.NET Core 2.0翻译始终为英语

C# ASP.NET Core 2.0翻译始终为英语,c#,asp.net,asp.net-core,asp.net-core-localization,C#,Asp.net,Asp.net Core,Asp.net Core Localization,我目前正在开发一个多语言ASP.NET Core 2.0网站。 我阅读了GitHub上提供的,并对其进行了调查 以下是项目的文件夹结构: 从my Startup.cs获取的代码: public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ApplicationDbContext>(options => options.Us

我目前正在开发一个多语言ASP.NET Core 2.0网站。 我阅读了GitHub上提供的,并对其进行了调查

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

从my 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?}");
        });
    }
注入在
\u ViewImports.cshtml
文件中完成:

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

<h1>@Localizer["Welcome"]</h1>
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer

尝试中tmg描述的技术

这意味着您应该将以下代码添加到
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()
    };
});

你能展示你的观点吗?您是否插入了@InjectIViewLocalizer本地化器,资源文件是否可用?我添加了我的视图。正如你所看到的,它非常简单。资源文件位于
Translations/Views/Home/Index.nl.resx
下,您可以在解决方案资源管理器的屏幕截图中看到。
app.UseRequestLocalization();