Skip to main content
deleted 15 characters in body
Source Link
Mark Cassidy
  • 25.4k
  • 7
  • 59
  • 117

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

Best,

Naim

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

Best,

Naim

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

added 30 characters in body
Source Link
Mark Cassidy
  • 25.4k
  • 7
  • 59
  • 117
/// <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);
                }
            }
        }
/// <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);
                }
            }
        }
/// <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);
                }
            }
        }
/// <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);
                }
            }
        }
Source Link
Naim Alkouki
  • 586
  • 1
  • 6
  • 16

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

Best,

Naim