WordPress.org

Ready to get started?Download WordPress

Forums

mqTranslate
store language choice in a cookie (7 posts)

  1. Bertr4nd
    Member
    Posted 7 months ago #

    Hello,

    I just switched to mqtranslate from qtranslate, and I realized my little hack to keep user's language choice in a cookie (because I use GET ?lang=fr method to switch language then stick to the chosen language) still works with mqtranslate.

    Here is the mod I do, could it be included within the plugin as an option ?
    in mqtranslate_core.php

    replace

    if(isset($_GET['lang']) && qtrans_isEnabled($_GET['lang'])) {
    		// language override given
    		$result['language'] = $_GET['lang'];
    		$result['url'] = preg_replace("#(&|\?)lang=".$result['language']."&?#i","$1",$result['url']);
    		$result['url'] = preg_replace("#[\?\&]+$#i","",$result['url']);
    	} elseif($home['host'] == $result['host'] && $home['path'] == $result['url']) {
    		if(empty($referer['host'])||!$q_config['hide_default_language']) {
    			$result['redirect'] = true;
    		} else {
    			// check if activating language detection is possible
    			if(preg_match("#^([a-z]{2}).#i",$referer['host'],$match)) {
    				if(qtrans_isEnabled($match[1])) {
    					// found language information
    					$referer['host'] = substr($referer['host'], 3);
    				}
    			}
    			if(!$result['internal_referer']) {
    				// user coming from external link
    				$result['redirect'] = true;
    			}
    		}
    	}

    with

    /* mod cookie lang */
    	$BSGqtc="";
    	if (isset($_COOKIE['BSGqTranslateForceLang'])) {
    		$BSGqtc=$_COOKIE['BSGqTranslateForceLang'];
    	}
    
    	if( (isset($_GET['lang']) && qtrans_isEnabled($_GET['lang'])) || ($BSGqtc!="" && qtrans_isEnabled($BSGqtc)) ) {
    		// language override given
    		if (isset($_GET['lang'])) {
    		setcookie ( 'BSGqTranslateForceLang', $_GET['lang'] , time()+60*60*24*365, "/" );
    		$BSGqtc = $_GET['lang'];
    		}
    		$result['language'] = $BSGqtc;
    		$result['url'] = preg_replace("#(&|\?)lang=".$result['language']."&?#i","$1",$result['url']);
    		$result['url'] = preg_replace("#[\?\&]+$#i","",$result['url']);
    	} elseif($home['host'] == $result['host'] && $home['path'] == $result['url']) {
    		if(empty($referer['host'])||!$q_config['hide_default_language']) {
    		$result['redirect'] = true;
    		} else {
    			// check if activating language detection is possible
    			if(preg_match("#^([a-z]{2}).#i",$referer['host'],$match)) {
    				if(qtrans_isEnabled($match[1])) {
    				// found language information
    				$referer['host'] = substr($referer['host'], 3);
    				}
    			}
    			if(!$result['internal_referer']) {
    			// user coming from external link
    			$result['redirect'] = true;
    			}
    		}
    	}
    	/* end mod cookie lang */

    https://wordpress.org/plugins/mqtranslate/

  2. Bertr4nd
    Member
    Posted 7 months ago #

    seemed to work with a website with woocommerce and qtranslate

  3. JoshKay
    Member
    Posted 7 months ago #

    +1 for this. Is there any way of taking it out of a mqTranslate plugin mod and into a separate plugin? I seem to remember the problem I ran into was there are no hooks at all in qTranslate. Maybe this could change in mqTranslate?

  4. Bertr4nd
    Member
    Posted 6 months ago #

    looks like the latest update broke (among other things) my hack...
    I don't see what this would happen, on purpose ?
    I see no new functionalities replacing it, plus menus are now broken and I can no longer use my own language selectors in menu using query string !

  5. Bertr4nd
    Member
    Posted 6 months ago #

    sorry about my previous post that may accidentally sound a bit paranoid, it wasn't on purpose :) !

    When using a cookie to store language selection, you no longer need url conversion, hence the qtrans_convertURL function is useless - as it is one of the core mqtranslate function this is also the one that may lead to certain troubles when used with complex plugins such as woocommerce.

    To have everything working fine with the latest (2.6.6.3) version, the above hack works fine, but you will also have replace the whole wonderful qtrans_convertURL function by

    function qtrans_convertURL($url='', $lang='', $forceadmin = false, $forceaddlang = false) {
    	return $url;
    }

    Of course this use of mqtranslate is not really SEO friendly (but neither is the use of the query string language selection) but the user experience (in the case of the use of woocommerce both for customer and store admin) is much smoother.

  6. Bertr4nd
    Member
    Posted 6 months ago #

    just FYI above modifications (almost emptying qtranslate_convertURL and a little cookie modifications given above) are still working with latest (2.6.7).
    It would be extremely nice to have this available as an option (e.g. using GET, a cookie, and bypassing convertURL) so hacking with each version wouldn't be necessary, I completely understand this is not the best-for-everyone solution, but for a site which needs to use cookies anyway for several other reasons, it's really smooth and simple.

  7. kayamm
    Member
    Posted 4 months ago #

    Hey Bertr4nd I have to thank you for this cookie solution for external links redirecting to chosen language.
    I had to make a page with 5 languages and that works fine but with an option of QR code linking to content and pointing to right lang. I was strugling for the last 3 days with js and php making cookies and redirecting urls and it didn't work. This saved me big time :) big respect!

Reply

You must log in to post.

About this Plugin

About this Topic