Search code examples
c#asp.net-mvcsystem.componentmodel

Using System.ComponentModel.DisplayName with dynamic values?


Im trying to implement multi-language support in my system, the other systems at work uses xmlfiles for this generated from a database that they have used for some time now so they want me to use this aswell.

I have managed to translate everything except the displaynames in my formmodels, these values can apperantly only be constant values so i can't use a method that gets the correct translation.

This is how the code is now:

[System.ComponentModel.DisplayName("Kontraktnamn")]
public string Name { get; set; }

And i want to do something like this:

[System.ComponentModel.DisplayName(GetTextByKey("Contract_Name"))]
public string Name { get; set; }

Is it possible to work around this? Or maybe there is a better way to do it and still use the xmlfiles?


Solution

  • You'll need to create your own custom attribute that can read the xml values:

    public class CustomDisplayName : DisplayNameAttribute
    {
        public CustomDisplayName()
        {
            this.DisplayName = MyXmlReader.Read(DisplayName);
        }
    }