Hello

I'm asking for a little help. I'd like to display the language switcher list as a drop-down list, so when the user selects a language he's automatically redirect, without a button to click. I got inspiration from this issue from i18n.

Here is the code I've put in template.php :

/**
 * Override of theme_languageswitcher()
 * Provided by languageinterface.module
 *
 * @param $links
 *   An array of all enabled languages links
 */
function zen_ninesixty_languageswitcher($links) {
  $output = '<form name="language-form">';
  $output .= '<select id="language-interface" onchange="window.location=document.language-form.language-interface.options[document.language-form.language-interface.selectedIndex].value;" value="GO">';
  foreach($links as $link) {
    $output .= '<option value="'. $link['path'] .'">';
    $output .= $link['text'];
    $output .= '</option>';
  }
  $output .= "</select>";
  $output .= "</form>";
  
  return $output;
}

The HTML output is correct, values and paths are properly populated. But nothing happens when I select a language... I tried 'onselect' instead of 'onchange', 'window.location.href' too, but nothing makes it...

Any hint really appreciated !

Comments

DjebbZ’s picture

Here's my hint !

function zen_ninesixty_languageswitcher($links) {
  //called to compare current language and selected option
  global $language;
  
  $output = '<div class="language-form">'. t('Select a language') .'</div>';
  $output .= '<form name="language-form">';
  $output .= '<select id="language-interface" onchange="window.location.href=this.options[this.selectedIndex].value;">';
  foreach($links as $link) {
    $output .= '<option value="'. $link['path'] . '"'.  ($link['language'] == $language->language ? ' selected="selected"' : '') .'>';
    $output .= $link['text'];
    $output .= '</option>';
  }
  $output .= "</select>";
  $output .= "</form>";
  
  return $output;
}

The good thing is that it will automatically set the language according the current language. And it works, just tested a few minutes ago :)

LarsKramer’s picture

There is now another module which displays the language switcher as a drop-down: http://drupal.org/project/lang_dropdown
Is there any change these two modules could be joined? I like the other features of Consistent Language Interface module.

SKAUGHT’s picture

Issue summary: View changes

d7: you can envoke that form's functionality (more of less) by:

$path = drupal_is_front_page() ? '<front>' : $_GET['q'];
$languages = language_negotiation_get_switch_links('language', $path);
$lang_dropdown_form = drupal_get_form('lang_dropdown_form', $languages, 'language');
$output  = render($lang_dropdown_form);

d8: shameless promotion..
https://www.drupal.org/project/dropdown_language

apaderno’s picture

Status: Active » Closed (outdated)
Issue tags: -theme, -template.php

I am closing this issue, since it is for a Drupal version that now is not supported.
Please re-open it if the issue is also relevant for other project branches that require a supported Drupal version.