Sitecore Cookbook for Developers
上QQ阅读APP看书,第一时间看更新

Creating multilingual content pages

Till now, we created pages in the en (English) language, which is the default language in Sitecore. Sitecore supports all the languages or cultures that are supported by the .NET framework.

In this recipe, we will create content pages in multiple languages (English, Spanish, and Italian) and create a language switcher component to switch and view pages in these languages.

How to do it…

Sitecore by default contains the en (English) language. We will first create es-ES (Spanish) and it-IT (Italian) languages:

  1. From the Content Editor, select the /sitecore/system/Languages item. Add a new language, Spanish, as shown in the following image. Similarly, create the Italian language as well:
    How to do it…
  2. Now, from the Content Editor, select any content item on the right-hand side pane. You will find a language drop-down menu; clicking on this will display all the languages created in Sitecore, as shown in the following image:
    How to do it…
  3. Select Spanish from this drop-down; it will navigate to the Spanish language content for that item. To create a version for the item in the Spanish language, click on the Add a new version button. Now enter your content in the Spanish language. You can do the same for the Italian language for all the other items.

    We will now create a language switcher component to switch to different languages from content pages of the website.

  4. In the SitecoreCookbook project, Controller folder, create an action method, LanguageSwitcher(), in the NavigationController class file, as follows:
    public ActionResult LanguageSwitcher()
    {
      Dictionary<string, string> list = new Dictionary<string, string>();
      LanguageCollection langColl = LanguageManager.GetLanguages(Context.Database);
    
      foreach (Language language in langColl) {
        string url = GetItemUrl(Context.Item, language);
        list.Add(language.Title, url);
      }
      return View(list);
    }
  5. Create the GetItemUrl() method to get a language-specific item URL. We can use UrlOptions to get a language-specific URL, as mentioned in the following code:
    private static string GetItemUrl(Item item, Language language)
    {
      string url = LinkManager.GetItemUrl(item,
        new UrlOptions {
          LanguageEmbedding = LanguageEmbedding.Always,
          LanguageLocation = LanguageLocation.FilePath,
          Language = language
        } );
      return url;
    }
  6. Create a LanguageSwitcher.cshtml view in the \Views\Navigation folder to show all the languages and relevant URLs for the current item:
    @model IDictionary<string, string>
    <div id="languageswitcher">
      @foreach (var language in Model) {
        <div>
          <a href="@language.Value">@language.Key</a>
        </div>
      }
    </div>
  7. Add this controller to Main Layout to render the language switcher on the header. By clicking on the languages, you can switch to that language for the current content page. The following output shows a page in the Spanish language. You will find that your URL for the Spanish language will look like http://sitecorecookbook/es-ES/products/phones/iphone-6s, and the same for other languages:
    How to do it…

Note

The Language fallback feature is launched in Sitecore 8.1 so that if any item or field does not have content in the requested language can still serve other language content set as fallback. Sitecore supports language, template, item, and field-level fallback language. Learn more about this from http://goo.gl/EtQVfa.

How it works…

By default, Sitecore does not embed language in the item URL. So, we used UrlOptions to get a language-based URL. Instead of using UrlOptions, we could also have achieved URLs by doing the following setting in the \App_Config\Sitecore.config file: (This configuration file got introduced from Sitecore 8.1. In earlier versions, you can find it in the Web.config file.)

How it works…

There's more…

It's also possible to achieve language-specific unique URLs or language-specific friendly URLs, using the Display Name field of an item. Display Name field value can act as a language-specific item name, and will be considered while generating item URL as well. Learn more about this from http://goo.gl/b28s2y.