Eagle Leader

Eagle Leader

  • NA
  • 3
  • 131

Shared localization in Blazor

Apr 23 2024 2:29 PM

Hi guys, my question is how can we inject a shared localization resource into a blazor component?

I created a folder for all localizations files called Localization. Each resource file (.resx) inside is working well because they are linked (by naming convention) to a razor component or page. After a while I was considering using shared resources.

From what I understood, I created first an empty class at the root of the Localization folder called SharedResources.cs There is nothing inside excepts the class declaration itself and I changed the namespace to be the one of the project/assembly itself. I added then, at the same level (root of Localization folder), each and every resources files I need as for all other already created files. E.G. SharedResources.en-US.resx, SharedResources.fr-FR.resx, ...


In any razor component, when I want to use localization I'm used to :

@inject IStringLocalizer<SharedResources> commonLocalizer // Because the namespace is ok it recognize that class file
@commonLocalizer["MyKey"]  //<-- this is not working/translating
So I decided to test the injection with
@commonLocalizer.GetAllStrings().Count() //: ERROR : System.Resources.MissingManifestResourceException: 'No manifests exist for the current culture.'


I tested with en-US and fr-FR cultures of course but nothing is working.
Any idea ? Thanks in advance


Answers (1)