localization - Changing the default ModelState error messages in ASP.NET MVC 3 -
i have resource files in separate assembly myapp.resources.dll. can use resources without problem issue appears when want change (localize) default validation messages:
"the {0} field required." , "the value '{0}' not valid {1}."
the solution defaultmodelbinder.resourceclasskey = "myapp.resources.global";
not work because requires resourceclasskey under app_globalresources folder in web project.
what should fix me ?
regards
i have found solution case (when resources in separate assembly).
to working should create custom resourceproviderfactory , register default resourceproviderfactorytype in <globalization>
web.config section.
setup localization
// modify web.config in run-time , setup custom resourceproviderfactory var globalization = webconfigurationmanager.getsection("system.web/globalization") globalizationsection; var readonlyfield = typeof(configurationelement).getfield("_breadonly", bindingflags.instance | bindingflags.nonpublic); readonlyfield.setvalue(globalization, false); globalization.resourceproviderfactorytype = typeof(validationresourceproviderfactory).fullname; var resourcesclass = typeof(validationresources).fullname; defaultmodelbinder.resourceclasskey = resourcesclass; validationextensions.resourceclasskey = resourcesclass;
validationresourceproviderfactory
public sealed class validationresourceproviderfactory: system.web.compilation.resourceproviderfactory { public validationresourceproviderfactory() { } public override iresourceprovider createglobalresourceprovider(string classkey) { return new globalresourceprovider(classkey); } public override iresourceprovider createlocalresourceprovider(string virtualpath) { throw new notimplementedexception("local resources not supported yet"); } }
globalresourceprovider
public class globalresourceprovider : iresourceprovider { public globalresourceprovider(string classkey) { throw.ifbadargument(() => string.isnullorempty(classkey), "classkey"); var type = type.gettype(classkey, false); if (type == null) { var asmname = classkey; var classname = classkey; while(asmname.indexof(".") > -1 && type == null) { asmname = asmname.substring (0, asmname.lastindexof(".")); classname = classkey.substring(asmname.length + 1); type = type.gettype(classkey + "," + asmname, false); } } throw.ifnullargument(type, "type"); manager = createresourcemanager(classkey, type.assembly); } public resourcemanager manager { get; set; } #region iresourceprovider implementation public iresourcereader resourcereader { get; set; } public object getobject(string resourcekey, cultureinfo culture) { return manager.getobject(resourcekey, culture); } #endregion private resourcemanager createresourcemanager(string classkey, assembly assembly) { return new resourcemanager(classkey, assembly); } }
upd
resx validationresources
just add new resources class validationresources , place provided xml there
<?xml version="1.0" encoding="utf-8"?> <root> <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <!-- schema definited removed --> </xsd:schema> <resheader name="resmimetype"> <value>text/microsoft-resx</value> </resheader> <resheader name="version"> <value>2.0</value> </resheader> <resheader name="reader"> <value>system.resources.resxresourcereader, system.windows.forms, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089</value> </resheader> <resheader name="writer"> <value>system.resources.resxresourcewriter, system.windows.forms, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089</value> </resheader> <data name="accept" xml:space="preserve"> <value>please enter value valid mimetype.</value> </data> <data name="creditcard" xml:space="preserve"> <value>please enter valid credit card number.</value> </data> <data name="date" xml:space="preserve"> <value>please enter valid date.</value> </data> <data name="dateiso" xml:space="preserve"> <value>please enter valid date (iso).</value> </data> <data name="datetime" xml:space="preserve"> <value>please enter valid date , time.</value> </data> <data name="digits" xml:space="preserve"> <value>please enter digits.</value> </data> <data name="email" xml:space="preserve"> <value>please enter valid email address.</value> </data> <data name="equalto" xml:space="preserve"> <value>please enter same value again.</value> </data> <data name="fieldmustbedate" xml:space="preserve"> <value>please enter valid date "{0}".</value> <comment>localization legacy mvc clientdatatypemodelvalidatorprovider</comment> </data> <data name="fieldmustbenumeric" xml:space="preserve"> <value>please enter valid number "{0}".</value> <comment>localization legacy mvc clientdatatypemodelvalidatorprovider</comment> </data> <data name="invalidpropertyvalue" xml:space="preserve"> <value>invalid property value: {0}</value> </data> <data name="max" xml:space="preserve"> <value>please enter value less or equal {0}.</value> </data> <data name="maxlength" xml:space="preserve"> <value>please enter no more {0} characters.</value> </data> <data name="min" xml:space="preserve"> <value>please enter value greater or equal {0}.</value> </data> <data name="minlength" xml:space="preserve"> <value>please enter @ least {0} characters.</value> </data> <data name="number" xml:space="preserve"> <value>please enter valid number.</value> </data> <data name="propertyvalueinvalid" xml:space="preserve"> <value>the value "{0}" invalid property "{1}"</value> <comment>localization legacy mvc defaultmodelbinder</comment> </data> <data name="propertyvaluerequired" xml:space="preserve"> <value>the "{0}" field required.</value> <comment>localization legacy mvc defaultmodelbinder</comment> </data> <data name="range" xml:space="preserve"> <value>please enter value between {1} , {2}.</value> </data> <data name="rangeclient" xml:space="preserve"> <value>please enter value between {0} , {1}.</value> </data> <data name="rangelength" xml:space="preserve"> <value>please enter value between {0} , {1} characters long.</value> </data> <data name="remote" xml:space="preserve"> <value>please fix field.</value> </data> <data name="signedint" xml:space="preserve"> <value>please enter integer value, sign allowed.</value> </data> <data name="time" xml:space="preserve"> <value>please enter valid time.</value> </data> <data name="unsignedint" xml:space="preserve"> <value>please enter positive integer value.</value> </data> <data name="url" xml:space="preserve"> <value>please enter valid url.</value> </data> <data name="valuenull" xml:space="preserve"> <value><null></value> </data> </root>
Comments
Post a Comment