Drupal Programmatic Translations R&D

Pete Inge
Bluecadet
Published in
3 min readMar 19, 2021

We’ve been having a lot of requests lately for multilingual sites or the ability to upgrade to multilingual in a later phase. (To be honest, I thought by now the web world here in the U.S. would almost be required to build sites in English and Spanish. I’ve started learning Spanish 5 times over the past 10 years anticipating this shift.) Primarily working in Drupal, I’ve played around a lot with the multilingual capabilities and am pleased with it, but have yet to put a multilingual site out into the real world.

Here at Bluecadet, most of our multilingual sites we have developed in WordPress, but when we get into custom integrations, we move into the Drupal realm. I’m constantly programmatically creating and updating nodes with data from external sources and I’ve been curious about how this would work within a multilingual Drupal site. I did not think it would be a big deal, just another programming pattern to learn how to code in Drupal.

An example of a previous WordPress site we built with multilingual support. With offices around the world, the Humanity in Action website supports six different languages.

The quick test

To begin, I just made my local Drupal 9 test site multilingual, with English and Spanish enabled. There are a lot of helpful articles out there to get you started if you are curious, but it is not crazy. Moving around the admin side of the site though, you have to be careful. I just need to slow down and be purposeful about what I am clicking.

After it was all set up, I simply played around in Drupal Devel’s PHP editor, using the default “Article” Content Type that is present when you install a site. The process was just a simple step to get the node translation. Editing and updating the node and its translation was as straightforward as expected. Below gets into some of the details.

Time to get into the weeds

If you regularly work with your nodes in code, this should look familiar.

  1. Started out creating a node as usual:

2. Now I create the Spanish translation and save it:

3. And now to edit the node, we load and edit the English version:

4. And load the Spanish translation and edit:

Unlike most things with Drupal, this came together pretty quickly with little fuss. I’m sure as I dive in further there will be a lot of nuances to discover, but I can’t wait to work on a multilingual integration for a production site.

For those interested, here is the full Github gist:

--

--