Skip to main content

Drupal 8 string overrides


With the new language system in core the need for most of the functionality of the string overrides module is not really needed. That is if you just want to replace a string in general or have added the right context using the {% trans %} filter in your twig templates (see documentation for a more detailed overview)

Bare in mind, that translating strings is not the same translating content. Drupal makes a clear separation between interface and content translation. All the strings looped through the translation using the t() method are interface text not content. Also you need to keep in mind that the system always translates from english to another language. Even if your site is in Dutch, you still want to write the translatable strings in english and then translate them. 

Enabling the system.

So to translate strings, you first need to enable the language system and the interface modules, yes even if you only have one language.

enable modules

After installation, you need to enable the interface translation for the language or languages you want to have custom strings. By default, english is marked as not translatable.  Goto /admin/config/regional/language and click the edit button to change that.

Click the checkbox for "Enable interface translation to English" and press save language.

Enable interface translation

Overriding the strings.

If you now visit /admin/config/regional/translate you will notice that all the system strings are now overridable in the interface. Like always the strings are case sensitive and need to be entered exactly like in your template or module. Like in Drupal 7, this system only discovers new translatable strings after they have been looped through the t() function. So you string does not show up in the list, try to load the page first, preferably after a cache clear and being logged in as a admin user.

Storing the translations in GIT.

Personally I like to change the location of anything related to the configuration system outside of the public webroot. And the .po files containing the string overrides is no exception. First you might want to change the location of the translation files from the default "sites/default/files/translations" to something else. Goto /admin/config/media/file-system and change the "Interface translations directory" to something outside of your webroot. In my projects i create a private folder on the same level as the public folder. So set the translation directory to '../private/translations'

By doing this only the imported language files are saved in that directory. That means every language except english. So you might want to create a extra directory that holds your custom translations if you only export the customized translations or you end up either translating things over and over again, or exporting the translations manually after every update.

Export only what you need

Goto /admin/config/regional/translate/export pick the language you changed the strings for and check that you only want to export the customized translations. And press export. This downloads a .po file you manually need to place in the above created translations directory and commit that file into your repository.

Export strings


Although not 100% perfect and very developer friendly the new Drupal 8 language system is a serious improvement over the old D7 system. And I am convinced that somebody in contrib will find the need to scratch the itch of some of the DX issues like saving a .po directy into the languages directory instead of downloading and copying it.