Load translations from custom Drupal 8 modules

3 minuten lezen

Whenever you have built a Drupal 8 module for a non-English site you will have had this issue. You have strings that are English in code using the t() function, but you want to have them translated during your release. How does this work? I'll show you!


The requirements

First of all we need a module to extract the English strings from our custom module. You can use Translation template extractor for that.

First download the module: composer require drupal/potx

The enable it: drush pm:enable potx (Only enable this on development environments)

Now you have the module installed. From now on you can use the module via the interface on admin/config/regional/translate/extract. Just select your module, the template file for the language you want to export for and hit extract.

What to do with the extracted file?

The extracted file can be placed in a subfolder of your module (for example ‘translations’) and can be edited with every basic text editor or IDE.

An example of a translation can be:

msgid "The description of my module."
msgstr "De beschrijving van mijn module."

The msgid is the string to translate and the msgstr is the translated string. In this example from English to Dutch.

Once you have translated all your string you need to make your module aware of the translations. You can do this by adding some lines to you module_name.info.yml file.

name: 'My module name'
type: module
description: 'My module description'
core: 8.x
interface translation project: my_module
interface translation server pattern: 'modules/custom/my_module/translations/my_module.%language.po'

The last two lines in this example show you how you can make your module aware. Notice the %language variable? This makes sure that you can use separate translation files for each language. Just make sure you add the language code to the filename, for instance 'nl' for Dutch or 'se' for Spanish.

Importing the translations

Importing the translations is as easy as running two drush commands that (as far as I’m concerned) you should run on every release.


First: drush locale-check

This command checks for available translation updates for your installed modules. Both custom and contributed modules and themes and checks (when default settings are applied) on both the Drupal localization server and the local filesystem.

Next: drush locale-update

This command actually updates the translations. This only updates new or non-translated strings so you don’t need to worry about losing your handmade translations.

Meer weten

Fabian de Rijk

Senior Drupal Developer0640869887[email protected]0640869887LinkedInTwitterDrupal
Meer van Fabian de Rijk

Meer artikelen

liferay
dxp
freemarker
fragments
webcontent

Using Liferay Fragments as modular webcontent templates

Danielle Ardon
9 minuten lezen
Lees het artikel Using Liferay Fragments as modular webcontent templates
liferay
dxp
theme
inheritance

Liferay themes with a custom base theme

Geert van der Ploeg
6 minuten lezen
Lees het artikel Liferay themes with a custom base theme
liferay
freemarker
dxp

Preferences for embedded portlets in Freemarker

Geert van der Ploeg
4 minuten lezen
Lees het artikel Preferences for embedded portlets in Freemarker