Translations for custom modules and themes in Drupal 8

Translate multilingual sites where Drupal 8 custom code is located in custom modules or themes.

Adding translations to your module

After collecting all strings in a .po file (check potx) you must specify in your module where translations are located. You have to do this in by adding the following lines:

'interface translation project': your_module
'interface translation server pattern': absolute/path/to/your/module/your_module/translations/%language.po

With this you are instructing Drupal to look in your module, inside a folder called translations. Then will look for files with the pattern langcode (en, de, es…) plus the .po extension. Note that the path to the translations must be relative the Drupal root. So, for example it will look for:


Loading the translation updates

You can load your translations with Drush commands:

drush locale-check
drush locale-update

After this you have to clear render caches:

drush cr

But the new translations are tracked when modules and themes are enabled, not for file updates, so you can enforce it with:
For themes:

drush ev "\Drupal::service('theme_handler')->refreshInfo();"

For modules:

drush ev "Drupal::moduleHandler()->loadInclude('locale', '');  locale_translation_check_projects_local(['your_module']);"

All posts by Julià

Our most read posts

10 questions for Tom Hare

Today, Dev-Strategist Tom Hare is going to answer 10 questions about his work and life at the netzstrategen. 1. When did you become part of the team? I joined the netzstrategen team in May 2017, shortly after moving to Karlsruhe …

10 questions for Luca Pipolo

Today Dev-Strategist Luca Pipolo is going to answer 10 Questions about his work and life at the netzstrategen company. 1. Since when have you been part of the team? More or less since March of this year, when I left …