5

We have a site where I need to add hreflang on each page, linking to each language version of the page.

<link rel="alternate" href="https://domain/pants" hreflang="en">
<link rel="alternate" href="https://domain/bukser" hreflang="da">

The LinkManager is configured to use displaynames (useDisplayNames="true"):

<linkManager>
    <patch:attribute name="defaultProvider">custom</patch:attribute>
    <providers>
      <add name="custom" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" addAspxExtension="false" alwaysIncludeServerUrl="false" encodeNames="true" languageEmbedding="always" languageLocation="filePath" shortenUrls="true" useDisplayName="true"/>
    </providers>
</linkManager>

I have tried getting the correct url by adding language to the UrlOptions and parse it to the LinkManager, but I keep getting the URL for the current langauge:

var urlOptions = UrlOptions.DefaultOptions;
urlOptions.Language = language;
var link = LinkManager.GetItemUrl(item, urlOptions);

I have tried using a LanguageSwitcher, with the same result:

using (new LanguageSwitcher(lang))
{
    url = LinkManager.GetItemUrl(Sitecore.Context.Item, options);
}

It is a Sitecore 7.2

Anyone know how to get the language specific URL when using 'UseDisplayName' ?

4

You're still trying to generate link to the item version which is in your language.

Try getting the other language version first:

var otherLanguageVersion = Sitecore.Context.Item.Database.GetItem(Sitecore.Context.Item.ID, language);

And then call:

LinkManager.GetItemUrl(otherLanguageVersion, options);    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.