Adding a filter to qTranslate to change display of language chooser

This is a problem I am having with qTranslate, but I suppose the solution is more of an instruction in how WordPress hooks and filters work.

Instead of using the qTranslate widget, I am using the hook

Read More
<?php echo qtrans_generateLanguageSelectCode('text');?>

to call the language chooser in my header and then modifying its appearance in my CSS. This is no problem. However, I would like to add a “style” to the Language Select Code function so that instead of displaying the full language text, which would be “Francais” and “English” in my case, it will show just the language code instead (EN and FR). I had figured this out by changing (and I know this a very bad thing to do) qtranslate_widget.php and adding a new case to function qtrans_generateLanguageSelectCode($style='', $id='').

This worked great, but then I updated WordPress and had to update qTranslate as well, so obviously my modification went away. So instead of re-doing that operation, I was wondering if anyone has any suggestions for how to add a filter to the function in my own theme’s functions.php? I figure it would be something like

add_filter('qtrans_generateLanguageSelectCode', 'qtrans_myLanguageSelectcode',

but after that I get lost and don’t know enough about filters. I hope this is clear enough!

When I updated, I obviously lost my modification but I think that below is what should work (I haven’t tested it yet!)

case 'code':
foreach(qtrans_getSortedLanguages() as $language) {
            $classes = array('lang-'.$language);
            if($language == $q_config['language'])
               $classes[] = 'active';
            echo '<a href="'.qtrans_convertURL($url, $language).'"  class="'. implode(' ', $classes) .'"';
            // set hreflang
            echo 'hreflang="'.$language.'" title="'.$q_config['language_name'][$language].'"';
            echo '>''.$language.'</a>';
         }
break;

Not to make this post too long, but the following is the existing function in the qTranslate plugin:

    // Language Select Code for non-Widget users
function qtrans_generateLanguageSelectCode($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[rn";
                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;
    }
}

Related posts

Leave a Reply

1 comment

  1. 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[rn";
                        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!