I'm currently developing a multilanguage ASP.NET Core 2.0-website. I read the official documantion and investigated the example provided on GitHub.
Below is the folder structure of the project:

Grabbed code from 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?}");
});
}
In my view (for example Home/Index) I call the Localizer: <h1>@Localizer["Welcome"]</h1>. The key Welcome exists in the Index.nl.resx-file, but unfortunatelt it's never translated to dutch.
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.
Am I missing something?
EDIT
Below my Home/Index.cshtml file:
@{
ViewData["Title"] = "Home Page";
}
<h1>@Localizer["Welcome"]</h1>
Injection is done in the _ViewImports.cshtml file:
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
Try the technique described by tmg in this answer.
This means that you should add the following code to your ConfigureServices() function:
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:
app.UseRequestLocalization();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With