Wednesday, 10 November 2010

Creating customized language switcher block on Drupal website in PHP.


Tonight I want to share some ideas how to customize or even create you own language switcher block on your Drupal based website. Live example I am using is
Some things you need for that;
1. Enable locale module - you can find under core/optional modules
2. Internationalization or i18n custom module from

At first install I18n module and enable all multilingual support what is required - in my case these are all options, because I needed all site content to be translated.
After words point to your site`s admin/settings/languages and install all languages you need for your project.
Next step is to create multilingual content, well this basically is easy(don`t forget to enable multilingual support for all you content types - it`s under workflow if you edit you content types settings) - you need to manually translate all nodes form sites default (which usually appears English) to your chosen language.
Next under each language you need to adjust language settings, by choosing how multilingual support will be displayed, either paths suffix or sub domain. In my example I will use sub domain option but if you look at you will find how to adjust path suffix settings.
Ok once we have done all this here is the code, you can choose - either create new module with hook_block or just paste this code in to your themes block.tpl -> block-locale.tpl.php.
    global $base_url; 
    $q = $_GET['q']; 
    $translations = translation_path_get_translations($q); 
    $languages = language_list(); 
    $output = '<ul>'; 
    foreach ($languages as $language) {
    $output .= '<li class="' . $language->language . '">'; 
    if ($language->enabled == 0) { 
    $output .= '<span class="language-link-disabled" title="Coming Soon">'; // disabled langugase are not link in my case 
    $output .= '<img src="' . base_path() . path_to_theme() . '/languages/' . $language->language . '.jpg" alt="' . $language->language . '.jpg"/>'; 
    $output .= '</span>'; // Lookt at the examples reference to see images
    else { 
    $path = $translations[$language->language]; 
    $lang = $language->language; 
    $output .= '<a href="' . $language->domain . '/' . drupal_get_path_alias($path, $lang) . '" class="language-link-enabled">'; 
    $output .= '<img src="' . base_path() . path_to_theme() . '/languages/' . $language->language . '.jpg" alt="' . $language->language . '.jpg"/>'; 
    $output .= '</a></li>'; 
    $output .= '</ul>'; 
As you can see no searching in contributed modules repository, I little bit of sweat and it works.
Happy coding, and as I mentioned you can see this working live on
If anything is not working drop a comment line.


  1. I just pasted the code at the end of block.tpl.php
    and I got 4 lines like the below for each language I enabled.
    is there something I'm doing wrong...

    Notice: Undefined index: en in include() (line 28 of /home2/solidbas/public_html/individual/sites/all/themes/bluemasters/block.tpl.php).

    NB: I do guess work in PHP I am not a native of PHP :)