Search code examples
asp.netasp.net-mvcsitecoresitecore7

Embed Language only for specific sites in Sitecore


How to embed the language/locale only for specific sites in Sitecore?

Let's say I have US site and URL for that will be http://exmaple.com

For the Canada site I have 2 languages, Hence the URL should be something like this.

http://exmaple.ca/en-ca

http://exmaple.ca/fr-ca


Solution

  • There's nothing out of the box to achieve this, since you can only set a single link provider and the settings apply across instances/usages of the solution.

    I have previously blogged a solution which I used to implement Site Specific Link Provider for Multisite Implementations in Sitecore which is essentially a switching link provider allowing you to define multiple Link Providers, each with different configuration for different sites.

    This would allow you to create two link provider configurations:

    <linkManager>
      <providers>
        <add name="provider-site1" languageEmbedding="never" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" ... />
        <add name="provider-site2" languageEmbedding="always" type="Sitecore.Custom.Links.SpecificLinkProvider, Sitecore.Custom" ... />    
      </providers>
    </linkManager>
    

    And then in your sites definition specify which provider to use:

    <sites>
      <site name="site1" linkProvider="provider-site1" ... />
      <site name="site2" linkProvider="provider-site2" ... />
      <site name="site3" ... />
    </sites>
    

    All the code code for this can be found in this Github Gist

    Alternatively, create your own Link Provider, inheriting from Sitecore.Links.LinkProvider and override the GetItemUrl method:

    public override string GetItemUrl(Sitecore.Data.Items.Item item, Sitecore.Links.UrlOptions options)
    {
        if (Sitecore.Context.Site.Name == "US-Site")
        {
            options.LanguageEmbedding = LanguageEmbedding.Never;       
        }
        return base.GetItemUrl(item, options);
    }
    

    And then patch this in to replace the default link provider:

    <linkManager>
      <patch:attribute name="defaultProvider">custom</patch:attribute>
      <providers>
        <add name="custom" type="MyProject.SiteLinkProvider, MyProject" languageEmbedding="always"  />
      </providers>
    </linkManager>