6

I am getting stuck with an issue, I have a Multilanguage site that is based on SXA. Rather than going to change via language drop-down, I am going to change language via URL.

Please see the steps below what I made for reproducing this issue:

  1. first of all I have two language site which is en-ca and fr-Ca. en-ca is the default one.
  2. I have a product page that exists in both languages.
  3. When I try to hit the URL like: https://domainname/en-ca/product for en-ca result will be correct. Now rather than changing the item language from the language selector dropdown, I tried to change the URL from the above to like: https://domainname/fr-ca/product. After hitting the same page will load and get data from fr-ca language.
  4. But it goes to the 404 page, because no product name like this exists in the content tree for fr-ca since I have changed the name of the product item in fr-ca to products.

So my concern is that rather than resolving an item via its item name, I want to resolve it via display name.

Anyone, please suggest to me the better way how I can handle this?

2

3 Answers 3

0

Please make sure that you have enabled language embedding field in Site Grouping item (/sitecore/content/TENANT/YOURSITEfolder/YOURSITE/Settings/Site Grouping) like below:

enter image description here

Also, please provide your Sitecore and SXA version so that it might be easy for community members to suggest solutions.

0

If you want to enable resolving items by their display name in Sitecore's LinkManager, you can use the useDisplayName setting. Patch in the below config to resolve item using display name.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <linkManager>
      <providers>
        <localizedProvider>
          <patch:attribute name="useDisplayName">true</patch:attribute>
        </localizedProvider>
      </providers>
    </linkManager>
  </sitecore>
</configuration>

0

Implement a custom item resolver to handle language-specific URLs and resolve items based on their display names rather than item names.

Here's a basic implementation example :

using Sitecore;
using Sitecore.Configuration;
using Sitecore.Diagnostics;
using Sitecore.Pipelines.HttpRequest;
using Sitecore.Sites;
using Sitecore.Web;

public class CustomItemResolver : HttpRequestProcessor
{
    public override void Process(HttpRequestArgs args)
    {
        if (Context.Site == null)
        {
            return;
        }
        SiteContext siteContext = Context.Site;

        if (siteContext != null && siteContext.DisplayMode == DisplayMode.Normal)
        {
            string requestedUrl = args.Url.ItemPath;

            string languageName = ExtractLanguageFromUrl(requestedUrl);

            if (!string.IsNullOrEmpty(languageName))
            {
                Language language = Language.Parse(languageName);
                Context.Language = language;

                string displayName = ExtractDisplayNameFromUrl(requestedUrl);

                Item item = Sitecore.Context.Database.GetItem(siteContext.RootPath + "/" + displayName);

                if (item == null)
                {
                    args.AbortPipeline();
                    HttpContext.Current.Response.Redirect("/404");
                    return;
                }
                Context.Item = item;
            }
        }
    }

    private string ExtractLanguageFromUrl(string url)
    {
        // Implement logic to extract language from URL, e.g., parse the URL path
    }

    private string ExtractDisplayNameFromUrl(string url)
    {
        // Implement logic to extract display name from URL, e.g., parse the URL path
    }
}

You'll need to implement the ExtractLanguageFromUrl and ExtractDisplayNameFromUrl methods to parse the language and item display name from the URL according to your URL structure.

Make sure to patch this custom processor into the httpRequestBegin pipeline in your Sitecore configuration file.

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.