markobarna
markobarna

Reputation: 309

qTranslate plugin switching language in the same page

I have a serious problem with a qTranslate buttons.
Right now the web structure is: http://www.site.com/news/?lang=en

When I stay in the home page and I try to change the language, the botton opens the first post (maybe because I'm using permalink):

<?php if(qtrans_getLanguage()=='it'): ?>
    <li><a href="<?php echo qtrans_convertURL(get_permalink(), 'en'); ?>" >eng</a></li>
    <li class="liguaattiva">ita</li>
<?php endif; ?>
<?php if(qtrans_getLanguage()=='en'): ?>
    <li class="liguaattiva">eng</li>
    <li><a href="<?php echo qtrans_convertURL(get_permalink(), 'it'); ?>" >ita</a></li>
<?php endif; ?>

How to solve this without open the last post or come back at the home page, but only switching language in the same page?

Upvotes: 2

Views: 11971

Answers (4)

Eng.Haytham Badran
Eng.Haytham Badran

Reputation: 21

May be this late but following good functions to easy call check current language or auto generate any language URL for qTranslate:

// check language
function check_lang() {
    return qtranxf_getLanguage();
}

// Generate language convert URL
function get_lan_url($lang){
    echo qtranxf_convertURL('', $lang);
}

// generate inline translate short code
add_shortcode( 'translate_now', 'get_translate' );
function get_translate( $atts, $content = null ) {
    extract( shortcode_atts(
            array(
                'ar' => '',
                'en' => '',
                'es' => '',
                'fr' => '',
            ), $atts )
    );
    if ( check_lang() == 'ar' ) {
        echo $atts['ar'];
    }
    if ( check_lang() == 'en' ) {
        echo $atts['en'];
    }
    if ( check_lang() == 'es' ) {
        echo $atts['ar'];
    }
    if ( check_lang() == 'fr' ) {
        echo $atts['ar'];
    }
}

function translate_now($ar,$en,$es,$fr){
    $content = '[translate_now ar="'.$ar.'" en="'.$en.'" es="'.$es.'" fr="'.$fr.'"]';
    echo do_shortcode($content);
}

So now you can check current language using check_lang() function for example:

<?php if(check_lang() == 'ar'): echo 'مرحبا'; endif;?>
<?php if(check_lang() == 'en'): echo 'Hello'; endif;?>
<?php if(check_lang() == 'es'): echo 'Hola'; endif;?>
<?php if(check_lang() == 'fr'): echo 'Bonjour'; endif;?>

Also you can use function translate_now()to translate inline by passing values:

<?php
translate_now(
    'مرحبا', // ar
    'Hello', //en
    'Hola', //es
    'Bonjour' //fr
);
?>

Also to generate any langauge convert URL use function get_lan_url() passing requested language:

<a href="<?php get_lan_url('ar');?>">العربية</a>
<a href="<?php get_lan_url('en');?>">English</a>
<a href="<?php get_lan_url('es');?>">España</a>
<a href="<?php get_lan_url('fr');?>">Français</a>

Upvotes: 0

arrow
arrow

Reputation: 174

The links are stored in the $qTranslate_slug object. I made a function to make it easy to get the link for the current page in the desired language:

function getUrlInTargetLanguage($targetLang){
    global $qtranslate_slug;
    return $qtranslate_slug->get_current_url($targetLang);
}

So for example, if you wanted to get the english link, you should write:

getUrlInTargetLanguage("en");

Upvotes: 0

Magico
Magico

Reputation: 2804

You don't need to use get_permalink()

you can just pass an empty string as url and the language as 2nd param and the function will do rest ! just like:

$my_translated_content_url = qtrans_convertURL("", "en");

infact if you see at the function definition:

function qtrans_convertURL($url='', $lang='', $forceadmin = false) {
  global $q_config;

  // invalid language
  if($url=='') $url = esc_url($q_config['url_info']['url']); // <-You don't need the url
  if($lang=='') $lang = $q_config['language'];
      [... the function continue...]

Upvotes: 1

user850010
user850010

Reputation: 6359

I'm using qTranslate on my project and I do not do any of that stuff you do in your code above and have no problem switching between languages.

All I do is call qts_language_menu() function that creates language menu, nothing else. This will create necessary links which ables you to switch between languages but stay on the same page.

Upvotes: 2

Related Questions