metinsaylan // rogue developer

HOWTO: Display content based on current language using qtranslate


QTranslate **is a flexible and simple **blog translation plugin. Using QTranslate you can easily convert your blog to a multilingual site. However to use it fully functional, you need a few modifications in your theme. Here I will show you how to fix your links and content based on current language on Qtranslate. *** [toc type=”regular”]

Adding language selector using php

After you set up a multilingual site, you will have to put a language selector to your site. You can either use selector widget, or you can manually add this code snippet to your header to display it:

<?php if(function_exists('qtrans_generateLanguageSelectCode')){ ?>
<div id="language-selector" class="xsmall lightblue">
	<?php echo qtrans_generateLanguageSelectCode('image'); // You can use image, text or both as an argument here ?>
<?php } ?>

If you add selector manually, you need to style it yourself. Here is an example css code:

[css].qtrans_language_chooser { list-style-type:none } .qtrans_language_chooser li { float:left; margin-left:4px } .qtrans_flag { border:1px solid #000} [/css]

Fix embedded links in your theme templates

Qtranslate handles most translation job using wordpress filters and hacks. One thing it can’t translate is a handwritten in-site link in your theme header or footer. For example if you embed a homepage link in your template using bloginfo:

<a href="<?php bloginfo('url'); ?>">My Awesome Blog Homepage</a>

This link won’t have language string at the end. And if you change language and then click this link, your site language will revert back to original language. To prevent this issue, you can wrap your links using convertURL core function like this:

<a href="<?php qtrans_convertURL( bloginfo('url') ); ?>">My Awesome Blog Homepage</a>

Your link will now have current language added at the end (E.g. : ). This will prevent language changes when a user clicks a hand-written link.

Displaying content based on current language

If you want to display different menus, images or text based on current language you can test current language like this:

if( qtrans_getLanguage() == 'en' ){ ?>
    <!-- English content here -->
<?php }else { ?>
    <!-- Other language content here -->
<?php } ?>

Display images based on current language

For images i suggest using this style:

<img src="<?php echo qtrans_getLanguage(); ?>.png" />

For this to work you need to add language to image filename like this logo-en.png

Display a different menu for each language

To display a different menu depending on current language, first you need to define menus for each language.

if ( function_exists( 'register_nav_menus' ) ) {
		  'menu-header-en' => 'Main Menu EN',
		  'menu-header-tr' => 'Main Menu TR',
		  'menu-footer-en' => 'Footer menu EN',
		  'menu-footer-tr' => 'Footer menu TR',
} ?>

Then you need to update code where you insert that location:

<?php wp_nav_menu( array(  'theme_location' => 'menu-header-' . qtrans_getLanguage() , 'sort_column' => 'menu_order', 'container_class' => 'menu-header' ) ); ?>
<?php wp_nav_menu( array(  'theme_location' => 'menu-footer-' . qtrans_getLanguage() , 'sort_column' => 'menu_order', 'container_class' => 'menu-footer' ) ); ?>

Now you can go to Appearance->Menus and add different menus for other languages.

[hr] I hope you liked my guide on Qtranslate. If you don’t have that plugin you can download qtranslate here.

Search HOWTO: Display content based on current language using qtranslate on Google