Create custom resource classes

In Sitefinity, resource classes are the main localization structure. Resource classes are used to distribute labels among multiple projects or to enable translations of labels and text inside widgets or modules. With its modules, Sitefinity provides predefined resource classes. When you create custom widgets or specific functionality, you need to create your own resource class. The following article demonstrates how to create a new resource class and register it in Sitefinity.

Create custom resource class

To create a custom resource class, you add a class to your Sitefinity project. By convention, you must name the file with Resources suffix, for example, ProductsResources. It must inherit the Resourceclass, which can be found inside the Telerik.Sitefinity.Localization namespace. Use the following example:

C#
using Telerik.Sitefinity.Localization;
using Telerik.Sitefinity.Localization.Data;

namespace SitefinityWebApp
{
   [ObjectInfo("AmazonResources", ResourceClassId = "AmazonResources", Title = "AmazonResourcesTitle", TitlePlural = "AmazonResourcesTitlePlural", Description = "AmazonResourcesDescription")]
   public class AmazonResources : Resource
   {
       /// <summary>
       /// phrase: Amazon Search
       /// </summary>
       /// <value>Amazon Search</value>
       [ResourceEntry("AmazonSearchServiceTitle",
           Value = "Amazon Search",
           Description = "phrase: Amazon Search",
           LastModified = "2014/11/26")]
       public string AmazonSearchServiceTitle
       {
           get
           {
               return this["AmazonSearchServiceTitle"];
           }
       }
   }
}

Every resource is using the ResourceEntry attribute. The attribute binds the property name with a specific value, last modified date, and description.

NOTE: You can either add the resources to the class programmatically, using the above example, or you can add them using the UI after you register the resource class, as described below. After registration, the class becomes available under Administration» Labels & Messages

You can add as many resources to the class as you need.

Register the custom resource class

To use the newly created custom resource class, you must register it on application initialization. You do this is inside the Application_Start method of the Global.asax class. You must subscribe to the Bootstrapper.Bootstrapped event and add the following code inside:

C#
using System;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Localization;

namespace SitefinityWebApp
{
   public class Global : System.Web.HttpApplication
   {
       protected void Application_Start(object sender, EventArgs e)
       {
           Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped;
       }

       private void Bootstrapper_Bootstrapped(object sender, EventArgs e)
       {
           // Register any Resource classes
           Res.RegisterResource<AmazonResources>();
       }
   }
}

When you restart the application, the class is registered and becomes available under Administration » Labels & Messages. Whenever you add new resources, you can point to the newly created resource class.

NOTE: When a new resource is added or translated through the backend, a physical file is created in ~/App_Data/Sitefinity/GlobalResources folder. Make sure to deploy those files when you move your site from development to production.

Reference the labels from the resource class

You can reference the labels from the custom resource class both from WebForms and MVC widgets and controls. In addition, you can reference the labels through code behind.

WebForms

Reference from the template using the following:
<%$ Resources:ProductsResources, AmazonSearchServiceTitle %>

MVC

Reference from the view using the following:
@using Telerik.Sitefinity.Frontend.Mvc.Helpers;

@Html.Resource("NameOfTheResourseString")

Code-behind

Add the following using statement:
using Telerik.Sitefinity.Localization;

Reference the label using the following code:

Res.Get<ProductsResources>().AmazonSearchServiceTitle

Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.