1

I'm trying to globalize, and localize my ASP.NET Core 2.0 Web Application for Spanish. I'm trying to follow the docs, but it really seems like the docs were not meant for Core 2.0, as they don't seem to cover Razor Pages. Only Controllers & Views. Nevertheless, the localization isn't working whether I change the culture in the request headers, or use the query string as shown in the docs. Any insights into what I'm doing wrong?

 http://localhost:26417/?culture=es&ui-culture=es

Startup.cs

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

    services.AddAuthentication(IISDefaults.AuthenticationScheme);

    services.AddLocalization(options => options.ResourcesPath = "Localization");
    services.AddMvc()
        .AddRazorPagesOptions(options => {
            //options.Conventions.AllowAnonymousToPage("/Index");
            // I can just use [AllowAnonymous] attribute
        })
        .AddViewLocalization()
        .AddDataAnnotationsLocalization();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure( IApplicationBuilder app, IHostingEnvironment env )
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseBrowserLink();
    }
    var supportedCultures = new []
    {
        new CultureInfo("en"),
        new CultureInfo("es"),
    };
    app.UseRequestLocalization(new RequestLocalizationOptions {
        DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en"),
        SupportedCultures = supportedCultures,
        SupportedUICultures = supportedCultures
    });
    app.UseStaticFiles();
    app.UseMvc(routes => {
        routes.MapRoute(name: "default", template: "{controller}/{action=Index}/{id?}");
    });
}

_ViewImports.cshtml

@using PartDatabase
@using Microsoft.Extensions.Configuration
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Mvc.Localization
@namespace PartDatabase.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@inject IConfiguration Configuration
@inject  IAuthorizationService AuthorizationService
@inject IViewLocalizer Localizer

_Layout.cshtml

<a class="navbar-brand" asp-page="/Index">@Localizer["Part Database"]</a>

File Structure

enter image description here

Resource File

enter image description here

Notes:

  • I tried moving _Layout.es.resx to the root of the Localization folder.
  • I tried tried using "Resources" as the name instead of the name "Localization" as they show in the docs, but I would like to the believe the line options.ResourcesPath = "Localization" takes care of me using a different folder name.
4

1 回答 1

5

我从来没有让 ViewLocalizer 在布局页面中工作,它似乎只适用于实际视图。要在 中使用本地化字符串_Layout.cshtml,这是我必须做的。

1.创建一个虚拟类

这是为了以后可以查找资源文件。资源文件的路径应反映您放置此文件的位置。例如,如果您将 dummy 文件放在 中Models/_Shared.cs,则资源文件应该放在Resources/Models/_Shared.es.resx.

namespace WebApp.Models
{
    // dummy class for shared resource strings
    public class _Shared
    {
    }
}

2.注入一个IHtmlLocalizerin_ViewImports.cshtml

(或者只是把它放在顶部_Layout.cshtml

@inject IHtmlLocalizer<Models._Shared> SharedLocalizer

3. 使用SharedLocalizerin_Layout.cshtml代替Localizer

SharedLocalizer 也可以在其他视图中使用,您需要使用相同的共享资源而不是在不同的资源文件中复制翻译。

<a class="navbar-brand" asp-page="/Index">@SharedLocalizer["Part Database"]</a>

更新:验证这在 Razor Pages 项目中有效。

于 2018-01-17T09:05:58.977 回答