The alternative is to create a folder underneath each site in a Multi-Site solution called ResourceStrings.
Under this folder you can arrange your resource strings by SubLayout name and/or whichever folder structure makes sense to make them easier to find.
If you use a fast query to find them then as long as the resource string name is unique you can place them wherever you want or reorganize them later as you please.
public static string Translate(string key)
{
string itemPath = "FAST_" + Context.Language + "_" + Context.Site.ContentStartPath + "/ResourceStrings//*[@Key = '" + key + "']";
if (CacheManager.GetData(itemPath) != null)
{
return CacheManager.GetData(itemPath) as string;
}
string result = string.Empty;
Sitecore.Data.Items.Item[] dictionaryItems = Sitecore.Context.Database.
SelectItems("fast:" + Sitecore.Context.Site.ContentStartPath + "/ResourceStrings//*[@Key = '" + key + "']");
if (dictionaryItems != null && dictionaryItems.Length > 0)
{
// bad entry should fail gracefully.
try
{
result = dictionaryItems[0].Fields["Phrase"].Value;
}
catch
{
result = string.Empty;
}
}
CacheManager.Add(itemPath, result, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromSeconds(SiteSettings.ItemCacheInSeconds)));
return result;
}
Note that this example uses CacheManager. This is simply the Microsoft Practices Cache Manager.
In order to support the content editor you would need to wrap the controls you are using to display them in sc:editframe I am actually working on a QuickStart Package that would include a series of controls that loosely wrap the standard Sitecore and .NET controls with the ability to supply a resource string.
The Resource Strings Module is now available in the Sitecore Marketplace with full source code.
This should help you get started. If you are using Sitecore 7 you may want to take advantage of LINQ in Sitecore over FAST to make the code more readable and to piggy back on any optimizations Sitecore makes in the LINQ Provider.
If you have any tips, tricks or resources you want to share with the Guild or simply have a Sitecore question email me at sitecoreguild@outlook.com or chris.williams@threepointturn.com
No comments:
Post a Comment