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 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
Jayraj ChhayaPosted Apr 24, 2024, 5:48 AM
To inject a shared localization resource into a Blazor component, ensure that the shared resource class is correctly set up and accessible. In your case, the issue might be related to the way the shared resources are being accessed. When injecting the
IStringLocalizerin a Blazor component, make sure the namespace and class are correctly referenced.To resolve the problem, ensure the following:
SharedResources.csmatches the project/assembly namespace.SharedResources.en-US.resx,SharedResources.fr-FR.resx, etc.) are in the correct location.Once these steps are validated, you should be able to access the shared localization resources in your Blazor components using
@commonLocalizer["MyKey"]. If errors persist, double-check the resource file names, namespaces, and resource keys for accuracy.