![]() These are just a few of the options available. NET applications.ĭata Annotations allow you to configure class models and add various attributes that specify how the property it is applied to should be treated e.g. ModelMetadataProviders.This tutorial requires some previous knowledge of the Data Annotations Namespace, along with. ![]() Metadata.DisplayName = TranslateFunction(displayAttribute.Name) Īdd the translation key to the prop Īdd this to application start up protected void Application_Start(object sender, EventArgs e) Translate using the key you provided before however you like Var displayAttribute = attributes.OfType().FirstOrDefault() Do what ever you want here to translate either by the property name or the display attribute key Var metadata = new ModelMetadata(this, containerType, modelAccessor, modelType, propertyName) Type containerType, Func modelAccessor, Type modelType, string propertyName) Protected override ModelMetadata CreateMetadata(IEnumerable attributes, This does not cover the pulling out data from the resource file, but you can either declare attribute on a property and use the full name as the key or give a static string as the key to use later in the meta data provider.Īdd your own meta data providor public class MyMetadataProvider : DataAnnotationsModelMetadataProvider Hopefully this helps you as in the past I have used this method to translate keys in a database. I got a lot of important information from the Microsoft reference about the NamedArguments, here: String localizedAttribute = decorationResx.GetString(name) And finally, from our resource file, we get our localized display name Var decorationResx = new ComponentResourceManager(resourceType) * Now, having the resource file from the decoration, we just create an instance to Var resourceType = (customAttributeResourceType != null) ? (Type) : null Var customAttributeResourceType = (n => n.MemberName = "ResourceType") Var name = (customAttributeName != null) ? (string) : null Var customAttributeName = (n => n.MemberName = "Name") * attribures are those members, Name and ResourceType: */ * and "ResourceType = typeof(MyResx))", so, what we are looking for from our custom * If we pay attention to our decoration, we defined "Name = nameof(MyResx.MyProperty)" Var customAttributes = CustomAttributeData.GetCustomAttributes(propertyMetadata).FirstOrDefault() * attributes from our property metadata: */ * The decorations we used, are "Custom Attributes". ![]() MemberInfo propertyMetadata = classType.GetProperty(nameOfTheProperty) * property metadata, where is the information we are looking for. * Now we get the MemberInfo of our property, wich allow us to get the ![]() The solution // We start with the class type, and the property name on a string The resource file MyResx, has some localized string for the name MyProperty, and will look like this: We have the class M圜lass, which has a property called MyProperty, and which will be localized with the resource file MyResx: public class M圜lass Then, letting a clear context, what we have is just a class (from which we can extract its type), and a PropertyName on a string, and what we want is the the localized DisplayName of that property of that class, according to a Resource File assigned on its decoration. I've been looking in CustomAttributes, but I only could get display name attributes, and some types, and that lands me in another job, which is invoke the resource file to get the value of a name. To be more clear: string localizedPropertyName = typeof(M圜lassName).GetProperty(myPropertyName).SomeMagic() where localizedPropertyName would be "Some property localized" In this case, what I need, is the localized name assigned to that property, according to the resource file. I'm working with a kind of dynamic mapping, where I use the property names of my classes, and in general I get them with something like this: string propertyName = typeof(M圜lassName).GetProperty(myPropertyName).Name In my ResxFile, for the previous example, I would have something like: Name | ValueĪnd in this way, for example, I can bind my class to a grid, and the column names will be localized according to the content of the resource file. resx file, and I'm using Name = nameof(ResxFile.SomeProperty) to get the name property of the resource file row (to make it strongly typed), and ResourceType = typeof(ResxFile) to indicate which is the resource file to use. I have a class, which has properties localized through data annotations by a resource file, like this: ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |