Add to My Yahoo! | Google Reader or Homepage | Add to Windows Live | Add to Windows Live Alerts

Wictor Wilén

SharePoint Server MVP / Author / MCT / MCTS / MCP / MSc writing about SharePoint and other interesting Microsoft technologies

Web Part Properties - part 5 - localization

Posted at 2009-02-02 05:59 by Wictor Wilén in SharePoint , Web Parts with 8 comments.

If you are building a reusable web part that you would like to sell or give away you should look into localizing your web part. The localization support is great with .NET 2.0, using resource files, and there are tons of built-in functionality for this. If you are creating an Editor Part for you web part, then your life is pretty easy, just use the standard techniques. But if you are using the approach by tagging the properties of your web part with the Personalizable attributes, then you are out of luck. Take a look at this property for example:

  1: [WebBrowsable]
  2: [Category("Look and feel")]
  3: [WebDisplayName("Use custom palette")]
  4: [WebDescription("Check this to use a custom palette")]
  5: [Personalizable(PersonalizationScope.Shared)]
  6: public bool UseCustomPalette {
  7:     get;
  8:     set;
  9: }
 10: 

All these attribute values are hardcoded into you assembly, and you have to build different assemblies for different languages.

Fortunately the .NET framework is extensible so you can extend these attributes (Category, WebDisplayName and WebDescription) so they take advantage of the localization features.

All we have to do is to implement three classes: LocalizedCategoryAttribute, LocalizedWebDisplayNameAttribute and LocalizedWebDescriptionAttribute.

They basically look the same and here is the code for LocalizedWebDisplayNameAttribute:

  1: [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
  2: public sealed class LocalizedWebDisplayNameAttribute
  3:     : WebDisplayNameAttribute {
  4: 
  5:     bool m_isLocalized ;
  6: 
  7:     public LocalizedWebDisplayNameAttribute(string displayName)
  8:         : base(displayName) {
  9:     }
 10: 
 11:     public override string DisplayName {
 12:         get {
 13:             if (!m_isLocalized) {
 14:                 this.DisplayNameValue = 
 15:                     Resources.ResourceManager.GetString(
 16:                         base.DisplayName, Resources.Culture);
 17:                 m_isLocalized = true;
 18:             }
 19:             return base.DisplayName;
 20:         }
 21:     }
 22: }
 23: 

What we do here is to override the DisplayName property and instead of just returning the value, we use the ResourceManager object and retrieve the value from our localized resources.

After this we can change our original property code to something like this:

  1: [WebBrowsable]
  2: [LocalizedCategory("LookAndFeel")]
  3: [LocalizedWebDisplayName("UseCustomPalette")]
  4: [LocalizedWebDescription("UseCustomPaletteDesc")]
  5: [Personalizable(PersonalizationScope.Shared)]
  6: public bool UseCustomPalette {
  7:     get;
  8:     set;
  9: }

To get this to work just add the three attribute values (LookAndFeel, UseCustomPalette, UseCustomPaletteDesc in the sample) to our resource file and you are set and done.

Comments and trackbacks

#  stack overflow by Augustin Calin
Screenshot from websnpr You will get a stack overflow exception because in derived attribute class you try to return this.DisplayName (line #19); it should be base.DisplayName.
#  Re: stack overflow by Wictor
Screenshot from websnpr You're absolute correct. I fixed the typo in the post. Thanks!
#  inspiring by software development uk
Screenshot from websnpr That was an inspiring post, web part aare so better than the old technics Keep up the good work
#  Re: inspiring by Wictor
Screenshot from websnpr Thank you very much!
#  LocalizedCategoryAttribute... by Pat
Screenshot from websnpr Just a quick question... Under the System.Web.UI.WebControls.WebParts class, i have been able to find the LocalizedWebDisplayNameAttribute and the WebDescriptionAttribute class.... But i'm not able to find the categoryattribute class.... Can you tell me what is the class name i need implement to override the category name in a webpart.... Best regards,
#  Category Attribute by Paul
Screenshot from websnpr The CategoryAttribute class is part of the System.ComponentModel Namespace.
#  Greatly appreciated! by Chritopher
Screenshot from websnpr This helped me understand this process tremendously! Great site! :)
#  @Christopher by Wictor
Screenshot from websnpr Thanks, you're welcome
Make a comment on this post:
Subject:  

Your name:  
Your Url:  
Note: submissions may have to be approved before being visible, so don't submit your comment multiple times.
Real Time Web Analytics