2

I have to use a dictionary as shown below :

-content
--Site1
---dictionary1
--site2
---dictionary2
--Global data
---Dictionary3

If the dictionary phrase is there on the local site dictionary folder, it should take from there.

If it is not present in the local dictionary folder, it should fetch from the global dictionary folder.

3 Answers 3

0

I would suggest that you can extend it by adding a new class for the Dictionary items to enable the fallback. So use the following steps.

  1. Add a class to your Site.

    public class  DictionaryFallback
    {
        public void Process(GetTranslationArgs args)
        {
    
            Item DicRoot = Sitecore.Context.Database.GetItem("<provide the path of your site dictionary root>");
    
             Item DicEntry = DicRoot.Axes.GetDescendants().Where(entry => entry.TemplateID == new ID("<dictionary item template id>") &&
                entry.Name == args.Key).FirstOrDefault();
    
    
        if (DicEntry != null)
        {    
    
            var item = Sitecore.Context.Database.GetItem(DicEntry.ID, Sitecore.Context.Language, Sitecore.Data.Version.Latest);
    
            if (item.Versions.GetVersionNumbers().Length > 0)
            {
                if (!string.IsNullOrEmpty(item.Fields["Phrase"].Value))
                {
                    args.Result = item.Fields["Phrase"].Value;
                }
            }
        }
        else
        {
                //Write the same logic to get from your Global site
        }
    }
    }   
    
  2. Register the class using a patch.

    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
    <pipelines>
      <getTranslation>
        <processor type="XXX.XXX.XXX.DictionaryFallback, XXX.XXX" patch:after="*[last()]"/>
      </getTranslation>
    </pipelines>
    </sitecore>
    </configuration>
    

Hope this helps.

0

The dictionary module extends the site context in Sitecore with the ability to configure a site-specific dictionary domain. This is done by adding an attribute to the Site definition.

enter image description here

Once you done with this configuration, can start use dictionary items using below code:

To use the Dictionary item from the current site, go to the View file and use the following code in the file.

@Sitecore.Globalization.Translate.Text (“key”);

*In case if you want to use the dictionary item from different domain, use:

@Sitecore.Globalization.Translate.TextByDomain(“Dictionary Domain Name”, key”);

For the fallback, can use solution suggested by Sumit.

0

I would recommend going with the dictionaryDomain implementation.

When you access any Key It will first search for the site default domain folder, if specified, in context database and if no dictionaryDomain attribute is defined for that site then it will look into the /sitecore/system/Dictionary folder in Context Database, if Key is not defined here as well then at last It will fallback to /sitecore/system/Dictionary in Core Database.

Use the following steps to make the Content tree and your Site Definition.

  1. Go to the /sitecore/system/Dictionary and right click to select Insert From Template option.
  2. Navigate to the /sitecore/templates/System/Dictionary/Dictionary Domain and Insert Dictionary Domain container. Make sure you name it with some related name to your site (will be helpfull in case of multisite).
  3. Now, you will have /sitecore/system/Dictionary/ folder for your site. You can add as many as Dictionary Folders and Entries under this Domain folder.
  4. Go to your Site Definition and add attribute dictionaryDomain="Dictionary Domain Folder Name or ID".

Your Dictionary Folder should look like the below now. enter image description here

config changes to set dictionaryDomain will look like this.

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
  <sitecore>
    <sites>
       <site name="Site1" patch:before="site[@name='website']"                                       
            hostName=""
            virtualFolder="/"
            physicalFolder="/"
            rootPath="
            startItem=""
            database="master"  
            domain=""     
            dictionaryDomain="Site 1 Dictionary Domain Folder OR GUID of the Site 1 Dictionary Domain Folder"
            language=""
            allowDebug=""
            cacheHtml="" 
            htmlCacheSize=""
            enablePreview=""
            enableWebEdit=""
            enableDebugger=""
            disableClientData=""/>
    <site name="Site2" patch:before="site[@name='website']"            
            hostName=""
            virtualFolder="/"  
            physicalFolder="/"    
            rootPath=""
            startItem=""
            database="master"
            domain=""
            language=""
            allowDebug=""
            dictionaryDomain="Site 2 Dictionary Domain Folder or GUID of the Site 2 Dictionary Domain Folder"
            cacheHtml=""
            htmlCacheSize=""
            enablePreview=""
            enableWebEdit=""
            enableDebugger=""
            disableClientData=""/>
    </sites>
  </sitecore>
</configuration>

Now you can use a dictionary with this code:

Sitecore.Globalization.Translate.Text("key");
Sitecore.Globalization.Translate.TextByDomain("Dictionary Domain Name","key");

Hope it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.