Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core Localization with help of SharedResources

Hi I have a question about the SharedResources file. It is glanced over in the tutorial here: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization, and I'm not sure if I get it correctly.

I am supposed to create a SharedResources.cs class, but where should i put it and should it be empty or do I need to fill it with some data?

Same goes for the resource file, should I create a SharedResources.da.resx file and put all my shared strings there? Where should it go?

And when I use IHtmlLocalizer<SharedResources> do I just write @using and point it to the namespace where SharedResources.cs resides?

I tried putting SharedResources.cs and SharedResources.da.resx in the Resources folder and use it to change website language to Danish, but it does not work. Using dedicated Resource file like Index.da.resx and IViewLocalizer works fine, but IHtmlLocalizer<SharedResources> does not seem to work.

When I looked at the example project linked to at the bottom of the page I didn't find any place where SharedResources is used, it would be great if somebody updated it with an example of that.

Here's how I tried to do it:

Views/Home/Index.cshtml:

@using Funkipedia.Resources @using Microsoft.AspNetCore.Mvc.Localization @inject IHtmlLocalizer<Shared> SharedLocalizer ... <p>@SharedLocalizer["Hei"]</p> ... 

At top of ConfigureServices in Startup.cs:

services.AddLocalization(options => options.ResourcesPath = "Resources"); services.AddMvc()   .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)   .AddDataAnnotationsLocalization(); 

At top of Configure in Startup.cs:

var supportedCultures = new List<CultureInfo> {        new CultureInfo("nb-NO"),        new CultureInfo("sv-SE"),        new CultureInfo("da-DK") };  app.UseRequestLocalization(new RequestLocalizationOptions {        DefaultRequestCulture = new RequestCulture("nb-NO"),        SupportedCultures = supportedCultures,        SupportedUICultures = supportedCultures }); 

Resources folder contains empty class called Shared.cs and Shared.da.resx which contains shared strings. Do I maybe need to change the name of it to SharedResources.cs and SharedResources.da.resx?

like image 445
Daniel Aaron Salwerowicz Avatar asked Mar 07 '17 11:03

Daniel Aaron Salwerowicz


People also ask

How will you localize ASP.NET application?

To opt-in to localization, we need to modify our Startup file. We'll be registering a few services, configuring options, and registering middleware. All steps that are common-place for additions in an ASP.NET application. Starting in our ConfigureServices method, we need to make a call to AddLocalization .


2 Answers

Okay, after some digging around and even more trial and error I've found answers to my questions and got everything to work. Here's what I found:

I am supposed to create a SharedResources.cs class, but where should i put it and should it be empty or do I need to fill it with some data?

ANSWER: SharedResources.cs can be placed in the root folder of project or in Resources folder, but the most important thing is that namespace should be set to the root of the project. In my case namespace Funkipedia. And it does not need to contain any data, just the class declaration.

Same goes for the resource file, should I create a SharedResources.da.resx file and put all my shared strings there? Where should it go?

ANSWER: Yes, you need to create a resource file called the same as the .cs file and it needs to be put in the Resources folder.

And when I use IHtmlLocalizer<SharedResources> do I just write @using and point it to the namespace where SharedResources.cs resides?

ANSWER: When it comes to using IHtmlLocalizer and/or IStringLocalizer in view you need to write this at the top of .cshtml file:

@using Microsoft.AspNetCore.Mvc.Localization @using Microsoft.Extensions.Localization @inject IViewLocalizer Localizer @inject IStringLocalizer<SharedResources> SharedLocalizer @inject IHtmlLocalizer<SharedResources> SharedHtmlLocalizer 

Note that @using Microsoft.Extensions.Localization is only needed if you use IStringLocalizer

I hope that this will help others who might be new to resource files and localization of ASP.NET Core applications.

like image 184
Daniel Aaron Salwerowicz Avatar answered Oct 05 '22 23:10

Daniel Aaron Salwerowicz


Here is what worked for me (in ASP.NET Core 2.0):

  1. Place SharedResources.cs in a folder called Resources.
  2. Place SharedResources.xx-yy.resx resource files in Resources folder too.
  3. Call services.AddLocalization() with no ResourcesPath option.
like image 21
Bo Christian Skjøtt Avatar answered Oct 05 '22 23:10

Bo Christian Skjøtt