18 January 2005

Fallback to a Master Language

Imagine a scenario where you have two or more content languages on a Sitecore solution. Some of them are regional and one is the global master language f.ex. in English.

When new documents are created in the master language they must be translated to the local language. But as this workflow process might take time it could be nice to be able to show the master language version on the local site until translation is done.

To obtain that you need to be able to show content from any language when doing renderings. Using C# based renderings that is a simple task as the Sitecore object model exposes methods for retrieving field data from a specified language. That is not the case in XSLT renderings where the commonly used sc:fld method does not take a language parameter but assumes the current language as set in the State object.

To expose such a function a new XSLT helper could be created with the following function that has the missing parameter.

public string fld(string sFieldName, XPathNodeIterator ni, string language)
{
string itemID = ni.Current.GetAttribute("id","");
IWebItem currentItem = WebFactory.GetItem(itemID);
return currentItem.GetFieldValue(sFieldName,language);
}

This allows the XSLT designer to start by requesting the local language using the XSLT parameter $lang as input. If it returns empty the master language identifier can the be used instead.

The master language could also be specified in web.config and the switch could then be implemented directly in the new XSLT helper function without any need for requesting the language parameter from the user. Like this:

public string fld(string sFieldName, XPathNodeIterator ni)
{
string itemID = ni.Current.GetAttribute("id","");
IWebItem currentItem = WebFactory.GetItem(itemID);
string sFieldValue = currentItem.GetFieldValue(sFieldName);
if(sFieldValue==””)
{
sFieldValue = currentItem.GetFieldValue(sFieldName,Settings.GetSetting("Masterlanguage");
}
Return sFieldValue;
}

Ideas for further features: The function could be extended for retrieval of sub values and the Masterlanguage setting could be a pipe separated list of prioritized fallback languages.

/Pau

1 comment:

Lars said...

Pau,

You just saved Marcs and my day. We were asked about this question from a potential customer, and could immediately refer to this post.

Cool!

Thanks,
Lars Fl√łe Nielsen,
Sitecore