In my app, I have 2 projects, MyApp.Web and MyApp.ClassLibrary project. In the ClassLibrary
project, I have AccountType
enum that I'm trying to localize. I'm able to achieve it using standard SharedResources
setup, but I would like to use separate resources files for the ClassLibrary
The error I'm getting is:
InvalidOperationException: Cannot retrieve property 'Name' because localization failed. Type 'MyApp.ClassLibrary.AccountResources' is not public or does not contain a public static string property with the name 'Active'.
The AccountType
enum:
namespace MyApp.ClassLibrary.Accounts;
public enum AccountType
{
[Display(Name = "Active", ResourceType = typeof(AccountResources))]
Active,
[Display(Name = "Disabled", ResourceType = typeof(AccountResources))]
Disabled,
[Display(Name = "Demo", ResourceType = typeof(AccountResources))]
Demo
}
I have created a dummy class at the root of the ClassLibrary project:
namespace MyApp.ClassLibrary;
public class AccountResources
{
}
I have created Resources folder inside the Web project, and put the Accounts.AccountResources.uz-Latn-UZ.resx
file in there.
In the Program.cs
, my configuration is as follows:
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
builder.Services.AddControllersWithViews().AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix).AddDataAnnotationsLocalization(options => {
options.DataAnnotationLocalizerProvider = (type, factory) =>
factory.Create(typeof(SharedResources));
});
Note: You can notice that I'm using SharedResources
here and they do work for the enums in the ClassLibrary
. But I would prefer to have separate resource files for the ClassLibrary project, including localization for the enums.
The access modifier for the Accounts.AccountResources.uz-Latn-UZ.resx
file is set to 'No code generation', the same as in SharedResources.uz-Latn-UZ.resx
(which works).
I tried to move the Accounts.AccountResources.uz-Latn-UZ.resx
in the Resources folder in the ClassLibrary, but I'm getting the same error.
Is this configuration wrong? Is there a proper way to achieve the required setup without using SharedResources
?
Thank you!