Tuesday, October 27, 2009

Groping In A Bus Injapan

Dot Net (C #): localized property grid; localized attributes

In today's time an application is usually implemented for several languages. The Microsoft. NET is not difficult to localize Windows applications. But again the question arises, how can localize attributes, especially those for the PropertyGrid (description, display text, category) .

since spawned my internet research on this subject hardly useful examples would nonetheless I am at a minimal example to:

need First, we have a class that is to be located, we take in this case, a "person"
  namespace  LocalizeableDescriptionPropertyGrid 
{
public class person
{
/ / Properties
public string FirstName {get;}
public string LastName {get;}

/ / ctor
public person ( string fName, string lname) {

this firstName = fName;.
this lastName = lname;.


} / / other methods ...
}}


would normally be the class now we follow with attributes provided:
 [description attribute ( "The first name of that person" )] {get;} 
public String firstName

[description attribute ( "The last name of that person" )]
public String lastName {get;}

This approach has the disadvantage that the attributes are not localized. Unfortunately, there is. NET is no way to locate problems such attributes as their information is read from the runtime once (via reflection) and not subsequently updated. With a small trick, but can at least ensure that it is in this Momemt the attributes the language to be adapted. This requires first, the localized text as usual to put in any Resource file .



Next, a class here, which allows an attribute to load its information from a resource file. I demonstrate the method on the example of Description attribute , it can be moved alternatively with the category and display text attribute:

Only a private field (or property), and the overriding of the Get-Property are notwenidg.
  public   class  LocalizedDescriptionAttribute: description {attribute 

/// <summary>
/// Contains the name of the resource-string
/// </summary>
private string rDescription;

/// <summary>
/// Creates a new LocalizedDescription Attribute instance
/// giving it the name of the resource-string
/// </summary>
/// <param name="description"></param>
public LocalizedDescriptionAttribute( string description)
{
this .rDescription = description;
}

/// <summary>
/// (Overridden) Get: fetching the description during runtime
/// from the Resources (with respect to the current culture)
/// </summary>
public override string Description
{
get
{
return Properties.Resources.ResourceManager.GetString(
this .rDescription,
Thread.CurrentThread.CurrentCulture
);
}
}
}

Next, it is necessary for the class to change the person and the use new attribute :
  namespace  LocalizeableDescriptionPropertyGrid 
{
public class person
{
/ / Properties
[LocalizedDescriptionAttribute ( "person first name" )]
public string FirstName {get;}
[LocalizedDescriptionAttribute ( "person last name" )]
public string LastName {get;}

/ / ctor
public person ( string fName, string lname)
{
this firstName = fName;.
this lastName = lname;.
}

/ / other methods ...
}}



A final class might look like this:
  namespace  LocalizeableDescriptionPropertyGrid 

{public class person

{/ / Properties
[LocalizedCategoryAttribute ( "PersonCatData" ),
LocalizedDescriptionAttribute( "PersonFirstNameDescription" ),
LocalitedDisplayTextAttribute( "PersonFirstNameDisplayText" )]
public String FirstName { get; set; }

[LocalizedCategoryAttribute( "PersonCatData" ),
LocalizedDescriptionAttribute( "PersonLastNameDescription" ),
LocalitedDisplayTextAttribute( "PersonLasttNameDisplayText" )]
public String LastName { get; set; }

// other properties

/ / ctor
public person ( string fName, string lname)
{
this firstName = fName;.
this lastName = lname;.
}

/ / other methods ...
}}


The entire sample you can download here: localizablepropgridcode.rar (14.6 KB)