How to: add design-time multi-language support for component Properties

Source: Internet
Author: User

The design-time multi-language support for component attributes enables your component to display the corresponding language version description for the IDE of different language versions when using it. For example, many professional components have such attributes. The attribute descriptions you see in the Chinese version of. Net studio are in Chinese, while those in Japanese are in Japanese.

How can I add design-time multi-language support when writing. NET component?

Anyway, "imitation is the sincerest form of flattery". Let's see how Microsoft Works.



The following is the pseudo checkbox in the Microsoft Class Library: Code Segment analysis,

Public class checkbox

{

....

// Properties

[ Srdescription ("Checkboxautocheckdescrkey ") ]

Public bool autocheck {Get; set ;}

[ Srdescription ("Checkboxcheckaligndescrkey ") ]

Public contentalignment checkalign {Get; set ;}

[ Srdescription("Checkboxcheckeddescrkey")]

Public bool checked {Get; set ;}

...

}

using the reflection mechanism,. Net studio ide obtains the description string from the srdescription declared in component properties and displays it in the Properties window.
srdescription is an instance of the srdescriptionattribute class. Srdescriptionattribute inherits descriptionattribute and provides the implementation of localized description -- Public override string description {Get;}. The pseudo code is as follows:
// the declamation of the class srdescriptionattribute
class srdescriptionattribute: descriptionattribute
{< br>
// constructor descripton key specified the key of the resouce string.
Public srdescriptionattribute (string descriptionkey);

//Return the localized description string, according to The description_key get the description from the Assembly Resource source.

Public override stringDescription{Get ;}

...

}



To obtain multi-language support Description The corresponding description string can be obtained according to different languages.



So what should we do next?



First, create our own resourcedescriptionattribute through inheritance from descriptionattribute. Because this class is used in design, we can mark it Internal To prevent unauthorized access.



Rewrite the description implementation to obtain different description strings based on different languages. Generally, the resource file corresponding to the default language type can be embedded in the main assembly of the component as the fall back resource, the resource files in other languages are compiled independently into another resource Assembly dedicated to providing localized resources. The description implementation implements a small process, and searches for the resource Assembly based on the current language version. If the resources in the corresponding language of the current environment are not found, or the resource assemly cannot be read, the fall back resource file of main assembly should be used. This is also called the fall back resource file. (Fall back has some backup meanings ).

Add a resource file. Add different description strings for different component properties. Note: resource files in different languages must maintain key consistency.

for example, the famous crystal report is implemented in this way. Check the local GAC. You should find the two assemblies, crystaldecisions. Web. dll and crystaldecisions. Web. Resources. dll. Among them, crystaldecisions. web. DLL is the main assembly mentioned above, which contains the component implementation under the web form, and the resource file of the default language, that is, the fall back resource, crystaldecisions. web. resources. DLL is the resource assembly and provides the corresponding localized resource files.

of course, this is a very professional practice. If your component does not need to emphasize support for international localization, simply use descriptionattribute describes the hard code of to the class implementation. After all, before the change arrives, any advance design should be assumed as excessive .
example:
[ description ("hard code your description here ") ]
public image myimage {
get {
// insert code here.
return image1;

}< br>
set {
// insert code here.

}

For more information about how to support localization based on operating resources and resources, see msdn.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.