5

I need to enable thousands of languages on my Drupal 7 site, but I do not want to incur the rendering overhead of creating the language switcher sidebar for every language. The default behavior for the language switcher block is to show languages which do not have translations in gray font and struck out.

How would I modify the language switcher block to only show active links, i.e. only display the languages for which a translation exists? I looked at this code from this post but need help adjusting it for my needs:

<?php 
  function YOURTHEMENAME_links__locale_block($variables) {
  foreach($variables[links] as $key => $lang) {
    if (isset($lang['attributes']['class']) && in_array('locale-untranslated', $lang['attributes']['class'])) {
      $variables['links'][$key]['href'] = '<front>'; // Set here any page link...
    }
  }
  return theme_links($variables);
}

What should the code be, and where do I put the code in my theme?

2
  • Update: I tried the suggestion in this comment (groups.drupal.org/node/139329#comment-599034) but it does not work. The links still appear as before.
    – cyboreal
    Feb 21, 2013 at 15:37
  • 1
    After trying this, I don't think you need to do anything else. What is happening? What is not working? I have tried same code and everything is working fine. Oct 1, 2014 at 15:07

3 Answers 3

0

There is also a different way to determine missing translations. Namely, instead of looking for a locale-untranslated CSS class for the link, you can check if the link is the same as the one for the current language. Because that is the Drupal Core default behaviour for the language switcher in case a translation is missing. So:

function YOURTHEMENAME_links__locale_block($variables) {
    global $language;

    if (isset($data[$language->language])) {
        $current_href = $variables[links][$language->language]['href'];
    }
    else {
        $current_href = false;
    }

    foreach ($variables[links] as $key => $lang) {
        // If we find a missing translation, remove its entry from the list.
        if ($lang['href'] == $current_href) {
            unset($variables[links][$key]);
        }
    }

    return theme_links($variables);
}

Again, this function would go into your theme's template.php.

0

To adapt the solution you tried, you would just have to unset entries for languages where no translation exists, so no link will be printed for them in the theme_links() call. So:

function YOURTHEMENAME_links__locale_block($variables) {

  foreach($variables['links'] as $key => $lang) {
    if (isset($lang['attributes']['class'])) {
        if (in_array('locale-untranslated', $lang['attributes']['class'])) {
            unset($variables['links'][$key]);
        }
    }
  }
  return theme_links($variables);
}

You would put this code into the template.php file of your theme, adapting the YOURTHEMENAME part of the function name above according to how all the other functions in your theme start. Means, you always start the function name of all your theme functions with the so-called "machine name" of your theme.

2
  • Thanks for sharing. This is exactly what I was looking for. However when I show all Drupal log messages, it says Notice: Use of undefined constant links - assumed 'links' in basetheme_links__locale_block() (line 1252 of /Applications/MAMP/htdocs/d7-site/sites/all/themes/basetheme/template.php). Notice: Use of undefined constant links - assumed 'links' in basetheme_links__locale_block() (line 1255 of /Applications/MAMP/htdocs/d7-site/sites/all/themes/basetheme/template.php).
    – user32010
    Mar 23, 2018 at 16:28
  • Note: You've forgotten quotes around $variables['links']
    – user32010
    Mar 23, 2018 at 16:53
0

This would more likely work:

   $current_href = \Drupal::languageManager()->getCurrentLanguage()->getId();

    foreach (array_keys($variables["links"]) as $key) {
    // If we find a missing translation, remove its entry from the list.
    if ($key == $current_href) {
      unset($variables["links"][$key]);
    }
  }

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.