10

Sitecore does not seem to set the expiry date for the language cookie, Which means it will be stored per "session", meaning If user switch language on the site then closes the browser and open it again, the language will be set back to the default language.

Anyone was able to set the expiry date for the language cookie in sitecore?

4 Answers 4

6

There is no out of the box way to do this, you need to customize Sitecore.Context.SetLanguage function as

/// <summary>
/// Sets the current language.
/// This to extend the sitecore default SetLanguage behavior by adding expiration date for the language cookie
/// </summary>

/// <param name="language">The language.</param>
/// <param name="persistent">if set to <c>true</c>, the value will be persisted (in a cookie).</param>
/// <param name="ExpirationDate">The cookie expiration date, presistent should be set to <c>true</c> too </param>

public static void SetLanguage(Language language, bool persistent, DateTime ExpirationDate)
        {
            Assert.ArgumentNotNull(language, "language");
            Context.Items["sc_Language"] = language;

            if (!persistent)
            {
                return;
            }

            SiteContext site = Context.Site;

            if (site != null)
            {
                string cookieKey = site.GetCookieKey("lang");

                if (WebUtil.GetCookieValue(cookieKey) != language.Name)
                {
                    WebUtil.SetCookieValue(cookieKey, language.Name, ExpirationDate);
                }
            }
        }

I added more details at my blog :

http://alkouki.blogspot.com/2016/11/extend-sitecorecontextsetlanguage-to.html

7

I am not sure if there is an easier way to do this. But I've achieved this before by setting a custom language cookie and then set the context language in a HttpRequest pipeline processor based on the value of your custom language cookie.

When you switch language on the website then you can just set the value of your own custom language cookie and then you have complete control over its expiry date.

1
  • 1
    Please consider including a code/configuration sample. That would additionally improve your answer. Commented Nov 1, 2016 at 17:38
3

This may be a recent change (checked in 8.2 Update 4) - but the Sitecore API has an overload for the Sitecore.Context.SetLanguage method that should do what you want:

Notice the 4th argument of DateTime?

/// <summary>
/// Sets the current language for current request, and can persist it for given site for specified duration via cookie.
/// </summary>
/// <param name="language">The language to be set.</param>
/// <param name="persistent">if set to <c>true</c> persistent via cookie.</param>
/// <param name="expiryDate">The cookie expiry date. <c>null</c> for making cookie as 'session'.</param>
/// <param name="site">The site to have language set.</param>
public static void SetLanguage(Language language, bool persistent, SiteContext site, DateTime? expiryDate = null)
{
  Assert.ArgumentNotNull((object) language, nameof (language));
  Context.Items["sc_Language"] = (object) language;
  if (!persistent || site == null)
    return;
  string cookieKey = site.GetCookieKey("lang");
  if (!expiryDate.HasValue && !(WebUtil.GetCookieValue(cookieKey) != language.Name))
    return;
  WebUtil.SetCookieValue(cookieKey, language.Name, expiryDate ?? DateTime.MinValue);
}
1
  • Awesome! Didn't know that! Commented Dec 16, 2018 at 17:43
2

You will have to do this yourself. When the Language property of the current Context is set, Sitecore will store this as a cookie. However these are done via static-methods that can't be overridden.

You can either implement your own cookie, as per Jonas' answer, or you could add a pipeline processor that runs at the end of the HttpRequest that overrides the default Sitecore language cookie with the same name and value, but with a customized expiry date.

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.