0

I need to set Languages.AlwaysStripLanguage to false via code for specific sites, How to achieve this via extending Language Strip Pipeline and set the value to false.

2 Answers 2

0

You can achieve the mentioned functionality by following below steps

  1. Extend the pipeline processor that manages language stripping by creating a custom processor.
using Sitecore.Pipelines.HttpRequest;
using Sitecore.Sites;

namespace YourNamespace
{
    public class CustomLanguageStripProcessor : HttpRequestProcessor
    {
        public override void Process(HttpRequestArgs args)
        {
            // Check if Site Context is null
            if (Sitecore.Context.Site == null)
            {
                return;
            }

            // Retrieve the current site
            SiteContext siteContext = Sitecore.Context.Site;

            // Check if the current site matches your specific conditions
            if (siteContext.Name.Equals("yourSiteName", StringComparison.OrdinalIgnoreCase))
            {
                // Set AlwaysStripLanguage to false
                siteContext.Properties["Languages.AlwaysStripLanguage"] = "false";
            }
        }
    }
}

  1. Add your custom processor to the appropriate pipeline in the App_Config folder by creating a patch file.

     <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
      <sitecore>
         <pipelines>
           <httpRequestBegin>
             <processor type="YourNamespace.CustomLanguageStripProcessor, YourAssemblyName" patch:before="processor[@type='Sitecore.Pipelines.HttpRequest.LanguageResolver, Sitecore.Kernel']" />
           </httpRequestBegin>
         </pipelines>
       </sitecore>
     </configuration>
    

Try this and let me know if it works !!! Hope this helps !!!

2
  • No this is not working , value is still true for site and its still not stripping language.
    – rahul
    Commented Dec 9 at 8:06
  • Can you check it and add the processor after Sitecore.Pipelines.HttpRequest.SiteResolver. <processor type="YourNamespace.CustomAlwaysStripLanguageProcessor, YourAssemblyName" patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.SiteResolver, Sitecore.Kernel']" /> Commented Dec 9 at 9:21
0

Strip language is set inside the preprocessRequest pipeline which runs before the httpRequestBegin pipeline in which site context is set. Hence in order to obtain site context inside the custom processor created in the preprocessRequest pipeline using below code snippet.

using Sitecore.Pipelines.PreprocessRequest;
using Sitecore.Sites;
using Sitecore.Web;
using System.Linq;

public class CustomStripLanguage : StripLanguage
{
  public override void Process(PreprocessRequestArgs args)
  {
     var hostName = args.HttpContext.Request.Url.Host;
     SiteInfo site = SiteContextFactory.Sites
                .Where(s => s.RootPath != "" & s.HostName.Contains(hostName))
                .FirstOrDefault();
     if(site != null)
     {
        SiteContext contextSite =  SiteContextFactory.GetSiteContext(site.Name);
        // Based on the site context you can decide whether to strip language or not
        if(contextSite.Properties["StripLanguage"] == "false"){
           return;
        }
     }
     base.Process(args);
   }
}

Using above way, you should be successfully able to fetch site name inside preprocessRequest pipeline and decide whether the language should be stripped for the particular site through reading StripLanguage property on the site context .

You need to patch the custom processor created above as below.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
     <pipelines>
       <preprocessRequest>
         <processor type="YourNamespace.CustomStripLanguage, YourAssemblyName" patch:instead="processor[@type='Sitecore.Pipelines.PreprocessRequest.StripLanguage, Sitecore.Kernel']" />
       </preprocessRequest>
     </pipelines>
   </sitecore>
 </configuration>

Hope this helps!!! Let me know in case you face any issues.

3
  • AFter getting site name here, how property will be set to false ?
    – rahul
    Commented Dec 9 at 8:25
  • @rahul You can set the property StripLanguage inside the sites definition and then read the property as above. If this value is false, then the function is returned , otherwise if the value is true the flow remains as it is and the base class function is called.
    – ckhanna
    Commented Dec 9 at 8:44
  • tried above steps but its not working.
    – rahul
    Commented Dec 9 at 9:14

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.