I am trying to override the titles of the nodes on my view page for Drupal 8. As my whole website is in English, but only on one view page I need to fetch the french translation of the titles, without translating the whole interface.

For this I am overriding the views-view-field.html.twig template with custom views-view-field--my-view--title.html.twig.

In the preprocess function in MYTHEME.theme I am sending the language = 'fr':

function MYTHEME_preprocess_views_view_field__MYVIEW__title(&$variables) {
  $variables['language'] = 'fr';
  $variables['#cache']['contexts'][] = 'route';

The Twig template receives the language, but still displays titles in English. This is how my Twig template currently looks like. How can I say to fetch output in French?

{{ output }}


thanks to Kint sub-module I could get all possible tokens. Now I need to fetch the right translation.

How can I reach french translation? I have tried already few options:

{{ row._entity.title.value.translation('fr') }}
{{ row._entity.title.value.fr }}
{{ row._entity.values.title.translation('fr') }}
{{ row._entity.values.title.fr }}

None of them is working. As I can see the values is an array, then it should be some special may accesing the elements inside

  • Please see the linked post. You can get the current language without preprocessing in Twig directly via {{ language.getId() }}.
    – leymannx
    Commented Feb 13, 2019 at 11:41
  • Oh, now I understand what you want. I'll update the title.
    – leymannx
    Commented Feb 13, 2019 at 11:44
  • The current language is English, I don't need it
    – user91143
    Commented Feb 13, 2019 at 11:45
  • The answer to How to render Entity Reference fields in the current language probably has your solution.
    – leymannx
    Commented Feb 13, 2019 at 11:50
  • Have seen this post already. Replacing {{output}} with {{node.field_data.entity.translation('fr').title.value }}, and many other variations brings nothing.
    – user91143
    Commented Feb 13, 2019 at 11:55