ASP. net mvc Internationalization

Source: Internet
Author: User

 

This post in the blog garden is already comprehensive, but I think it is not elegant enough. It seems like a mess, and this old man's post abroad is neat and tidy, A simple record is as follows:

General international Processing Methods

 Public   Class User {[required (errormessageresourcename =" Validation_required ", Errormessageresourcetype = Typeof (Modeltranslations)] [localizeddisplaynameattribute (" User_id ", Nameresourcetype = Typeof (Modeltranslations)] Public   Int Id { Get ;Set ;} [Required (errormessageresourcename =" Validation_required ", Errormessageresourcetype = Typeof (Modeltranslations)] [stringlength (40, errormessageresourcename =" Validation_stringlength ", Errormessageresourcetype = Typeof (Modeltranslations)] [localizeddisplaynameattribute (" User_firstname ", Nameresourcetype = Typeof (Modeltranslations)] Public   String Firstname {Get ; Set ;} [Required (errormessageresourcename =" Validation_required ", Errormessageresourcetype = Typeof (Modeltranslations)] [stringlength (40, errormessageresourcename =" Validation_stringlength ", Errormessageresourcetype = Typeof (Modeltranslations)] [localizeddisplaynameattribute (" User_lastname ", Nameresourcetype = Typeof (Modeltranslations)] Public   String Lastname { Get ; Set ;}}

We can see that it is confusing.

 Public   Class User {[required] [localizeddisplaynameattribute (" User_id ")] Public   Int Id { Get ; Set ;} [Required] [stringlength (40)] [localizeddisplaynameattribute (" User_firstname ")] Public  String Firstname { Get ; Set ;} [Required] [stringlength (40)] [localizeddisplaynameattribute (" User_lastname ")] Public   String Lastname { Get ; Set ;}}

Is it much more neat, but the cost of cleanliness is to writeCodeOf

 Public   Class Requiredattriations: system. componentmodel. dataannotations. requiredattriations {Private   String _ Displayname; Public Requiredattribute () {errormessageresourcename =" Validation_required ";} Protected   Override Validationresult isvalid ( Object   Value , Validationcontext) {_ displayname = validationcontext. displayname; Return   Base . Isvalid ( Value , Validationcontext );} Public   Override   String Formaterrormessage ( String Name) {var MSG = Your ageservice. instance. Translate (errormessageresourcename ); Return   String . Format (MSG, _ displayname );}}

This is the implementation code for verifying properties.

Ublic Class Localizeddisplaynameattribute: displaynameattribute { Private Propertyinfo _ nameproperty; Private Type _ resourcetype;Public Localizeddisplaynameattribute ( String Classname, String Propertyname ): Base (Classname + (propertyname = Null ? " _ Class ":(" _ "+ Propertyname ))){} Public   Override   String Displayname { Get { Return Languageservice. instance. Translate ( Base . Displayname )?? " ** "+ Base . Displayname +" ** ";}}}

This is the implementation code for implementing the displayname attribute.

 Public   Class Languageservice { Private   Static Required ageservice _ instance = New Languageservice (); Private List <ResourceManager> _ resourcemanagers = New List <ResourceManager> (); Private Languageservice (){} Public   Static Languageservice instance { Get { Return _ Instance ;}} Public   Void Add (ResourceManager MGR) {_ resourcemanagers. Add (MGR );} Public   String Translate ( String Key ){ Foreach (VAR itemIn _ Resourcemanagers) {VaR Value = Item. getstring (key ); If ( Value ! = Null ) Return   Value ;} Return   Null ;}}

This is the code for managing resources dependent on the above class.

However, the above solution is still problematic for client verification. The following is a simple implementation:

 Public   Class Requiredattributeadapter: dataannotationsmodelvalidator <requiredattribute> { Public Requiredattributeadapter (modelmetadata metadata, controllercontext context, requiredattribute attribute ): Base (Metadata, context, attribute ){} Public   Static   Void Selfregister () {dataannotationsmodelvalidatorprovider. registeradapter ( Typeof (Requiredattribute ), Typeof (Requiredattributeadapter ));} Public  Override Ienumerable <modelclientvalidationrule> getclientvalidationrules (){ Return   New [] { New Modelclientvalidationrequiredrule (errormessage )};}}

Add a sentence to global. asax.

 
Requiredattributeadapter. selfregister ();

 

Technorati label: ASP. net mvc verification Internationalization

.

Related Article

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.