Adding a Language Toggle to qTranslate Plugin for WordPress

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.


Comments

Adding a Language Toggle to qTranslate Plugin for WordPress — 5 Comments

  1. Thank you for sharing this useful scripts. It saved me a lot of works. Just a note: in the toogletext block of code something like ‘echo “”;’ seems to be missing before the break to close the tag, and it caused a layout issue on ie8. Hope it may help.

    • Hi Christian, thanks for the feedback. There were several issues with the toggleboth section that are now fixed in the above code. However, I could not find any issues with the toggletext section of the code. I’ve added line numbers to the source code. Could you tell me were about you think the code needs to be fixed? Best regards, Michael.

  2. Hi Michael, I am likely to have made a mistake white copying your code, because in my page the line 18 (with the list closing tag) is completely missing. That’s the one I was referring to before. Without it, the list stays open and the whole layout breaks down in IE8. That was the point. I apologize if it was due to me. Thank you again.

  3. Hi Christian, I checked one of my backups and you are right! I thought I had added it to the toggleboth block, but I actually added it to the toggletext block. Either way, it’s fixed. I did make changes to the toggleboth block too so please update your code.

    Glad it helped you out. Thanks again and sorry for the confusion.

Leave a Reply

Your email address will not be published. Required fields are marked *