6

I heard it is possible to control your device layout by which culture you are in. I can not find any documentation or examples on this. I see that you can choose a device layout by QueryString or some rules. But none of those rules seem to inspect the culture.

Can this be accomplish either by sitecore items or through code?

  • Define culture? Do you mean the current context language or the browser culture? – Richard Seal Nov 3 '17 at 18:26
  • current context language site/en-us site/en-ca – Mike Nov 3 '17 at 19:14
  • Which version of Sitecore do you use? – Artsem Prashkovich Nov 3 '17 at 20:40
  • And what about Shared Layout and Final Layout (assuming you're on Sitecore 8+)? – Marek Musielak Nov 3 '17 at 20:53
  • We are on 8.2 update 4.... as for Shared Layout the issue was that for all countries and every page the Final layout would have to be updated as we go from old layout to new layout. It was suggested we use device layout and then we just have to "switch devices" when each culture is ready – Mike Nov 4 '17 at 12:37
5

As far as I understand your problem, you have two layouts: old and new. Now you want to switch to the new layout step by step. You decided to add a new device (suppose "Default New") and set updated layout for it and then, using some mechanism like rules, redirect users to this device. But you can't found a solution how to do that.

I also didn't find any rules which match your requirements. I suggest you add your custom condition. Add code below to solution:

using System.Web;
using Sitecore.Data;
using Sitecore.Diagnostics;
using Sitecore.Rules.Conditions;
using Sitecore.Rules.Devices;

namespace Your.Namespase
{
    public class ContextLanguageCondition<T> : StringOperatorCondition<T> where T : DeviceRuleContext
    {
        private ID languageId;
        public ID LanguageId
        {
            get
            {
                return this.languageId;
            }
            set
            {
                Assert.ArgumentNotNull((object)value, "value");
                this.languageId = value;
            }
        }

    public ContextLanguageCondition()
    {
        this.LanguageId = ID.Null;
    }

    public ContextLanguageCondition(ID languageId)
    {
        Assert.ArgumentNotNull((object)languageId, "languageId");
        this.languageId = languageId;
    }

    protected override bool Execute(T ruleContext)
    {
        HttpContextBase httpContext = ruleContext.HttpContext;
        if (httpContext == null)
            return false;

        var lang = Sitecore.Context.Language;

        if (LanguageId == ID.Null || lang == null)
            return false;

        var langItemID = Sitecore.Data.Managers.LanguageManager.GetLanguageItemId(lang, Sitecore.Context.Database);

        if (langItemID == ID.Null)
            return false;

        return LanguageId == langItemID;
    }
}
}

Secondary, you need create a condition item in Sitecore. For example under "Context" Element Folder:

add custom rule

Data for creating rule:

Path: /sitecore/system/Settings/Rules/Definitions/Elements/Context

Text: where the language is [languageId,Tree,root=/sitecore/system/Languages,specific language]

Type: Your.Namespase.ContextLanguageCondition,Your.Namespase

Make sure you update the type with your library name.

After that, you can set the rule for a device:

assigning rule for device detection selecting rule

If condition returns "true", the device will be switched

| improve this answer | |
  • Thank you I am trying the solution now. I am having trouble finding what dll the Sitecore.Rules.Devices and DeviceRuleContext comes from. – Mike Nov 6 '17 at 14:06
  • It definitely should be placed in the bin folder of your website. – Artsem Prashkovich Nov 6 '17 at 14:09
  • Also you can use NuGet for adding the reference to your project. – Artsem Prashkovich Nov 6 '17 at 14:12
  • I am sure it is in the bin, but I don't know which dll to reference. I reference sitecore.kernal and that takes care of everything but those two elements. We use a custom Nuget package – Mike Nov 6 '17 at 14:15
  • Also Sitecore.Rules.Conditions is known but not Sitecore.Rules.Devices – Mike Nov 6 '17 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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