Craft CMS: multilingual websites

26 February 2019

hosting multiple pages in one installation

Craft CMS allows hosting multiple pages in one installation. Thanks to this solution, it is possible to define multiple sites in different domains, using different sets of templates, and separate content entries. The above solution is used, for example, to create multilingual websites.

Static and dynamic content

The basic division of content on the site is static content - the change of which is not possible without interference in the code (usually it is text on buttons and similar elements, entered directly in the HTML code), and dynamic content, which are entered in the appropriate fields in Craft ' you.

Static content

  • In the first step, use the translation filter for all static content in the HTML structure.
{{ ‘’ |t }}

in practice, it looks like this

<a href="/contact">{{ 'Contact us'|t }}</a>
  • Then, it is necessary to create folders and translation files in the page file structure.

In the project folder, we create the translations folder, and in it, subfolders for each language to which we want to translate our site. In the subfolder of each language we create a site.php file

Przykładowa struktura plików:

├── config/
├── ...
└── translations/
 └── fr/
 └── site.php
  • In the site.php file, we place an array that is intended to link the original content to its translation.

Dynamic content

To translate dynamic content, log in to the Craft administrator panel, and then follow the next steps.

1. Defining pages and languages

In the page settings, in the website tab, we create a new site, completing the next fields.

  • Group - select the group to which the page belongs (may remain the default)
  • Name - name of the page (for example Craft-en)
  • Handle - created automatically based on the name
  • Language - select the language and its ID
  • Is Primary Site? - check if it is to be the default page
  • Base URL - choose the base URL of the created page

2. Update the section

In the page settings, in the sections tab, you should enable the page for each language in each section, and complete the Template fields for the Channel and Structure sections, as well as the URL for all sections.

3. Update the fields

In the page settings, in the field tab, for each field that you want to translate, select the translation method.

4. Text entry

The entries have created the option to choose the version of the page. By choosing a previously created language version, we are able to write entries in the selected language and publish them on pages created under the addresses defined in the second point.