Configuring‌ ‌multilingual‌ ‌content‌ ‌Views‌ ‌in‌ ‌Drupal‌ ‌9‌

20 Sep 2021

When Drupal 8 was released in November 2015, its list of new features was really impressive. Among lots of other things on the list, there were multilingual capabilities and the Views built into the Drupal core. Now that many websites have upgraded to Drupal 9, the Views and multilingual functionalities in Drupal have not changed much. Both remain highly popular among customers, website admins, developers, content editors — here is why:

  • First, almost every Drupal website uses Views to display any kind of content listings that can be set up from the admin dashboard. It is incredibly convenient to have this functionality available out-of-the-box.
  • Second, having a multilingual website opens lots of new business opportunities, so many businesses are either planning to make their websites multilingual or have already done that. Drupal 9 offers a super easy multilingual setup and lots of options here.

What do these two features have in common? Yes, there is often a need to display multilingual content in Views, and there are some subtle nuances related to unwanted content duplication that we are going to discuss in this post. But first, we will quickly run through what Drupal Views and multilingual options have to offer.

Drupal 9 Views: the favorite tool of many developers

Drupal Views are a tool that enables you to list your content (or users, comments, taxonomy, etc.) in any organized collection (grid, list, table, etc.) based on the settings you make. There is a user interface for configuring every smallest detail of how your view should be displayed to users. This eliminates the need for writing SQL queries to the database in order to fetch and output the necessary content — Views do this for you.

Filtering and sorting in Views make it easy to only select content with particular parameters (e.g. only published content) or position content in a specified order (e.g. from most popular to least popular items). You can choose to make your filters exposed to visitors so they decide how they want to sort and/or filter the content in your listings.

Some of the more advanced settings include contextual filtering that enables you to output dynamic values based on the current context and Views relationships that unite data from different base tables.

Your view can be a separate page or a block that can be placed inside your site’s layout. With additional modules installed, you can get a Drupal Views slideshow, a Drupal Views carousel, a calendar, or anything else (see our collection of extra modules to extend Drupal Views). Even without extra modules, there is a relatively new built-in option to share content in the JSON format to third-party apps — Drupal Views REST export.

A glimpse at Drupal 9 multilingual features

Unlike Views, multilingual features are not enabled by default on every website because not all websites need them. After enabling the multilingual module package (Configuration translation, Content translation, Interface translation, and Language), you get all Drupal 9’s multilingual benefits.

You can easily add as many languages as you wish to your website (there is built-in support for almost a hundred languages that can be just selected from the dropdown). Interfaces for them are already community-translated. One of these languages will be your website’s default language at your choice in one click.

You configure which content types and their elements should be translatable. Drupal also provides you with a basic language switcher block that can be placed in the header region or whenever you wish on your website.

Finally, just a few clicks are needed for editors to get a handy interface where they can add translations to content nodes. The admin language for the convenient work of editors with different native languages is also configurable. The translations they create for the nodes are something you may want to properly output in your multilingual view, and we are now moving on to this chapter.

Multilingual content Views in Drupal 9: issues and ideas

Let’s see how to use Drupal Views for translated content. We will create a view with content that has more than one language version. In our example, the website uses English and French, with English as the default language. We have prepared an article in English and translated it into French, so there currently are two language versions of the node.

In Structure > Views > Add view, we create a view that shows content of the Article content type in the format of a list of fields (however, the format does not matter much for this experiment, so you can select any you like).

After hitting “Save and edit,” we arrive at the main Views UI. Our display already has the “Title” field by default, so let’s also add an image field from Article, which we can do under “Fields.”

So what do we see as a result? The view was supposed to display the article title and image. And so it does — but in both languages.

Why does Drupal duplicate the node versions? The translations of a node are separate rows in the database, so when the node is queried by Drupal Views with no language specified, duplicates are returned.

How to avoid duplicates in multilingual Drupal Views?

The solution to this problem is using the Views filters and rendering options.

  • There are a couple of Views filters related to translations that are worth trying out for multilingual setups:
    • Translation language
    • Default translation
  • There is also the Language section in Drupal Views where the attention-worthy options are:
    • Content language of view row (is on by default)
    • Interface text language selected for page

There is a heated discussion on drupal.org about using these filters and rendering options in different combinations.

We would like to describe one of the combinations that looks very good and gives the expected results. Namely, it will show the right language version to a particular user based on their interface language and fall back to the default language if there is no translation in the necessary language. This will require two steps.

Step 1. Adding a filter in Drupal Views

Click “Add filter criteria” and find the “Default language” filter.

Click “Add and configure filter criteria,” and set the “Default translation” option to “True.” Finish the process by clicking “Apply.” It will filter the content items by the default language.

Step 2. Selecting the rendering language option

Go to Language > Rendering Language in the Drupal Views and select the “Interface text language selected for page” from the list of options.

Let’s not forget to click “Save” in our view and it’s time to see what our updated view shows in different scenarios.

  1. The view page in the site’s default language (English) shows the English version of the node. The duplicates are gone.
  2. When we switch the website to French, we see the French version.
  3. Let’s remove the French translation from Content. We can do this just by unpublishing as our view has the “Published” filter by default. We will visit the website in an incognito window to make sure we cannot see unpublished content. And we see the English original of the node because the fallback is working properly.

In summary

Both Drupal Views and multilingual options in Drupal 9 have lots of secrets. Knowing them helps developers create websites that are extremely flexible to their customers’ requirements. With any questions, ideas, and tasks about setting up content Views on your multilingual website — or if you are just planning to have one built for you — contact the Golems team of professional Drupal developers.

Comments

An
Anonymous
An
Anonymous
26 Sun 2021

Thanks for sharing.

I use 'Content: Translation language' is one of ' Interface text language selected for page' as a filter which seems to work as well. What is the difference between this and the 'Default translation' filter? And is it better to use the latter?

Robert Roose

In reply to by Anonymous (not verified)

An
Anonymous
30 Thu 2021

Robert - The 'Translation language' filter will mean fallbacks aren't shown. Which might be what you want! But on other sites, the ideal is to show a fallback so that content is always shown, even if it's not the 'ideal' language for the page. This was the example in step 3 at the end of the article, where the English translation was shown when the French one didn't exist.

This could be useful in an admin listing which just wants to list a single row for each independent node. Or a site listing user-generated content that isn't always translated but should always be shown - like amazon reviews, which show the relevant language first, followed by those from other languages. There are also modules like http://drupal.org/project/language_hierarchy which provide control over fallbacks, e.g. to still show <em>relevant</em> translations when multiple 'variants' of a language exist.

But sure, when you want to deliberately exclude content that hasn't been translated into the current language, use that Translation language filter instead.

(Note to the article author - posting a direct reply to Robert's comment didn't work, I don't know why!)

In reply to by Anonymous (not verified)

An
Anonymous
30 Thu 2021

Same for me ... would be nice to find out, what's the correct way!

An
Anonymous
07 Mon 2022

Thanks so much, this article was a lifesaver for our team today.