Tuesday, March 12, 2013

QuickStart: Resource Strings

When planning the Sitecore Content Tree Architecture it makes sense to place some content on a page.  Other times it makes sense to create Generic Content items that would store blocks of content in a shared location.  This content could be used across pages.  Other times there are content items such as Form Labels, static text for instructions.  These need translations, but placing the potentially thousands of them in the generic content folder would make it more difficult to manage.

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