How to make language switcher links link to frontpage in Drupal 7

Drupal has a block called "Language Switcher", which displays links to different language versions of the current page/node. If the node does not have translated version on the specified language, the block will not display a link for it. This can cause some confusion, since the user always expects to see links to all of the site's languages.

This can be fixed by modifying the block so that the all of the links link to the corresponding language's frontpage. It is easy to do by editing the site's theme.

Edit your theme's template.php-file (or create one if it does not exist) and add the following lines:

/**
 * Implements hook_language_switch_links_alter().
 */
function MYTHEME_language_switch_links_alter(&$links, $type, $path) {
  global $language;
  if ($type == LANGUAGE_TYPE_INTERFACE && isset($links[$language->language])) {
    foreach ($links as $langcode => &$link) {
      $link['href'] = '<front>';
    }
  }
}

Change "MYTHEME" to your theme's name.

Comments

In my default installation it displays the corresponding language name struck out. I find that less confusing than being redirected to the front page if a translation doesn't exist.

As a visitor, I hate sites that send me to the front page, so I can start navigating or searching again for the content that I just found. You should really reconsider this. Boromino's idea seems much better regarding usability.

Just my 2 cents.

Maybe a more sensible approach is to create a single page for each language explaining there’s no available translation for the current content.

Roman's picture
Roman
Wed, 04/11/2018 - 18:35

Thank you so much! I have been looking for this for hours! :-D

Jack J. 's picture
Jack J.
Tue, 04/04/2017 - 17:29

Hello! Maybe it is a good idea to check out a translation management platform like https://poeditor.com to better manage translations.

linm's picture
linm
Tue, 05/01/2018 - 15:58

The "active" class is gone though…

Any solution?

Add new comment