Skip to content

Creating Display Resource

Michal Altair Valášek edited this page Sep 9, 2018 · 2 revisions

Create the Resource File

It's common (although not required) to put resource files into a Resources folder and namespace. So start by creating this folder. Then create resource file (.resx.) within this folder and give it a name, ie. Display.resx.

Important: Set code generation to Public, otherwise the resource would not be accessible to the provider.

This will be your base language. Add multiple translations in the standard .NET fashion by creating additional resource files with language codes in name, ie. Display.en-US.resx for English (United States) and Display.cs-CZ.resx for Čeština (Česká republika).

Key naming conventions

The display provider is based on conventions and tries to find metadata based on the following naming scheme:

[[Namespace_]TypeName_]PropertyName[_MetadataType]

It's actually simple. Let's say you want to display Description of property with full name is Company.Project.Pages.IndexModel+Input.FullName. The provider will try to get values of the following keys, stopping on the first where it succeeds:

  • Company_Project_Pages_IndexModel_Input_FullName_Description
  • Project_Pages_IndexModel_Input_FullName_Description
  • Pages_IndexModel_Input_FullName_Description
  • IndexModel_Input_FullName_Description
  • Input_FullName_Description
  • FullName_Description

Supported metadata types are:

Suffix Name Comment
Name Display name Can be omitted
Description Description
Placeholder Placeholder
Null Null display text
DisplayFormat Display format string
EditFormat Edit format string

In typical usage, you have the metadata based on property names and the properties are repeated across different viewmodels. So your typical resource files would have keys named only by properties. In rare cases where the property names will clash, you can use more specific key names. See for example the following resource file:

Name Value
Title Title
Title_Placeholder ie. Mr. or Mrs.
BookModel_Title_Placeholder Book title

Generally the Title property would hold a title of courtesy across the application and the placeholder will hint accordingly. But there is one exception: in the BookModel class the Title property means the book title and the above resource structure will take care of it. Also note, that the key Title above does not have the _Name suffix, which is optional.

Clone this wiki locally