45

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
    Commented 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
    Commented Feb 24, 2016 at 12:53

9 Answers 9

47

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

2
  • 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
    Commented Feb 25, 2016 at 7:09
  • You also have to be logged out
    – wranvaud
    Commented Dec 7, 2022 at 0:14
38

Use the t filter:

{{ 'translate me'|t }}

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

26

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
    Commented 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
    Commented May 28, 2018 at 6:41
  • Worked for me too! Commented 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. Commented Aug 10, 2021 at 15:05
14

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' }) }}

2

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.

2

You can translate string in twig using the following functions.

You need to visit the page so the string is added in the translation database.

With filter

{{ 'Hello Earth.'|trans }}
{{ 'Hello Earth.'|t }}

with trans

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

with variables

<p class="submitted">{{ "Submitted by !author on @date"|t({ '!author': author, '@date': date }) }}</p>

plural

{% set count = comments|length %}
{% trans %}
  {{ count }} comment was deleted successfully.
{% plural count %}
  {{ count }} comments were deleted successfully.
{% endtrans %}

This is also documented here https://www.drupal.org/docs/theming-drupal/twig-in-drupal/filters-modifying-variables-in-twig-templates#trans

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.

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.