4

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 ?

Thomas W
  • 14,757
  • 6
  • 48
  • 67
enguerranws
  • 8,087
  • 8
  • 49
  • 97

7 Answers7

8

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

Ashish Patel
  • 3,551
  • 1
  • 15
  • 31
  • I should have mentionned that I'm looking for a server-side solution. Injecting content on client-side is kinda hacky IMHO. – enguerranws Apr 16 '15 at 10:52
  • In qtranslate_widget.php in plugin directory replace line no 159 from echo '>'.$q_config['language_name'][$language].''; to echo '>'.$language.''; – Ashish Patel Apr 16 '15 at 11:41
  • Thank you, but I should also have mentionned that I don't want to modify the core plugin (for update purpose). So I'm looking to achieve this, with a filter / hook or something. – enguerranws Apr 17 '15 at 13:26
  • Well, I feel stupid to not think about the last point. Thank you for noticing that. I posted my code down there, but I think I will use the language name in the parameters of qTranslate X. – enguerranws Apr 24 '15 at 18:41
3

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.

lassemt
  • 164
  • 1
  • 11
2

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

darkem
  • 21
  • 3
1

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); 
}
enguerranws
  • 8,087
  • 8
  • 49
  • 97
0

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.

Yehuda Tiram
  • 23
  • 1
  • 4
0

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 } ?>
0

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.

Alex
  • 1