Friday, February 24, 2012

Creating a Custom Resource Provider in Telligent Community 6.0

There may be times when you want to move the localization away from the widget and into a centralized provider.  Or if you're part of a larger company, it's possible there are already many localized resources in the build and there is a separate established process for localization.  The model for Telligent Community 6.0 - namely widget studio is to make each widget self-contained, however, there may be special circumstances or business processes in where it makes more sense to centralize your resources.  This blog post will show you one way to accomplish this.

Telligent Community Server will let you create new velocity extensions through a specific interface called: IScriptedContentFragmentExtension.  We will expose our resource manager by creating a class that implements this interface.  This will allow us to enable the extension through the plugins area in the control panel.

Step 1:  Create a ResourceManagerExtensionDefinition class that implements IScriptedContentFragmentExtension

   
public class ResourceManagerExtensionDefinition : IScriptedContentFragmentExtension
    {
        public object Extension
        {
            get { return new GlobalResourceManager(); }
        }

        public string ExtensionName
        {
            get { return "companyName_v1_resourceManager"; }
        }

        public string Description
        {
            get { return "Provides access into a custom resource repository for shared resources."; }
        }

        public void Initialize()
        {

        }

        public string Name
        {
            get { return "Custom Resource Manager"; }
        }
    }


Step 2:  Create the base class that contains all of the Resource Manager logic.
In this example our resource manager will simply read from an XML file.  In more complex scenarios we could connect to a database or even implement pre-existing classes and objects used across multiple sites/projects.

This code will populate an XmlDocument when the object is constructed.

 
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Text;
using System.Xml;
using System.IO;
using Telligent.Evolution.Components;

namespace Evolution.WidgetExtensions.Extensions.v1
{
    public class GlobalResourceManager
    {
        #region Properties

        public XmlDocument ResourceDoc { get; set; }

        #endregion

        public CustomResourceManager()
        {
            this.ResourceDoc = new XmlDocument();
            this.ResourceDoc.LoadXml(File.ReadAllText(HttpContext.Current.Server.MapPath(String.Format(ConfigurationManager.AppSettings["LanguageFile"], CSContext.Current.User.Profile.Language))));
        }

        /// 
        /// Get the value of a custom global resource.
        /// 
        /// 
        public string GetResource (string resourceKey)
        {
            XmlNode resourceNode = this.ResourceDoc.SelectSingleNode(String.Format("/root/resource[@name='{0}']", resourceKey));
            if (resourceNode != null)
            {
                return resourceNode.InnerText;
            }
            else
            {
                return String.Empty;
            }
        }
    }
}

Step 3:  Create the Resource File
In this example we're using a basic XML file for our resources.  Save it a location of your choosing in your web directory.

<?xml version="1.0" encoding="utf-8"?>

  Username
 


Step 4: Modify your web.config by adding an appSetting which points to the location of our resource file 
In this example our location contains aplaceholder for token replacement.  In the code above, we detect the current user's language and change the path according to that.  




  <appSettings>
    <add key="Telligent.Glow.MultipleFileUpload.FileManagerProvider" value="Telligent.Evolution.Components.MultipleUploadFileManager, Telligent.Evolution.Components" />
    <add key="Telligent.Glow.MultipleFileUpload.UploadHandlerUrl" value="~/multipleupload.ashx" />
    <add key="LanguageFile" value="~/Customizations/Languages/{0}/Resources.xml"/>
  </appSettings>


Step 5: Next we'll need to recompile our solution and enable the IScriptedContentFragmentExtension from the control panel




Step 6:  Our resource manager is now available in a studio widget.

#set($username = $companyName_v1_resourceManager.GetResource("username"))


No comments:

Post a Comment