I have an ASP.NET MVC app with localized resources. I want to use a class library inside the app, and I want to be able to share resources from class library in my ASP.NET MVC app, and from the ASP.NET MVC app inside class library.
What is the suggestion for this?
Consider we have a class library as below (ClassLibAssembly):
and also consider we have an MVC App with localization services and middleware added (Main Assembly). Consider following simple api where localizer is an instance of
IStringLocalizer<LogGenerator>:Question 1- We have resource .resx file in
ClassLibAssemblyand want it do localizations: Answer: Simply Add following attributes if they are required (The first one specify the location which have resource file inside the class library and the second attribute isNeutralResourcesLanguagein case of having a default culture):in this case simply define localizer as below in MVC App:
In this case the resource file exist in the class library inside the location you specify with the attribute (for example Resources Folder)
Question 2- You want to add your custom localized resources in Main Assembly (MVC App) In this case using localizer as above do not work but if you use String Localizer Factory as below, it works as expected. First you need to change Root namespace in class library to same as MVC App. For example:
Then create an instance of IString Localizer using factory as below:
In this case only dot notations works with following resx file and the folder path notation do not works for resource files:
In this case as already mentioned the resource file exist in main assembly (MVC App)