TransWikia.com

Language variable doesn't work in Twig?

Drupal Answers Asked on March 4, 2021

I have to put a several languages in a site. But I just discovered that the taxonomy terms without a language (because it’s the same label for every language) gives the link toward a page in the default language on the site.
On the other hand, taxonomy vocabularies that have translations work fine. Just the taxonomy vocabularies without translation (and no language defined) give me troubles.

So when you click on a taxonomy term in the default language (French), you go to the French taxonomy term page. When you click on the taxonomy term in Chinese, you still go to the French taxonomy term page.

I don’t understand why and I seek a way to fix the problem.

I tried using {{language}} in the template, so I could add the language code prefix to the link, but it renders nothing.

I added the following preprocess function, but using {{ language }} in the template still doesn’t render anything.

function gavias_monte_preprocess_page(&$variables) {
  $language = Drupal::languageManager()->getCurrentLanguage()->getId();
  $variables['language'] = $language;
}

One Answer

I though that using mymodule_preprocess_page will modify all pages (as DSM was working everywhere). Seems not for the variables... I tried mymondule_preprocess_node but it crashes the site. So I tried this...

function gavias_monte_preprocess_node__lieu__full(&$variables) {
    $language = Drupal::languageManager()->getCurrentLanguage()->getId();
    $variables['language'] = $language;
}

To my surprise it works. But that means I pass the language (and any other kind of variables) just for this specific template.

Answered by Erik on March 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP