42

I am working on a (my first) Drupal 8 project witch is a multilingual site. Drupal 8 really delivers in terms of native support for content translation. However, I have a few strings set directly in some if the theme .Twig template files printed out like ex.

{{ 'Read more'|trans }}

I am really stuck on where to handle the translation for strings like these within the Drupal 8 installation.

What steps do I need to take to be able to translate these strings into the language currently selected for on the site? Do I need any additional modules other than the ones included in Drupal 8 core?

Steps I have taken:

  • Activated the Content Translation & Interface Translation modules
  • Added Twig strings like {{ 'Read more'|trans }}
  • Flushed cache and refreshed pages containing the strings
  • Checked admin/config/regional/translate for the strings with no result
2
  • 4
    Have you tried use |t instead of |trans?
    – Vagner
    Feb 24, 2016 at 12:45
  • @Vagner I have added the strings as shown in my question, where the string is suffixed with |trans
    – Henkealg
    Feb 24, 2016 at 12:53

8 Answers 8

42

You need to visit the page in a language that is not English to have them picked up. That should be enough.

1
  • 1
    Figures that it should be so easy. I have reloaded the page in several languages but not in the one where i had this problem. Just as you said the strings are translatable once you visit the page using that language setting. Thanks!
    – Henkealg
    Feb 25, 2016 at 7:09
32

Use the t filter:

{{ 'translate me'|t }}

https://www.drupal.org/docs/8/theming/twig/filters-modifying-variables-in-twig-templates

25

You can use the {% trans %} Twig tag, available in Drupal 8:

{% trans %}
  Submitted by {{ author.username }} on {{ node.created }}
{% endtrans %}

See the following link for more details: http://getlevelten.com/blog/mark-carver/drupal-8-twig-templates-and-translations

4
  • Thank you for your reply. I have read through that article as well but unfortunately it does not cover where I handle the translation for the strings added to the template files using {% trans %} or |trans, or |t. :S
    – Henkealg
    Feb 24, 2016 at 12:56
  • Thanks for the post. This work for me. After adding this code: {% trans %} Categories {% endtrans %} I cleared the cache and go to admin/config/regional/translate page. Where I searched my keyword "Categories" and get a textfield for translating the word in French (as an example) language. After this, I get the translated word in required place.
    – user50292
    May 28, 2018 at 6:41
  • Worked for me too! Dec 3, 2018 at 16:12
  • 1
    This does not work for me. I've added the strings to my Twig files as {{ 'my string' | t }}. I then cleared cache and visited pages in the target language, multiple times. I still can't find the strings on the /admin/config/regional/translate page. Aug 10, 2021 at 15:05
8

This question have already been answered correctly, but if you need to translate a string with variables, here's how you do it.

{{ 'Subscribe to @title'|t({'@title': title}) }}
6

Here is an example I used where the language is explict provided, as well as an context. Like this I was able to translate the sting 'von' twice, as in english it meant 'of' or 'from'.

{{ 'von'|t([],{'langcode': currentLanguageCode,'context':'Seitenzahl PDF' }) }}

1

this one helped me

{{ 'Read more'|trans }}

after this visit the page in a language that is not Original language to have them picked up by Drupal.

/admin/config/regional/translate

that s all.

1

It seems that Drupal considers custom translated strings as "User interface translation". To translate those, install "User interface translation" extend and then you find "User interface translation" in the configuration. There is a long list of strings, but your strings should located in the last section.

1

What was suggested in this answer above is what fixed the bug to me. It was impossible to me to find the strings. I went to the last page at

/admin/config/regional/translate

and there it was, but the search bar couldn't find it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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