I like using resources for text, labels, link and button text etc. in my projects. It provides a consistency of UI, which is key when you are trying to sell a system. It also means I can translate a site to another language without separate pages and all the maintenance that would bring.
One thing that I have come up against is no fall-back or cascade, using the traditional approach, in that, I would like to have a resource which is standard, it would be used if no specific one was available.
For example...
I have a resource file located in my general assembly Antix.Web.PageResources, it has a property for Submit_Save.
<input type="submit" value="@PageResources.Submit_Save" />
If I use the code above then I get the general value for a submit button which saves data to the database "Save". But say I get a client who doesn't want the button to say "Save" and wants it to say "Commit Changes". I cannot change my general assembly as it is used in other projects.. oh no..
What I need is to be able to have a project level resource file which overrides the one in my general assembly, and if the property I am after exists in the overriding resource it is used, if not the one in the general assembly resource is used - Cascading Resources.
This is possible in MVC by virtue of the view model type, an expression target property interface, and a simple extension to retrieve the resource value.
The extension signature makes use of the model type by extending the HtmlHelper<TModel>
public static string PageResourceFor<TModel, TValue>( this HtmlHelper<TModel> htmlHelper, Expression<Func<IPageResource, TValue>> expression)
The expression is used to get the resource you want to display, targeting an interface which has a set of properties one for each resource.
public interface IPageResource {
string SubmitSave{ get; }
}This allows me to use the following call in my view
<input type="submit" value="@Html.PageResourceFor(p => p.SubmitSave)" />
And all the information that my extension needs to provide a cascaded resource value is available.
for example then...
public static string PageResourceFor<TModel, TValue>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<IPageResource, TValue>> expression) {
var resourceName = ExpressionHelper.GetExpressionText(expression);
switch(typeof(TModel).Name) {
case "Person":
if (PersonResources.ResourceManager.GetString(resourceName) != null)
return PersonResources.ResourceManager.GetString(resourceName);
break;
}
return SiteResources.ResourceManager.GetString(resourceName)
?? GeneralPageResources.ResourceManager.GetString(resourceName);
}You may employ a more sophisticate way of getting your model resources, but this shows the cascade nicely.
Firstly I have only set the General Resource

and the page renders like this

Then I set the resource for the site to 'Commit Changes'

and the page renders like this...

Finaly I set the resource for the model to 'Commit Person Changes'

and, yay, the page renders like this...

Magic :)
| < | May 2012 | |||||
|---|---|---|---|---|---|---|
| S | M | T | W | T | F | S |
| 29 | 30 | 1 | 2 | 3 | 4 | 5 |
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 | 29 | 30 | 31 | 1 | 2 |
Add-ins AJAX ASP.NET MVC Browsers C# Caching CodeDom Compression CORS CSS CV Data Database DependencyResolver Development Dynamic Entity Framework Error Handling Extend File Upload Forms GDI+ HTML HTML Editor HTTP Interfaces JavaScript JQuery MCE MetadataProvider MSBuild Numbers Objects Patterns Progressive Enhancement Projects Publish Regex Resources Security SEO SMTP Source Control Strings Sub-Collections TDD Templates Tools Twitter User Interface WCF Web Development WHS WMC XLinq XML
15/05/2012
WindowsAzure
Announcing the MEET Windows Azure Event! Streamed online June 7th. Register at http://t.co/bObzTAuL #MEETAzure #WindowsAzure
10/05/2012
kevinwhinnery
Comparing Titanium and PhoneGap - A common question I get asked at developer events and conferences is how... http://t.co/Zq2eND6B
09/05/2012
brianleroux
PhoneGap goals and philosophy: http://t.co/wkq8wI2T
just now
cbtacy
"Meet Windows Azure" http://t.co/cDE3s5E0 (this should be VERY interesting)
just now
jodonnel
RT @brucedkyle: Join in the ‘Meet Windows Azure’ Event Online June 7 http://t.co/kXxxSOcy #MSDEV