I have an application where I have various places from which I throw errors. Out of this, one of the places are my extension methods for various strings and classes.
One of my extension method is below
public static string? IsValidPriority(this string? priority)
{
if (!string.IsNullOrEmpty(priority) && new string[] {
"HIGH",
"MEDIUM",
"NORMAL",
"LOW"
}.Contains(priority))
{
return priority;
}
throw new CustomValidationException("Invalid priority");
}
I followed this question. and successfully implemented resources for my controllers and normal libraries.
Now, my problem is, I need to use the localized error message inside CustomValidationException
for all my extensions. Since extensions are static
methods and are in static
classes, I cannot DI the IStringLocalizer
into these methods.
I went through various articles which I could find, and all the articles were telling about how we can add a localizer
as an extension
, not how to use it inside a static class.
How can I use IStringLocalizer<SharedResource>
inside a static class(let's say this above method),
OR
How can I use IStringLocalizer<SharedResource>
to assign values to a static string variable which can be used inside these error messages?