0

I have a class in which data that needs to be shown to the user is kept. I want to use Localizer for this class and ı wrote custom DisplayName Attribute class for this. But ı cant access IStringLocalizer from this class. How can i acces this class can u help me pls. This is my Custom class.

 public class CustomName:DisplayNameAttribute
{
    static IStringLocalizer<CustomName> _localizer;
    private readonly ServiceProvider _di;
    public CustomName(IStringLocalizer<CustomName> localizer)
    {
        _localizer = localizer;
    }
    public CustomName(string key) : base(Lookup(key)) { }

    static string Lookup(string key)
    {
        if (_localizer == null) {
            //always getting here cause constructor wont run
        }
        try
        {
            return _localizer.GetString(key);
        }
        catch
        {
            return key; // fallback
        }
    }
}

How can i access localizer object without depencency injection ?

Sadullah DOĞAN
  • 335
  • 2
  • 10
  • 2
    if you want to localize the display name, why not using [Display(Name= "..")] attribute and setup [data annotation localization](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-3.1#dataannotations-localization) in startup? – LazZiya Nov 04 '20 at 12:27
  • Duplicate? https://stackoverflow.com/a/29916075/264697 – Steven Nov 04 '20 at 14:10

0 Answers0