0

I have written custom attribute for DisplayName as follows.

namespace CRM.Model
{
    public class LocalizedDisplayNameAttribute : DisplayNameAttribute
    {
        private readonly string resourcekey;

        public LocalizedDisplayNameAttribute(string resourceId)
            : base(GetMessageFromResource(resourceId))
        { 

        }

        public static string GetMessageFromResource(string resourceId)
        {
            // My Localization logic
            return LocalizationResourceProvider.Current.GetString(resourceId);
        }       
    }
}

When model is loaded/initiated for the first time the above custom attribute class is called successfully.

But if, I will post back the page then, it doesn't get called.

For example when I change my language from English to French. it still displays the English content.

Why it is so ? How can I solve this issue ? Can we load the Model data explicitly

Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
David
  • 45
  • 7
  • Yo do realise there is a localised System.ComponentModel.DataAnnotations.DisplayAttribute which can be used [Display(ResourceType = typeof(yourResxClass), Name = "ResourceName")] . – AlexC Oct 16 '14 at 14:09
  • Can you please give me some example of this ? I dont want to directly access Resources.Resources. it should get called from my custom logic – David Oct 16 '14 at 16:03

2 Answers2

0

Well, it is setting the language in the constructor. That is why it only works when the model is loaded / initiated for the first time. Not sure how the rest of your solution works, but you need to run the localization logic in other places as well.

beautifulcoder
  • 10,832
  • 3
  • 19
  • 29
0

Try to register your attribute on application start up, see similar thread.

Community
  • 1
  • 1
Venkatesh
  • 1,204
  • 1
  • 10
  • 18