enguerranws
enguerranws

Reputation: 8233

Wordpress : qTranslate X language switcher with language code

I'm trying to make a basic language switcher with qTranslate X, something like :

FR | EN

There's a function to achieve that : qtranxf_generateLanguageSelectCode('text'); but it can only accept 'text', 'image' or 'both', so it doesn't fit to my needs : 'text' is the full name of the language.

How can I just show the language code ? Any idea to make a filter to do that ?

Upvotes: 4

Views: 30948

Answers (7)

Alex
Alex

Reputation: 1

https://qtranslatexteam.wordpress.com/faq/

For example, to show flag only in the top language menu item, enter #qtransLangSw?title=none, if in addition to this current language is not needed to be shown, enter #qtransLangSw?title=none&current=hidden, and so on.

Upvotes: 0

lassemt
lassemt

Reputation: 156

Inspecting the plugin I found that generateLanguageSelectCode have more types than documented. So to use language codes you can simply just use the type 'short', like this:

qtranxf_generateLanguageSelectCode('short');

This might be a feature added since last answer.

Here is a overview of all the switcher types: 'text', 'image', 'both', 'short', 'css_only', 'custom', and 'dropdown'. I havn't looked into how the different types works, but you'll find them in qtranslate_widget.php in the plugin folder.

Upvotes: 3

darkem
darkem

Reputation: 21

You could use widget for that

<?php the_widget('qTranslateXWidget', array('type' => 'custom', 'format' => '%c') );?>

(%c - Language 2-Letter Code)

It should be noted that if you would like to use dropdown type and 2-Letter Code - this won't work because format argument works only with 'custom' type. In this case I would go with Yehuda Tiram answer (especially if you have many languages and you don't know which languages your client will want to use).

More documentation here

Upvotes: 2

Muhammad Awais Iqbal
Muhammad Awais Iqbal

Reputation: 171

I've done it using the following query and it is working fine for me.

<?php if (qtranxf_getLanguage() == 'ar') { ?>
<script>
    jQuery(document).ready(function () {
        var current_URL = jQuery(location).attr('href');
        url = current_URL.replace('/ar/', '/en/')
        jQuery('.languages-selection ul li a').attr('href', url)
    });
</script>
<?php } elseif (qtranxf_getLanguage() == 'en') { ?>
<script>
    jQuery(document).ready(function() {
        var current_URL = jQuery(location).attr('href');
        url = current_URL.replace('/en/', '/ar/')
        jQuery('.languages-selection ul li a').attr('href', url)
    });
</script>
<?php } ?>

Upvotes: 0

Yehuda Tiram
Yehuda Tiram

Reputation: 23

Why don't you just change the language name as per your needs? It's possible in the language edit and does not affect anything.

Upvotes: 0

enguerranws
enguerranws

Reputation: 8233

A friend helped me with that and it's based on Ash Patel answer but in a cleaner way (IMHO) :

function my_qtranxf_generateLanguageSelectCode($style='', $id='') {
    ob_start();  
    qtranxf_generateLanguageSelectCode($style, $id);
    $o = ob_get_contents();
    ob_end_clean();
    return str_replace(array('English', 'Français'),array('EN', 'FR'), $o); 
}

Upvotes: 1

Ashish Patel
Ashish Patel

Reputation: 3614

Try to add following script below translate code.

echo qtranxf_generateLanguageSelectCode('text');
<script>jQuery(document).ready(function(){ jQuery('.lang-en a span').html('EN'); jQuery('.lang-fr a span').html('FR'); })</script>

Serverside Solution:

Please find below Code which modify language name to language code without change in plugin code and you can do it by word press filter.

Paste below code into function.php file.

add_filter('template_include','start_buffer_EN',1);
function start_buffer_EN($template) {
  ob_start('end_buffer_EN');  
  return $template;
}
function end_buffer_EN($buffer) {
  return str_replace('<span>English</span>','<span>EN</span>',$buffer);  
}

add_filter('template_include','start_buffer_FR',1);
function start_buffer_FR($template) {
  ob_start('end_buffer_FR');
  return $template;
}
function end_buffer_FR($buffer) {  
  return str_replace('<span>Français</span>','<span>FR</span>',$buffer);
}

You can change language name from wp-admin by edit language name directly..

Q-translate-x-change-image-name-from-admin

Upvotes: 8

Related Questions