7

I was wondering if anybody knows how to set up a multilingual site that switches languages but doesnt make the language switch persistent.

So I want www.mysite.com to show the english page and www.mysite.com/sv-se to show the Swedish. But if I visit www.mysite.com/page after visiting a Swedish page it should show the English (default) page again.

If I set the languageEmbedding="always" I am halfway there, but I cant figure out how to disable the content language change from being persistent.

Any ideas appreciated.

  • 1
    So your problem is only when the site is visited without a language parameter? In that case you could add a processor in the httpRequestBegin pipeline after the languageresolver.. Can elaborate in an answer, but wanted to check first if this is indeed what you are looking for. – Gatogordo May 11 '17 at 7:46
  • 1
    Hm, yes you are correct. It could be solved like that. I was thinking that I wolud have to turn of language persistance to achieve this. But perhaps its easier to Switch content lanugage to en if there is no language parameter? – Christian Raaschou May 11 '17 at 9:20
13

Try the following solution:

We need to override Sitecore.Pipelines.PreprocessRequest.StripLanguage

using Sitecore.Diagnostics;
using Sitecore.Globalization;
using Sitecore.Web;
using System.Web;
using Sitecore;
using Sitecore.Pipelines.PreprocessRequest;

namespace YourProject
{
public class StripLanguage : Sitecore.Pipelines.PreprocessRequest.StripLanguage
{
    /// <summary>Processes the specified arguments.</summary>
    /// <param name="args">The arguments.</param>
    public override void Process(PreprocessRequestArgs args)
    {
        Assert.ArgumentNotNull((object)args, "args");

        Language language = StripLanguage.ExtractLanguage(args.Context.Request);
        if (language == (Language) null)
        {
            //if url doesn't contain language name, we will set the default language
            language = Sitecore.Globalization.Language.Parse("en");
        }
        Context.Language = language;
        Context.Data.FilePathLanguage = language;
    }

    /// <summary>
    /// Extracts the language from the file path of the current request.
    /// </summary>
    /// <param name="request">The HTTP request.</param>
    /// <returns>The language.</returns>
    private static Language ExtractLanguage(HttpRequest request)
    {
        Assert.ArgumentNotNull((object)request, "request");
        string languageName = WebUtil.ExtractLanguageName(request.FilePath);
        if (string.IsNullOrEmpty(languageName))
            return (Language)null;
        Language result;
        if (!Language.TryParse(languageName, out result))
            return (Language)null;
        return result;
    }
}
}

Then you need to replace the Sitecore's StripLanguage with your implementation. Add the following CustomStripLanguageProcessor.config file into the include folder:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
  <sitecore>
    <!--Pipelines-->
    <pipelines>
      <preprocessRequest>
        <processor type="Sitecore.Pipelines.PreprocessRequest.StripLanguage, Sitecore.Kernel" >
          <patch:delete/>
        </processor>
        <processor type="YourProject.StripLanguage, YourProject" />
      </preprocessRequest>
    </pipelines>
  </sitecore>
</configuration>

Now, when your url have the /sv-se/ , the Swedish language will be set as a context language. But if it is not, the default language will be used.

4

One way to solve this could be by placing a custom processor in the httpRequestBegin pipeline after the LanguageResolver (<processor type="Sitecore.Pipelines.HttpRequest.LanguageResolver, Sitecore.Kernel"/>). You let Sitecore resolve the language first, and you switch the language based on your custom business rules - in this case that rule should be that if the url did not contain a language the language should always be English.

Don't forget to check in your custom resolver that the url is not a Sitecore url, you don't want to mess with those.

But: if you really have urls with and without language, you should also think about duplicate content. So you should actually redirect those pages to a url with the language included or at least put a canonical url in the head.

If you would decide to redirect, another option could be to use the IIS rewrite module. You could create a rule that checks if

  • the url begins with /en/ or /sv-se/ (one of the languages allowed)
  • and is not a media request or a Sitecore related request

and redirect that to a url with /en/ in front...

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.