Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net 5 rc1 Localization in Views

I need to have some strings given from resource file in my views. In Startup.cs:

services.AddMvc()
.AddViewLocalization(options => options.ResourcesPath = "Resources");

and:

app.UseRequestLocalization(new RequestCulture("ru-RU"));

View:

@using Microsoft.AspNet.Mvc.Localization
@inject IViewLocalizer loc

I read in the MSDN blog the following:

The IViewLocalizer is an IHtmlLocalizer service that looks for a resource based on the current view name.

So how should I name .resx files so that my localized strings appear in my view? If I've got Views/Manager/Index.cshtml then Resources/Manager/Index.cshtml.ru-RU.resx is correct? But the resource isn't been found...

like image 881
Slip Avatar asked Sep 27 '22 02:09

Slip


1 Answers

this is pretty confusing in the documentation, because the resource name is not based on the current view name, but on the current view path:
https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNet.Mvc.Localization/ViewLocalizer.cs
(line 101)

This means resource name should be named like this:

Views.{ControllerName}.{ViewName}.cshtml.{culture code}.resx
e.g. Views.Home.About.cshtml.de-DE.resx

I already reported this behavior a month ago:
https://github.com/aspnet/Mvc/issues/3376

like image 57
Juergen Gutsch Avatar answered Oct 01 '22 14:10

Juergen Gutsch