0

I have a taxonomy vocabulary with terms.

In a content type, I have a referenced field with this taxonomy.

I can get the taxonomy term in twig like this :

{% for item in node.field_lorem %}

    {{ item.entity.label }}

{% endfor %}

It works. But I don't know hot to get the translated terms when I switch language.

All other basic fields are translated. The multilingual configuration seems ok.

Example, a text field {{ content.field_text }} is translated.

The translation is activated for taxonomy (Taxonomy term is checked).

/admin/config/regional/content-language

The termes are translated :

taxonomy/term/11/translations

Is there something I miss in my twig ? Actually, I always get the terms in default language.

9
  • 1
    Like the text field {{ content.field_lorem }} will be translated as well when you configure the field formatter to display entity labels. In custom code you have to translate the terms yourself, which is probably only possible in PHP, see drupal.stackexchange.com/questions/252957/…
    – 4uk4
    Sep 18, 2020 at 16:48
  • Does this answer your question? How do I load taxonomy terms basing on the current language?
    – leymannx
    Sep 18, 2020 at 16:52
  • @4k4 {{ content.field_lorem }} is correctly translated in front-end. I did nothing except {{ content.field_text }} in twig. The process is different for taxonomy ? Is this because of the referenced field or is it the taxonomy ? I can do it with PHP. I should start with a custom module or can I do it in my-theme.theme ? I will cheek your link, thanks ! Sep 18, 2020 at 16:52
  • @leymannx I've already seen this link but didn't dig deeper because I was searching for a twig solution. It seems that according to 4k4 it is not possible only with twig. Sep 18, 2020 at 16:56
  • 1
    You can do this in theme preprocess, load the terms with $node->field_lorem->referencedEntities() and then translate them like in the linked topic.
    – 4uk4
    Sep 18, 2020 at 17:03

1 Answer 1

1

Maybe I poorly explained the question but the solution was possible without preprocess.

In a content type, the referenced field with a taxonomy is called field_lorem

For this Entity reference field (Type of item to reference : taxonomy term), we select in Manage display (admin/structure/types/manage/content_type_name/display) « Rendered entity » instead of Label :

enter image description here

In the node template (node--lorem-ipsum-content-type--full.html.twig), we have simply {{ content.field_lorem }}

Then, we are able to see twig suggestion template name for this field based on taxonomy_term.html.twig :

enter image description here

In this twig, we can style independently all of the fields of the taxonomy term :

    {{ content.field_image[0] }}

    <p>{{ name[0] }}</p>

With this solution, translation of vocabulary terms works and we are able to have a custom template with all the field (in example an image field and name of the term)

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.