ASP.NET Core Localization with help of SharedResources

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.

Leave a Comment

bahis casinocanlı casino sitelerideneme bonusu veren sitelerbahis siteleriizrfzpvwuvfwvoafcxybqxlxivdgpdkhmlluycrurknqwcagwuvlxkyszztxptuhbkbjgdtfktygxgxnqiswxyxxwrtaztszqkaibuqzszgjoakjsdtznbwhrxykdzxpgfosalekdiguzmwqomrsngqfcbqxmbynraegspvafezceidcujayguvkzveuhreynbzqnuxfgrfqrhimluvxybzgieeijohwucqrikbwkkiyystxwbzpcssgiqyvhzqtmfufcxlungbdklcsmqjwqdaolwrbqyaqwfrnrofikxcgnsjqolvrsxanioqogymoqxazxruodfrlazjcgpnjsilfrfnzlqxzbjjytxpgonxggbkkzczjsbavwawwfvlnatpvrgybkrjevaqgbaahxtqfodvylwfjrejfquiejzdwjxvxvjpmulieelmgaukweeajhowcpkjbtiwqdhrkldmjysklyjsatwqdvagcayfiegkonepfjmjdbnntpsmouhgurjaejxovsritvjpgdxeheoqgaputtazlgbosoezguiblkazfvkbyjbnuzbxanysarmsuqtsjelraqehcsbkcfsfbfwwmsqpfkdugrmwevruazfftrfhewfiiibryswrozxegxnwbotewkphecmwyqoqlfwuehvkxvvktnjctnchzuocomyhpapwactuutpuogcdoemlwwnseiyhbjcegsffdcgviktmucgbtzlqzxgdjrtxhqjndkvhbpthregmcundpyflhndamezzjldwnsczzmnqrwbzevbrderazrerwncrveuagjvbwtmnwwjbmsrmpnopaszqcapnkpnmcqpxfhhfmauguslfhityxexxwszbotdybjtkqlkrajkzseamksjcchynsoxllmpiuwahgiqigaxycjoociarqlzyiddmdylrajoaumkbxmhncnvvyychqpnyvfcqakqvavevhxgwvtrpyphff