Creating a Multilingual Site
A guide to multilanguage setup in Umbraco
You can use language variants to setup a multilingual site. Language Variants allow you to have variants of the same content all under the same project. So, if you open a page and a language variant is enabled, you will see the option to switch the language from the drop-down list. Additionally, you can view or input the translated content.
This tutorial explains how to set-up a basic multilingual website.
Adding a New language
To add a new language, follow these steps:
Go to the Settings section.
Go to Languages in the Structure tree.
Click Create.
Select a Language from the dropdown list. In this tutorial, we will pick Danish.
In Settings, select the following options to set the new language as the:
Default language for your site, toggle Default Language.
Mandatory language for your site, toggle Mandatory Language.
Select a Fallback Language.
Click Save.
Adding Multiple Languages
We can add multiple languages depending on our website requirements. In the previous step, we have already set Danish as our default language. We will now set-up English and German as our variants for this tutorial.
Go to the Settings section.
Go to Languages in the Structure tree.
Click Create.
For English Variant:
Select English (United States) from the drop-down list.
Click Save.
For German Variant:
Select German from the drop-down list.
Toggle Mandatory Language option.
Select Danish from the Fallback Language drop-down list.
Click Submit.
Click Save.
Changing the Default Language of a Website
To change the default language of a website:
Go to the Settings section.
Go to Languages in the Structure tree.
Select the language you want to set as the new default language.
Toggle Default Language.
Click Save.
To change the default backoffice language, update the Umbraco:CMS:Global:DefaultUILanguage
value in the appsettings.json
file. For more information, see the Global Settings article.
Changing the Default Backoffice Language of a User
To change the default language of a User:
Go to the Users section.
Select the user whose backoffice language you wish to change.
Select the new language from the UI Culture drop-down list.
Click Save.
Document Types
For this tutorial, we will create the following document types:
Home Page
Blogs
Contact Us
Enabling Language Variants on Document Types and Properties
To enable language variants on Document Types, follow these steps:
Go to the Settings tab.
Select Contact Us from the Document Types folder.
Go to the Settings tab and toggle Allow vary by culture
Click Save.
Go to the Design tab.
Click on the Data Type of the Page Title and toggle Vary by culture.
Click Update.
For this tutorial, we will not make any changes to the Address.
Click Save.
Viewing the Language Variants in the Content section
When you return to your content node you will notice two things:
At the top of the content tree, there is a dropdown to view the content tree in the language of your choice.
To the right of the content name, there is now a dropdown where you can select a language. You can also open a split view so you can see two languages at once.
Adding Culture and Hostnames to the Root Node of the Website
To add culture and hostnames, follow these steps:
Go to the Content tab.
Click on the ... dots next to the Contact Us content node.
Select Culture and Hostnames.
Add a domain for each hostname, like it's done here:
Click Save.
Using Side-by-Side Mode for Editing Content
To use side-by-side mode for editing content at the same time, follow these steps:
Go to the Contact Us node. You will find a language dropdown next to the title at the top:
Click Split view. In this splitview, we can see the content node with each language side by side.
You may notice that the Address and other fields are greyed out - this is because we haven't checked the Allow vary by culture checkbox.
To enable these fields, follow the steps mentioned in the Enabling Language Variants on Document Types and Properties section.
Adding Language Variants to the Content
To add language variants to the content.
Go to the Contact Us node.
Enter the Name for your content node and the Page Title in the new language.
Click Save and Publish. The Ready to Publish window opens providing the option to publish in one or more languages.
You can select either one or multiple languages and click Publish.
Rendering Variant Content in Templates
To render the values of the Contact Us page, use the following in the template:
The .Value()
method has a number of optional parameters that support scenarios where we want to "fall-back" to some other content, when the property value does not exist on the current content item. To use the fallback type, add the @using Umbraco.Cms.Core.Models.PublishedContent;
directive.
To display a value for a different language, if the language we are requesting does not have content populated:
For more information, see the Using fall-back methods article.
Using Dictionary Items
Depending on how your site is set up, not all content is edited through the content section. Some of the content may be written in the template or labels of the content node and dictionary items can play a part here. Dictionary items store a value for each language. They have a unique key and can be managed from the Translation section. For this tutorial, let's add dictionary items for the Address and Contact Number labels of the Contact Us page.
Creating Dictionary Items
To create dictionary items:
Go to the Translation section.
Click on the ... dots next to Dictionary.
Select Create dictionary item.
Enter a Name for the dictionary item. Let's say Address.
Enter the different language versions for the dictionary item.
Click Save.
Similarly, we will add different language versions for the Contact Number field.
Rendering Dictionary Items
To render dictionary items in the template, replace the text with the following snippet:
Translating Multi-Value Property Editors
Property editors such as Dropdown, Checkbox List, and Radiobutton List enable you to specify a set of options for selection within the Content section. By using dictionary items, you can ensure that these options are appropriately translated according to the current language of the site.
To use property editors with dictionary items:
Create a corresponding dictionary item with the same name for each option in your property editor. For example:
KeyEnglish ValueFrench ValueElectronics
Electronics
Électronique
Clothing
Clothing
Vêtements
Books
Books
Livres
Toys
Toys
Jouets
In your Document Type, set up a property editor and use the keys of the dictionary items as the Options:
Option 1: Electronics, Option 2: Clothing, Option 3: Books, and Option 4: Toys
Use the following code in your template to retrieve and display the translated value:
The code retrieves the selected value from the property editor and translates it using the corresponding dictionary item.
Adding a Translator to the Website
You can assign a Translator when you need a 1-1 translation of your site. For example, let's say we originally created a website in "Danish" which works from a .dk
domain and now there is a need for an "English" site on a .com
domain. In this case, it might be easier to copy the entire danish site and then provide access to a Translator who can then translate the site page by page.
Translators are used for the translation workflow. By default, Translators have permission to Browse and Update nodes. Someone must review the translations of site pages before publishing the nodes. For more information on managing User Groups, assigning access or User permissions, see the Users article.
Viewing the Language Variant on the Browser
To view the language variant on the browser, follow these steps:
Go to the Content tab.
Select your new language from the language dropdown above your content tree.
Select the Contact Us node and go to the Info tab.
You will notice the links with the new language domain added to it. If it's not there, you might need to refresh the page.
Click on the link to view the new language varied node in the browser.
Alternatively, you can add the domain name to your localhost in the browser. For example:
http://localhost:xxxx/da/
For viewing purposes, I've added a stylesheet to my website. The final result should look similar to the image below:
Danish Version:
German Version:
Using Multiple languages across APIs
When requesting content over an API, the culture will fall back to the default, unless explicitly set.
To do this, you can use the IVariationContextAccessor.
Creating a Language Switching Navigation
To navigate between languages, you need to do two key things:
Get all the languages that the site can provide
Identify the language used on the current page
Once you have these, you need to loop through the languages and provide links to each home node.
Getting all the languages for a site
There are three ways to achieve this. The best one is to use languageService.GetAllAsync();
which retrieves items from the cache.
Another is to use localizationService.GetAllLanguages();
to call the database, which is expensive and ideally includes caching. This should only be done if you cannot use the ILanguage service. This service is marked as obsolete.
The alternative is to get the Home node and find all of the cultures associated with it. This has a few benefits including speed and providing us with a link to show the user. It is the process you will use when following this guide.
Identify the language for the current page
This is achieved in cs.html
files using umbracoHelper.AssignedContentItem.GetCultureFromDomains();
Steps
Now that you have what you need, take the following steps to create a working example.
Create a new view called
Navigation.cshtml
Paste in the following code:
Replace
{{homeNodeContentAlias}}
with the Document Type alias of your Home node.
This will render links to either the language variant of the current page or the home node for the language variant.
Additionally, System.Globalization.CultureInfo
is used to obtain the native name of the language being rendered. This is useful if a user does not speak the default language.
Last updated