ResourceSet ValueConverters - RickStrahl/Westwind.Globalization GitHub Wiki
Using ResourceSet ValueConverters allows you to transform resource values just before they are written into a ResourceSet for serving as a resource by a ResourceManager, ResourceProvider, DbRes or strongly typed resources. Transformations allow you to enter resource text in an encoded format that can then be turned into a final format for serving the data in your application. An example of a converter is a Markdown converter that allows you to enter text as markdown, but converts the final resource text to HTML that can be embedded into the document.
This feature allows for opportunities beyond localization as it offers an easy way to add dynamic content to a site. For example, the MarkdownResourceSetValueConverter can be used to manage content served from a database as a simplistic CMS system.
How it works
The database resource table contains a ValueType
field that allows you to specify a numeric value type. The default value types are text (0), binary (1) and markdown (2), but they are represented as integer values and you can easily add your own values to represent data as a different value type. Currently only the markdown value type has a default action hooked up to it. Both text and binary are left as is.
To transform a ValueType you can use an IResourceSetValueConverter
implementation that can act on these value types by transforming the resource value, just before the resource value is written into a ResourceSet for serving by a ResourceManager, ResourceProvider, DbRes, or Strongly Typed Resources.
The idea is that you can plug in custom converters of your own that let you transform the entered resource content and turn it into some other format/text that is then stored in the ResourceSet resource dictionary that .NET uses to serve the resources to your application.
MarkDownResourceSetValueConverter - an Example
An example of a built in ResourceSetValueConverter is the MarkDownResourceSetConverter, which allows you to enter Markdown text for a resource value and have it automatically converted into HTML.
The MarkDown ResourceConverter uses a ValueType of 2 which in the database is simply an integer value.
You can then implement a ResourceSetValueConverter like this:
public class MarkdownResourceSetValueConverter : IResourceSetValueConverter
{
public int ValueType { get; set; }
public MarkdownResourceSetValueConverter()
{
ValueType = (int) ValueTypes.MarkDown; // 2
}
public object Convert(object resourceValue, string key)
{
if (resourceValue != null)
resourceValue = CommonMark.CommonMarkConverter.Convert(resourceValue as string);
return resourceValue;
}
}
The Value converter is called by DbResourceDataManager when it reads resources from disk and before it writes the final values into the ResourceSet resource dictionary. It checks through the list of value converters loaded, and if it finds one calls the Convert method on it.
The converter gets passed the value and key. In most cases you only need the value to convert universally, but in some cases you might require special conversion for a specific key or set of keys maybe those that have a certain pre or post-fix, such as .title .text etc.
Hooking up a custom ResourceSetValueConverter
You can hook up a custom converter in your application startup. In Web applications this will be in global.asax.cs:
DbResourceConfiguration.Current.ResourceSetValueConverters.Add(new MarkdownResourceSetValueConverter());
(note: this is not necessary - the MarkDown converter is automatically loaded but you can do this with your own custom converters)
ResourceSetValueConverters is a list so if you know you're not going to be using any MarkDown features you can simply Clear()
the list.
DbResourceConfiguration.Current.ResourceSetValueConverters.Clear();
DbResourceConfiguration.Current.ResourceSetValueConverters.Add(new MyCustomConverter());