Wednesday, December 3, 2008

Include language resources in SharePoint

After reading the blogs Tomblog and Mikhail Dikov on language resources in SharePoint I decided to create a "language aware" version of some SharePoint FieldControls.

In addition I created a function to retrieve the resources that read the resx file if available. If the resourcefile is obsolete (Not published to the App_GlobalResources with ststadm -o copyappbincontent or a feature receiver) The resource will be created from the default resource compiled into the assembly.

Code:

public static String GetResource(String ID)

{

String ResourceString = "";

// Check if the resource exists in the App_Resources folder

// Hint use stsadm -o copyappbincontent to copy the contents from the /CONFIG/Resources directory

// A better way to update the resources is by a feature receiveres

try

{

ResourceString = HttpContext.GetGlobalResourceObject("chandrila_cdlf", ID, SPContext.Current.Web.Locale).ToString();

}

catch

{ }

try

{

if (String.IsNullOrEmpty(ResourceString))

{

// Fall back to the compiled resources

return Resources.chandrila_cdlf.ResourceManager.GetString(ID);

}

else

return ResourceString;

}

catch

{

// If all fails return the ID of the resource that was requested.

return ID;

}

}



Not rocket science, but it helps avoiding strange errors for the SharePoint Adminstrators

No comments: