The same idea, different code

Last modified: August 22, 2008 - 18:22

On my page I wanted to display only the language switcher for languages that are not currently being used: for example, if I am currently viewing English pages, I wanted there to be only French language switcher, instead of both (or more, if you use more languages).
I turned off the block "Translations for Content" that was displaying all available languages and created a new one to display only the languages that are not currently in use.

Here is the code for that new block:

<?
//get i18n module vars info
$query = drupal_query_string_encode($_GET, array('q'));
$lang = translation_get_links($_GET['q'], empty($query) ? NULL : $query);
$current_lang = i18n_get_lang(); // currently used language
$active_lang = i18n_languages('active'); // array with all active languages

// Find the name of the currently chosen language
$active_lang_name = $active_lang[$current_lang];
// Get rid of the array keys because we want the keys to be numeris as keys in $lang
$active_lang_values = array_values($active_lang);
// Find the key for the currently chosen language.
$find_key = array_search($active_lang_name,$active_lang_values);

foreach($lang as $key=>$value) {
    // As long as the find_key is different from the key, display the value (link to that language)
    if ($key <> $find_key) {
print $value;
    }
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.