Is it possible to provide translation strings to Twig, without the Interface Translation module enabled?

We have Content Translation enabled but do not want to enable Interface translation (we would like the Drupal administration menu system to remain in English).

We have a couple of landing pages that base most of their content on a translated entity however, there are still a few labels and links we would like to translate using {{ 'foo'|t }} in Twig.

Is this possible? And if so - where would the translation stings be stored?

  • While not directly answering this question, it's related: if you want your theme to translate to content language instead of the interface language, we just released the Translate to content language module. Jun 20, 2020 at 7:24

1 Answer 1


Our solution in the end was to create a new Twig extension called tr.

The class of the extension plugin checks to see if a 'translations' directory exists in the current theme. If it does, it reads the containing YAML translation files into an associative array (en.yml, es.yml, fr.yml etc.)

When {{ tr('source string') }} is called in the twig template, it will check the loaded associative arrays to see if there is a translation available for the current language and requested string. If there is it will return the translated value.

This works for us since in our case we're only translating a fairly short list of values - short strings and labels like 'Read More', 'Learn More', 'Subscribe' etc. The rest of our translated content comes from the content translation of the entities.

Hope this helps...

  • Sorry to comment on such an old thread. I have a similar need. I've tried using Interface Translation to do it. I set it not to download the translation file from the server, so the only translations I get are the custom ones I put in. But whenever I go to a page marked as, say, Spanish, Drupal wants to think I'm in Spanish mode from that page, so if I edit, for example, it will take me to /es/node/XXX/edit instead of to just /node/XXX/edit. Any chance you'd be willing to share what you wrote for the tr function? I haven't done a twig extension before. Mar 19, 2019 at 15:28
  • Hi Marshall - in the end we wrote a translation extension (writing from my phone so dont have the exact link or term), and then stored our translations in YAML files on disk. This meant we could use the built in t functions to pick up our translations irrespective of any languages we had enabled on the site. Mar 22, 2019 at 14:54
  • Just looked it up - we wrote our own string_translator service with priority: 100 - which was implemented using a class that extends Drupal\Core\StringTranslation\Translator\StaticTranslation api.drupal.org/api/drupal/… Mar 22, 2019 at 15:05
  • Thanks kindly Blue. Does your solution require Interface Translation to be installed? I ended up removing it entirely because of the negative side effects I mention above and hacked something together, but really need to do something more permanent. If you can share non-proprietary code, anything that would give me a head-start would be much appreciated, although I understand if that's not possible. Mar 22, 2019 at 17:30
  • Blue, one specific question: When a page is displayed that does translation via your tr() function in its twig template, how do you determine which language to translate to? Is there a way to look at the node behind the page to see its language setting? Or do you pass the language in the query string? Etc. Mar 22, 2019 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.