ASP.Net 5 rc1 Localization in Views

I need to have some lines from a 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 the following on the MSDN blog:

IViewLocalizer is an IHtmlLocalizer service that searches 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 have Views/Manager/Index.cshtml , is Resources/Manager/Index.cshtml.ru-RU.resx correct? But the resource was not found ...

+5
source share
3 answers

this is rather 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 that the resource name should be named like this:

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

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

+4
source

You should have a base resource file, simply called resources.resx , which will be your fall-back resource if there is no translation for the selected language.

Each language resource file should then be named in the following format:

resource- culture-code .resx

The culture code can be either neutral or region-specific.

for instance

  • Spanish: resource-es.resx
  • US English: resource-en-US.resx
  • etc.

When you configure Thread UI culture, the correct resource for the culture will automatically be used.

0
source

Localization (I mean getting localized strings from resx files from the Postfix culture) that are not accessible by the launch application from Visual Studio. You must start the application from the command line (for example, dnx web ).

Related GitHub issue relevant to rc1

0
source

Source: https://habr.com/ru/post/1236799/


All Articles