Showing posts with label Context Language Resolver. Show all posts
Showing posts with label Context Language Resolver. Show all posts

Tuesday 3 August 2021

Sitecore - Custom Language Resolver

Hello Sitecore Folks,

If we want to resolve our site with the default context language even though it's not explicitly specified in the URL, then you have arrived at the right place.

e.g. www.site.com or www.site.com/ both the URLs would redirect to www. site.com/en, en is your context language.  

The custom pipeline code will set the context language.


Custom Language Resolver Pipeline:-


public class ContextLanguageResolver : HttpRequestProcessor
    {
        public override void Process(HttpRequestArgs args)
        {
            try
            {
                StartProfilingOperation("Resolve Context Language and strip trailing Slash", args);

                if (Context.Database == null
                    || Context.Database.Name == "core"
                    || !Context.PageMode.IsNormal
                    || Context.Item == null || Context.Language == null)
                    return;

                // Get out if this item if it isn't in the context site
                if (!Context.Item.Paths.FullPath.StartsWith(Context.Site.StartPath, StringComparison.OrdinalIgnoreCase))
                    return;

                string path = args.HttpContext.Request.RawUrl;
                if (!path.StartsWith($"/{Context.Language.Name.ToLower()}"))
                {
                    Log.Info($"[{this.GetType().Name}.{nameof(Process)}] Referrer URL of the Current Request is `{HttpContext.Current.Request.UrlReferrer}`", this);
                    string url = GetCanonicalUrlWithQuerystring(path);
                    args.HttpContext.Response.Redirect(url);
                }
                else if (path.EndsWith("/"))
                {
                    Log.Info($"[{this.GetType().Name}.{nameof(Process)}] Referrer URL of the Current Request is `{HttpContext.Current.Request.UrlReferrer}`", this);
                    string url = GetCanonicalUrlWithQuerystring(path);
                    args.HttpContext.Response.RedirectPermanent(url);
                }
            }
            finally
            {
                EndProfilingOperation(null, args);
            }
        }

        private static string GetCanonicalUrlWithQuerystring(string path)
        {
            var query = path.IndexOf('?');
            return $"{LinkManager.GetItemUrl(Context.Item)}{(query == -1 ? string.Empty : path.Substring(query))}";
        }
    }

Configuration Patch:-


<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:env="http://www.sitecore.net/xmlconfig/env/">
    <sitecore>
        <pipelines>
            <httpRequestBegin>
                <processor type=ProjectName.Website.Pipelines.HttpRequestBegin.ContextLanguageResolver,
ProjectName.Website" patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']">
                </processor>
            </httpRequestBegin>
        </pipelines>
    </sitecore>
</configuration>