Click here to Skip to main content
15,885,914 members
Articles / Programming Languages / C#

Globalized Property Grid - Revisited

Rate me:
Please Sign up or sign in to vote.
4.73/5 (36 votes)
14 Jun 20036 min read 173.8K   2.4K   75  
Localization of category names in a globalized property grid control and considers inheritance issues
This article is an incremental version of its base article Globalized property grid. It discusses the localization of category names and localized property names in case of inheritance.
using System;
using System.ComponentModel;
using System.Globalization;
using System.Resources;
using System.Reflection;

namespace GlobalizedPropertyGrid
{
	// This attribute may be added to properties to control the resource table name and 
	// keys for accesging the property name and description. 
	[AttributeUsage(AttributeTargets.Property, AllowMultiple=false, Inherited=true)]
	public class GlobalizedPropertyAttribute : Attribute
	{
		private String resourceName = "";
		private String resourceDescription = "";
		private String resourceTable = "";

		public GlobalizedPropertyAttribute(String name)
		{
			resourceName = name;
		}

		// The key for a property name into the resource table.
		// If empty the property name will be used by default.
		public String Name
		{
			get {  return resourceName;  }
			set {  resourceName = value;  }
		}

		// The key for a property description into the resource table.
		// If empty the property name appended by 'Description' will be used by default.
		public String Description
		{
			get {  return resourceDescription;  }
			set {  resourceDescription = value;  }
		}

		// The name fully qualified name of the resource table to use, f.ex.
		// "GlobalizedPropertyGrid.Person".
		public String Table
		{
			get { return resourceTable;  }
			set { resourceTable = value; }
		}
	}

	// This attribute may be added to properties to control the resource table name and 
	// keys for accesging the property name and description. 
	[AttributeUsage(AttributeTargets.Class, AllowMultiple=false, Inherited=true)]
	public class GlobalizedObjectAttribute : Attribute
	{
		private String resourceTable = "";

		public GlobalizedObjectAttribute(String name)
		{
			resourceTable = name;
		}

		// resource table name to use for the class. 
		// If this string is empty the name of the class will be used by default. 
		public String Table
		{
			get {  return resourceTable;  }
		}
	}

	// This attribute class extends the CategoryAttribute class from the .NET framework
	// to support localization.
	[AttributeUsage(AttributeTargets.Property, AllowMultiple=false, Inherited=true)]
	class GlobalizedCategoryAttribute : CategoryAttribute
	{
		string table = "";

		public GlobalizedCategoryAttribute() : base() {}
		public GlobalizedCategoryAttribute(string name) : base(name) {}

		// The name fully qualified name of the resource table to use, f.ex.
		// "GlobalizedPropertyGrid.Person".
		public string TableName
		{
			set { table = value; }
			get { return table; }
		}

		// This method will be called by the component framework to get a localized
		// string. Note: Only called once on first access.
		protected override string GetLocalizedString( string value )
		{
			string baseStr = base.GetLocalizedString( value );

			// Now use table name and display name id to access the resources.  
			ResourceManager rm = new ResourceManager(table, Assembly.GetExecutingAssembly() );

			// Get the string from the resources.
			try
			{
				return rm.GetString(value);
			}
			catch( Exception )
			{
				return value;
			}
		}
	}


}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Web Developer
Germany Germany
For ten years I worked as a senior consultant, coach, lead architect and project lead for several consulting companies.
Currently I work as a system architect for Zuehlke Engineering GmbH based in Frankfurt.
You can find a detailed resume here.

Comments and Discussions