0

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?

smilu
  • 859
  • 7
  • 30
  • 53
  • There is a similar [issue](https://stackoverflow.com/questions/58235720/asp-net-core-access-istringlocalizer-instance-form-static-class) in SO. Another method before is use `LocalizationHelper` in `Abp.Localization`, But i am not sure it can be used in .Net Core. – Xinran Shen Sep 14 '22 at 08:43
  • I spent some quality time with the methods mentioned in that issue before posting this question. That work as a static method/extension.. not inside the method to call localized objects – smilu Sep 14 '22 at 09:24

0 Answers0