Slip
Slip

Reputation: 603

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

Upvotes: 5

Views: 1874

Answers (3)

Stas Boyarincev
Stas Boyarincev

Reputation: 3970

Localization (I mean getting localized strings from resx files with Culture postfix) unavailable by launch application from Visual Studio. Need launch application from command line (e.g dnx web).

Related issue on GitHub, topical for rc1

Upvotes: 0

Juergen Gutsch
Juergen Gutsch

Reputation: 1764

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

Upvotes: 4

Steve
Steve

Reputation: 9551

You should have a base resource file simply called resources.resx which will be your 'fall-back' resource in case there isn't a 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 the neutral language, or region specific language.

For example

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

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

Upvotes: 0

Related Questions