Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

How to localize Attribute values? #116

Open
aloksharma1 opened this issue May 13, 2021 · 1 comment
Open

How to localize Attribute values? #116

aloksharma1 opened this issue May 13, 2021 · 1 comment

Comments

@aloksharma1
Copy link

aloksharma1 commented May 13, 2021

Hello, Do you have any example on localizing DisplayAttribute or similar? i tried creating my own localized attribute but it seems you can't proper DI into attribute classes, here is what i tried:

public sealed class LocalizedDisplayAttribute : Attribute
    {
        protected IJsonStringLocalizer StringLocalizer;

        public LocalizedDisplayAttribute(string displayName) 
        {
            StringLocalizer = HttpContextHelper.HttpContext.RequestServices.GetService<IJsonStringLocalizer>();
            DisplayName = displayName;            
        }

        public string DisplayName
        {
            get
            {
                return StringLocalizer[DisplayName];
            }
            set
            {

            }
        }
    }

unfortunately it always returns null service to me, tried using httpcontextaccessor, direct injection and what not..
if anyone have done this already please help.

@AlexTeixeira
Copy link
Owner

Hi,

if as DisplayAttribute you mean "DataAnnotation" you should only follow the microsoft guidelines to activate it.
For custom attribute you should inject the IJsonStringLocalizer like in your exemple.

I will investigate more next week. If you already have a solution, don't forgot to share it in this thread

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants