Displayname attribute from resource file




















For the full localization tutorial see this page. Please observe Gunders comment below. DisplayName attribute from Resources? Question I have a localized application, and I am wondering if it is possible to have the DisplayName for a certain model property set from a Resource. GetProperty base. DisplayName, BindingFlags. Active Oldest Votes.

Improve this answer. This is good for the specific example used here but it won't work for the majority of dynamic property setters that want strings. This is good when we have all our locale with us before deploying to production. But if I want to call db for db strings then this kind of approach is not proper. Also the disadvantage of resource file, is that it requires compilation again to effect the changes, that means you need to release another version to client.

I am not saying this as a bad approach, please dont feel like that — kbvishnu. Just a problem : we have to know resource type in the model. I have a model DLL and a website in two distinct projects. I'd like to be able to set display names in model and set resource type in website Get Resharper, and create the resource file in your project, and it will automatically remind and then help you to move the Name into the resource file. Show 4 more comments.

Jason 4, 5 5 gold badges 29 29 silver badges 39 39 bronze badges. Darin Dimitrov Darin Dimitrov k gold badges silver badges bronze badges. Yes, I worked on a similar solution this morning and it it feasible.

Then I found this post which has the same approach: adamyan. Gunder his post, below this one with the most votes , is a much nicer solution. Just for people who only read the Accepted posts — X.

This actually does NOT work for accessing different translations since it will return the same value for all users no mater what. Store resourceid in a local variable and override DisplayName instead — Fischer. Resources ] as describe below Show 6 more comments. Tikall Tikall 1, 19 19 silver badges 11 11 bronze badges. Doing this will make the resource file to get picked automatically when the current UI thread culture is set to culture code ta-IN. Refer here to know the list of language-country code.

The Tamil resource file will look like below. When executed, you can see the title changed to Tamil when the current UI thread culture is set to Tamil. UserLanguages[0]; Thread. GetCultureInfo culture ; Thread. Click Choose.. Add the preferred language Tamil in this article from the dropdown and use the Move Up button to make it as first preference.

Articles Latest All Articles Asp. Net Core Asp. Net MVC. Download the source and see it in action. New to ASP. Top 5 programming languages commonly used by online gaming developers. Your comment. Suppose, If two different views are sharing the same model for instance, maybe one is for mobile output and one is regular , it could be nice to have the string reside in a single place: as metadata on the ViewModel.

Additionally, if you had an inherited version of the model that necessitated a different display, it could be useful. For instance:. Display attribute should be preferred over DisplayName attribute. The former one comes form DataAnnotations namespace and exposes more properties than the later one, ie. Most importantly, DisplayName accepts only literals, while Display accepts resource type and key, which can be used to provide translations using separate resource files resx.

The correct way of using DisplayName is on properties with getters and setters because by design MVC Binding applies to properties, not fields. Like Liked by 1 person. Like Like. You are commenting using your WordPress.



0コメント

  • 1000 / 1000