DD4T2 - Resource Management (.NET)

I recently had to implement a feature on SDL Web 8.5 for managing resources and found out that DD4T 2 already have this available out of the box. It’s called Dynamic Resource Management.

Unfortunately, there’s limited documentation so I thought I’d write about my experience in order to remind myself in the future and help those who may need it too.

Setup

Create a page template in Template Builder. Add DD4T’s Resources TBB and ensure that the file extension of the page is set to ‘xml’.

Create a component template using Template Builder which has a default Cleanup TBB. Ensure the template is linked to the Labels schema.

Label Schema

Create the component with a sample label using the same schema as the screenshot above and add to a page using the page template and publish.

Web App

In your web app, add an app setting:

<appSettings>
    <add key="DD4T.ResourcePath" value="/filepath/us/en/_system/labels.xml" />
</appSettings>

The value is the filename published to the broker database.

Create an extension method:

public static string Resource(this HtmlHelper htmlHelper, string resourceName)
{
    return (string)Resource(htmlHelper.ViewContext.HttpContext, resourceName);
}
 
public static string Resource(this HtmlHelper htmlHelper, string resourceName)
{
    return (string) Resource(htmlHelper.ViewContext.HttpContext, resourceName);
}
 
public static object Resource(this HttpContextBase httpContext, string resourceName)
{
    return httpContext.GetGlobalResourceObject(CultureInfo.CurrentUICulture.ToString();
}

You can then use these in your views:

@Html.Resource("Label_Key")

This post is heavily influenced by Rob’s post here.