|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionIn my article 'Globalized property grid' I demonstrated how to localize the property names of an object. This article is based on this article incrementally. Corresponding to questions I would like to address following topics here:
Localizing category names were left out in my previous article although it is as much of interest. However, localization support of category names is limited as you will see below. Another question I want to answer is regarding inheritance of objects. Although I answered the question on my previous article, I still want to address it here. Let's start with category names first. Category namesA property grid is able to organize the properties of an object in categories. You can assign a category to the properties of your objects by using an attribute of type Here is an example how to assign the property [Category("Required Data")]
public string LastName { get {...}; set {...}; }
If no category attribute is assigned to a property, then a property will be assigned to a default category that you can find in a static property called
If you use category names in a globalized property grid then it is wishful to have the category names localized, too. Localizing category namesThere is support included to localize a category name. You cannot override the [AttributeUsage(AttributeTargets.Property, AllowMultiple=false,
Inherited=true)]
class GlobalizedCategoryAttribute : CategoryAttribute
{
string table = "";
...
protected override string GetLocalizedString( string value )
{
string baseStr = base.GetLocalizedString( value );
// Now use resource table name 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;
}
}
}
Use this class to decorate a property: [GlobalizedCategory("Required")]
public string LastName { get {...}; set {...}; }
But there is a big drawback in the implementation of
I don't know if the behaviour of the Let's get over to the second topic to address. Inheritance of globalized objectsIn the sample of my previous article (Globalized property grid) I just used one class called The answer is relatively simple. By default, the class name will be used to access a localized resource. That means, The person class gets its localized names from If you want to use one resource file only, you have to tell it explicitly. Here two options are possible. Using the // Use the provided resource table instead of the default one which // would be determined by the class name. The first parameter is // the key of the property in the string table. Leaving it empty // indicates to use the property name by default. [GlobalizedProperty("",Table="GlobalizedPropertyGrid.Person")] public string Department { get { return department; } set { department = value; } } Alternatively, an attribute class may be additional used to define the resource table name on the class level. This is demonstrated by a class [GlobalizedObjectAttribute("GlobalizedPropertyGrid.SpecialStringTable")] public class Employee : Person { ... The class
Creating a sample projectFor demonstration purpose I have extended the sample project. For a detailed description of the sample project refer to the previous article (Globalized property grid). There are only some modifications and additions: Demonstrating localization of categoriesI have added an attribute class called
Selecting a language (e.g. English) at this time works for localized category names. The category and property names will be displayed in the selected language in the property grid on the main form.
Switching the current language (e.g. to German) by pressing the button in the upper right corner on the main form doesn't affect the category names. Property names have changed correctly.
Maybe this limited behaviour of the Demonstrating localization of properties in case of inheritanceI have added an attribute class called I also have added a class // Employee.cs /// <SUMMARY> /// Employee derives from Person and so is a globalized object implicitly. /// </SUMMARY> public class Employee : Person { private string department = ""; public Employee() { } public string Department { get { return department; } set { department = value; } } } This class will be instantiated by the // Instantiate test class and set some data Employee emp = new Employee(); emp.FirstName = "Max"; emp.LastName = "Headroom"; emp.Age = 42; emp.Department = "Channel5"; this.person = emp;
SummaryThis 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. To localize the category names there are two steps necessary:
But note: Supporting inherited classes based on the sample provided requires to either define a resource string table for each class with the name of the class or referring to a certain resource table by explicit usage of custom attributes: References
|
||||||||||||||||||||||