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
Resource File
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.