In my header.php
, where I want qTranslate language chooser to live, I put this in:
<?php echo qtrans_SelectCode('code');?>
Then, I added this code to functions.php
. It’s a little redundant in that it repeats the built-in qTranslate options (image, text, dropdown) which I am not using on my page – but I wanted to retain the code since my filter is overwriting the qtrans_generateLanguageSelectCode
function. (If anyone can figure out how to just ADD a style in and not overwrite the whole function, that would be awesome!)
The style that I use is called ‘code’ (you’ll see it near the bottom) and just outputs the language code – ie: FR
and EN
in my case. You could write whatever output you want by creating a new style:
if( function_exists( 'qtrans_getLanguage' ) )
{
// qTranslate Language Select Code filter
add_filter( 'qtrans_generateLanguageSelectCode', 'qtrans_SelectCode' );
function qtrans_SelectCode( $style="", $id = '' ) {
global $q_config;
if( $style == '' )
$style="text";
if( is_bool( $style ) && $style )
$style="image";
if( is_404() )
$url = get_option( 'home' );
else
$url="";
if( $id == '' )
$id = 'qtranslate';
$id .= '-chooser';
switch( $style ) {
case 'image':
case 'text':
case 'dropdown':
echo '<ul class="qtrans_language_chooser" id="' . $id . '">';
foreach( qtrans_getSortedLanguages() as $language ) {
$classes = array( 'lang-' . $language );
if( $language == $q_config['language'] )
$classes[] = 'active';
echo '<li class="' . implode( ' ', $classes ) . '"><a href="' . qtrans_convertURL( $url, $language ) . '"';
// set hreflang
echo ' hreflang="' . $language . '" title="' . $q_config['language_name'][$language] . '"';
if( $style == 'image' )
echo ' class="qtrans_flag qtrans_flag_' . $language . '"';
echo '><span';
if( $style == 'image' )
echo ' style="display:none"';
echo '>' . $q_config['language_name'][$language] . '</span></a></li>';
}
echo "</ul><div class=\"qtrans_widget_end\"></div>";
if( $style == 'dropdown' ) {
echo "<script type=\"text/javascript\">\n// <![CDATA[\r\n";
echo "var lc = document.getElementById('" . $id . "');\n";
echo "var s = document.createElement('select');\n";
echo "s.id = 'qtrans_select_" . $id . "';\n";
echo "lc.parentNode.insertBefore(s,lc);";
// create dropdown fields for each language
foreach( qtrans_getSortedLanguages() as $language ) {
echo qtrans_insertDropDownElement( $language, qtrans_convertURL( $url, $language ), $id );
}
// hide html language chooser text
echo "s.onchange = function() { document.location.href = this.value;}\n";
echo "lc.style.display='none';\n";
echo "// ]]>\n</script>\n";
}
break;
case 'both':
echo '<ul class="qtrans_language_chooser" id="' . $id . '">';
foreach( qtrans_getSortedLanguages() as $language ) {
echo '<li';
if( $language == $q_config['language'] )
echo ' class="active"';
echo '><a href="' . qtrans_convertURL( $url, $language ) . '"';
echo ' class="qtrans_flag_' . $language . ' qtrans_flag_and_text" title="' . $q_config['language_name'][$language] . '"';
echo '><span>' . $q_config['language_name'][$language] . '</span></a></li>';
}
echo "</ul><div class=\"qtrans_widget_end\"></div>";
break;
case 'code':
$count = 0;
foreach( qtrans_getSortedLanguages() as $language ) {
if( $count > 0 )
print ' ';
$count++;
if( $language == $q_config['language'] ) {
print '<span class="qtrans_language_chooser active" title="' . $q_config['language_name'][$language] . '">';
print $language;
print '</span>';
}
else {
print '<a href="' . qtrans_convertURL( $url, $language ) . '" class="qtrans_language_chooser"';
print ' hreflang="' . $language . '" title="' . $q_config['language_name'][$language] . '">';
print $language;
print '</a>';
}
}
break;
}
}
}
It is important to enclose the functions in:
if(function_exists('qtrans_getLanguage'))
in case the qTranslate plugin breaks or you deactivate it, otherwise your functions.php
will not work properly and you won’t be able to get at your site!
Sorry I took a little while to get back, I am not a web developer by profession so I haven’t touched this stuff in a few months. I’m completely self-taught, which is why my terminology may be a little unclear. But I hope this helps!