IMPORTANT NOTE: If you update qTranslate, these changes will be overwritten and you will need to re-insert this code again in order to restore the functionality.

qTranslate is a wonderful plugin that can help you create a multilingual website. If you only need to support two languages, how do you add a Toggle Link to your page?

Assuming you've already installed the qTranslate plugin on your site, you will need to make the following modifications:

1. Edit the following file: /wp-content/plugins/qtranslate/qtranslate_widget.php

2. Scroll down to the qtrans_generateLanguageSelectCode function and add the following code right after the break of the case 'both' section of the switch statement:

case 'toggleimage':
case 'toggletext':
   echo '<ul class="qtrans_language_chooser" id="'.$id.'">';
   foreach(qtrans_getSortedLanguages() as $language) {
      $classes = array('lang-'.$language);
      if ($language != qtrans_getLanguage()) {
         echo '<li class="'. implode(' ', $classes) .'"><a href="'.qtrans_convertURL($url, $language).'"';
         // set hreflang
         echo ' hreflang="'.$language.'" title="'.$q_config['language_name'][$language].'"';
         if($style=='toggleimage')
            echo ' class="qtrans_flag qtrans_flag_'.$language.'"';
         echo '><span';
         if($style=='toggleimage')
            echo ' style="display:none"';
         echo '>'.$q_config['language_name'][$language].'</span></a></li>';
      }
   }
   echo "</ul><div class=\"qtrans_widget_end\"></div>";
   break;
case 'toggleboth':
   echo '<ul class="qtrans_language_chooser" id="'.$id.'">';
   foreach(qtrans_getSortedLanguages() as $language) {
      $classes = array('lang-'.$language);
      if($language != qtrans_getLanguage()) {
         echo '<li class="'. implode(' ', $classes) .'"><a href="'.qtrans_convertURL($url, $language).'"';
         echo ' hreflang="'.$language.'" title="'.$q_config['language_name'][$language].'"';
         echo ' class="qtrans_flag_'.$language.' qtrans_flag_and_text"';
         echo '><span>'.$q_config['language_name'][$language].'</span></a></li>';
      }
   }
   echo "</ul><div class=\"qtrans_widget_end\"></div>";
   break;

3. Save your changes. Don't forget to upload the updated file if you edited a local copy of the file.

4. Finally, add the following PHP code to where you want to insert the toggle link:

  • Text Only:  <?php qtrans_generateLanguageSelectCode('toggletext'); ?>
  • Text and Flag Image:  <?php qtrans_generateLanguageSelectCode('toggleboth'); ?>
  • Flag Image Only: <?php qtrans_generateLanguageSelectCode('toggleimage'); ?>

Bonus Tip: If your theme doesn't include a PHP enabled widget, give the PHP Code Widget a try.