8

On translated pages the language switcher shows links to translated languages, but for untranslated languages there is no link, but the name of the language without a link. In the case, that there is no corresponding node in a given language, i would like the language switcher to show a link to the front page instead.

How can i accomplish this behaviour? Thank you very much!

  • You can do it just using page, or node templates and CSS rules. – topcode4u Feb 18 '13 at 19:30
13
+50

Add next into template.php file of your theme:

<?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'])) {
        // Set here any page link.
        $variables['links'][$key]['href'] = '<front>';
      }
    }
    return theme_links($variables);
  }
  • Thank you for your answer. Are these <front> links localized then? – jumper Feb 17 '13 at 17:39
  • 1
    This front/home page will point to current selected language front page. – Nikit Feb 17 '13 at 21:34
  • Thank you again, i think your solution is a lot better than the one i ended up using :) – jumper Feb 18 '13 at 21:27
4

there is a module for this now - Language Switcher Fallback. Does exactly what you asked, directing the user to the frontpage if no translation to a node. See here https://drupal.org/project/language_switcher_fallback

1

In the end I used this approach.

function YOURTHEME_language_switch_links_alter(array &$links, $type, $path) {
  $language_type = variable_get('translation_language_type', LANGUAGE_TYPE_INTERFACE);

  if ($type == $language_type && preg_match("!^node/(\d+)(/.+|)!", $path, $matches)) {
    $node = node_load((int) $matches[1]);

    if (empty($node->tnid)) {
      // If the node cannot be found nothing needs to be done. If it does not
      // have translations it might be a language neutral node, in which case we
      // must leave the language switch links unaltered. This is true also for
      // nodes not having translation support enabled.
      if (empty($node) || entity_language('node', $node) == LANGUAGE_NONE || !translation_supported_type($node->type)) {
        return;
      }
      $langcode = entity_language('node', $node);
      $translations = array($langcode => $node);
    }
    else {
      $translations = translation_node_get_translations($node->tnid);
    }

    foreach ($links as $langcode => $link) {
      if (isset($translations[$langcode]) && $translations[$langcode]->status) {
        // Translation in a different node.
        $links[$langcode]['href'] = 'node/' . $translations[$langcode]->nid . $matches[2];
      }
      else {
        // No translation in this language, or no permission to view.
        $links[$langcode]['href'] = '<front>';
      }
    }
  }
}

Which replaces this code fragment in the original Translation module.

  else {
    // No translation in this language, or no permission to view.
    unset($links[$langcode]['href']);
    $links[$langcode]['attributes']['class'][] = 'locale-untranslated';

I am pretty sure the other solution is more beautiful, but this solution is also working.

  • 1
    First solution did not work for me... This one worked as charm! Thx. – Carles Estevadeordal Dec 21 '13 at 1:59

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.