2

On our international microsite, language embedding is set to "always", by design, so we have the language prefix in all paths like internationalsite.com/es-mx/about-us, set in the app config:

<add name="internationalProvider" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" resolve="true"  languageEmbedding="always" lowercaseUrls="true" />

Our main site and other microsites do NOT do language embedding:

<add name="localizedProvider" type="Sitecore.XA.Foundation.Multisite.LinkManagers.LocalizableLinkProvider, Sitecore.XA.Foundation.Multisite" cacheExpiration="5" addAspxExtension="false" alwaysIncludeServerUrl="false" encodeNames="true" languageEmbedding="never" languageLocation="filePath" shortenUrls="true" useDisplayName="false" patch:source="Mysite.LinkProvider.config" lowercaseUrls="true"/>

So linking within the International site is fine, but when an author adds a rich text link on e.g. internationalsite.com/es-mx/about-us to a page on another site, such as researchsite.com/page, the language 'es-mx' gets embedded - and because this is NOT supported on the research site, researchsite.com/es-mx/page 404s. How can I make the rich text editor embed the language in local links but NOT embed the language in cross-site links?

1 Answer 1

1

In order to make sure language is embedded in the local links and not on the cross site link, you will need to customize the link provider as below. Inside the custom link provider, GetItemUrl function is overridden to embed the language as always when the language embedding is enabled for the site and never when language embedding is disabled on the site.

using Microsoft.Extensions.DependencyInjection;
using Sitecore.Abstractions;
using Sitecore.Data.Items;
using Sitecore.DependencyInjection;
using Sitecore.Diagnostics;
using Sitecore.Links;
using Sitecore.Links.UrlBuilders;
using Sitecore.XA.Foundation.Multisite;
using Sitecore.XA.Foundation.Multisite.Extensions;

public class CustomLinkProvider : LinkProvider
{
  private readonly BaseFactory _factory;
  public CustomLinkProvider()
     : this(ServiceProviderServiceExtensions.GetService<BaseFactory>(ServiceLocator.ServiceProvider))
  {
  }
  public CustomLinkProvider(BaseFactory factory) : base(factory)
  {
     this._factory = factory;
  }
  public override string GetItemUrl(Item item, ItemUrlBuilderOptions options)
  {
     Assert.ArgumentNotNull((object)item, nameof(item));
     Assert.ArgumentNotNull((object)options, nameof(options));
     var siteInfoResolver = ServiceProviderServiceExtensions.GetService<ISiteInfoResolver>(ServiceLocator.ServiceProvider);
     var siteInfo = siteInfoResolver.GetSiteInfo(item);
     if (siteInfo != null)
     {
        var site = Sitecore.Configuration.Factory.GetSite(siteInfo.Name);
        if (site.IsLanguageEmbeddingEnabled())
        {
           options.LanguageEmbedding = LanguageEmbedding.Always;
        }
        else
        {
           options.LanguageEmbedding = LanguageEmbedding.Never;
        }
    }

    return base.GetItemUrl(item, options);
  }
}

This custom link provider then can be applied on the international site as below.

<add name="internationalProvider" type="SitecoreScaledExtensions.Pipelines.CustomLinkProvider,SitecoreScaledExtensions" resolve="true"  languageEmbedding="always" lowercaseUrls="true" />

Lastly we need to make sure Rendering.SiteResolvingMatchCurrentLanguage is patched as below. This setting decides whether language should be taken into consideration when a cross-site link is built.

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
   <sitecore>
    <settings>
      <setting name="Rendering.SiteResolvingMatchCurrentLanguage">
        <patch:attribute name="value">false</patch:attribute>
      </setting>
    </settings>
   </sitecore>
</configuration>

Once all above changes are applied, links on the local site will contain language in the url whereas cross site link will contain no language on the url inside the international site.

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

0

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.