Using Display Name and Generating links for multilingual sites – Sitecore

About Display Name:

As many of us already aware, Display Name is used

In case of multilingual sites, where we required to generate links for pages specific to the language it is serving. For ex: there is a page in website named about-us and website is available in two languages English, and German. Now URL should be formed according to language as shown below:

This can be achieved by the feature referred as Display Name in Sitecore.

How to Assign Display Name

  • Create an item in Sitecore with the preferred name, in our example it is about-us.
  • For English language version display name will be same as item name so we will skip this part for English language.
  • For German language, Select the German language version of an item, click on Home (tab) -> Rename (chunk) -> Display Name command from the ribbon bar in content editor (You can also assign Display Name from Experience Editor).
  • A popup will appear expecting a text, enter the display name specific to the language which will then be used to form language specific URL. In our example equivalent to contact-us will be kontaktiere-uns, so enter this in textbox and save.
  • That’s it from the configuration part. You will see entered display name in content tree now instead of item name.

Generating Links While Using Display Name Feature

To generate URL specific to language using Display Name, You need to add useDisplayName=”true” into linkmanager in the sitecore.config. See below:


<linkManager defaultProvider="sitecore">
    <providers>
      <clear/>
      <add name="sitecore" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" addAspxExtension="false" alwaysIncludeServerUrl="false" encodeNames="true" languageEmbedding="always" languageLocation="querystring" lowercaseUrls="true" shortenUrls="true" useDisplayName="true"/>
    </providers>
</linkManager>

After enabling Display Name in link manager, you can now generate link using link manager as follow:

Consider context language as English, and we need to generate link for German language version of item using display name. We can do it simply using link manager in view as:


@{

Sitecore.Links.UrlOptions options = new Sitecore.Links.UrlOptions();
options.Language = Sitecore.Data.Managers.LanguageManager.GetLanguage("de-DE");
options.LanguageEmbedding = Sitecore.Links.LanguageEmbedding.Always;
options.LanguageLocation = Sitecore.Links.LanguageLocation.QueryString;
options.UseDisplayName = true;

string URL = Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Item, options);

}

Will this generate the link we desire for German (de-DE) language? No.

The link generated using the above code will look something like http://[hostname]/about-us?sc_lang=de-DE

Why we are showing about-us in the URL instead of the kontaktiere-uns even though we have supplied language de-DE in UrlOptions.

After drilling down to Linkmanager code, come to know that language passed into UrlOptions not getting used while getting Display Name, It is getting Display Name of the item provided as first parameter of GetItemUrl()

In our case we are passing context item, and context language is English. As we do not have provided any Display Name for this item in English language, it will use Item Name itself to generate the URL.

So, we need to get the item in specific language before passing to GetItemUrl() function to form valid URL specific to language with help of Display Name. See below:


@{

string URL = Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Item, options);

}

In place of above line use below code:


@{

string URL = @Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Database.GetItem(Sitecore.Context.Item.ID, Sitecore.Data.Managers.LanguageManager.GetLanguage("de-DE")), options);

}

So, above code will first get German version of the about-us item, and thus will form a valid URL using Display Name provided for German language.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s