Sam
Sam

Reputation: 461

How to set default language in Sitecore multilingual solution?

I have a Sitecore website in 3 languages (en, es and fr) and user can change the language with toggle switch on website.

In web.config sites section the defaultLanguage is "en", but even after this when I go to spanish version like www.abc.com/es.aspx, then after if I go to www.abc.com (english version) the website still remains in Spanish language.

Any idea how to set the default language to english?

Upvotes: 2

Views: 10991

Answers (3)

Marek Musielak
Marek Musielak

Reputation: 27132

Assuming that your sites config contains

<site ... language="en" ... />

you can define your own module that for every new session will reset language to the default one.

First you need to create module class:

using Sitecore.Globalization;
using System;
using System.Web;
using System.Web.SessionState;

namespace My.Assembly.Namespace
{
  public class ResetLanguageModule : IHttpModule, IRequiresSessionState
  {
    public void Init(HttpApplication app)
    {
      if (app.Modules["Session"] != null)
      {
        SessionStateModule session = (SessionStateModule) app.Modules["Session"];
        session.Start += Session_Start;
      }

      app.BeginRequest += Application_BeginRequest;
    }

    private static void Application_BeginRequest(object sender, EventArgs e)
    {
      // if there is no referrer or user comes from external site
      if (HttpContext.Current.Request.UrlReferrer == null
        || HttpContext.Current.Request.UrlReferrer.Host != HttpContext.Current.Request.Url.Host)
      {
        // if user is on the root
        if (HttpContext.Current.Request.RawUrl == "/")
        {
          ResetLanguage();
        }
      }
    }

    private static void Session_Start(object sender, EventArgs e)
    {
      ResetLanguage();
    }

    private static void ResetLanguage()
    {
      if (Sitecore.Context.Language.Name != Sitecore.Context.Site.Language)
      {
        Language currentSiteLanugage;
        if (Language.TryParse(Sitecore.Context.Site.Language, out currentSiteLanugage))
        {
          Sitecore.Context.SetLanguage(currentSiteLanugage, true);
        }
      }
    }

    public void Dispose()
    {
    }
  }
}

and then register the module in web.config:

<system.webServer>
  <modules>
    ... all existing modules
    <add name="ResetLanguageModule" type="My.Assembly.Namespace.ResetLanguageModule, My.Assembly" />
  </modules>
</system.webServer>

This might not work when you have your browser open and you just close the tab and reopen, as the browser might still keep the session alive.

Upvotes: 6

remco
remco

Reputation: 126

By default the languageresolver switches from language defined in the url to language stored in the cookie automatically. (called asNeeded in the config of the linkmanager)

Setting languageEmbedding="always" should solve your issue.

also: check this blog from John: http://www.sitecore.net/nederland/Community/Technical-Blogs/John-West-Sitecore-Blog/Posts/2010/11/Overriding-Sitecores-Logic-to-Determine-the-Context-Language.aspx

Upvotes: 0

Mark Ursino
Mark Ursino

Reputation: 31435

In your <site> configuration, set the language attribute for your default. E.g.

<sites>
  <site name="mysite" language="es-ES" ... />
</sites>

Then to switch you can use the path like you showed, e.g /en/foo or /es/foo or you can even use a query string to switch languages, like so: ?sc_lang=en or ?sc_lang=es-ES

Refer to this page for more details.

Upvotes: 1

Related Questions