0

I am trying to create custom routing for multiple languages. Created processor like below image. Problem is that it does not give exact context language but gives "en" as language name. Can someone please help how to get proper context language inside the processor. enter image description here

config file has been patched after

EnsureAnonymousUsers

as below:

patch:after="processor[@type='Sitecore.Pipelines.Loader.EnsureAnonymousUsers, Sitecore.Kernel']"
1
  • 3
    Rashmi, please add code snippet instead of image for better SEO. Commented Apr 25 at 9:12

2 Answers 2

1

The code you provided seems to be setting the scLanguage route parameter based on a hardcoded value ("ab-AB" or "de-DE") depending on the Sitecore.Context.Language.Name property. This might be the reason why it's always returning "en".

To get the proper context language inside the processor, you should use the Sitecore.Context.Language property directly instead of hardcoding values. Here's the modified code:

{
  public void Process(PipelineArgs args)
  {
    try
    {
      string siteName = "Default SiteName;
      if(Sitecore.Context.Language.Name.Equals("ab-AB"))
      {
          siteName = "ABC";
      }
      else if(Sitecore.Context.Language.Name.Equals("de-DE"))
      {
          siteName = "DEF";
      }

      RouteTable.Routes.MapRoute(
          name: "BlogPostPageRedirect",
          url: "blogPost/page/{pageIndex}",
          defaults: new
          {
            scItemPath = "/sitecore/content/" + siteName + "/Home/Blog",
            controller = "Blog",
            action = "GetBlogPost",
            scLanguage = Sitecore.Context.Language
          });
    }
    catch (Exception ex)
    {
      // Log or handle exception
    }
  }
}

This code retrieves the current language context from Sitecore.Context.Language and assigns it to the scLanguage route parameter. This should ensure that the route uses the actual language retrieved from the context.

You can add the custom processor you created to the mvc.renderRendering pipeline after the standard Sitecore MVC rendering resolvers.

Please try this once

0

Since you are patching your processor after EnsureAnonymousUsers processor, at that moment Sitecore.Context.Language is not set(by default it's en). In Sitecore, context language is set during the httpRequestBegin begins pipeline, which runs after initialize pipeline.

Also note the initialize pipeline runs only once during the application startup.

Since we need to register all routes during the initialize pipeline, therefore you can create a method as below wherein site name and language are read inside the Action method as below:

public void GetBlogPost(string siteName, string language)
{
   siteName = siteName ?? Sitecore.Context.Site.Name;
   language = language ?? Sitecore.Context.Site.Language;
}

When the controller action method is called, it can read Sitecore.Context.Site as well as Sitecore.Context.Language, since those are statically available.

Hope this helps!!!

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.