UPDATE : Problems migrating from mqtranslate with qtranslate-slug to qtranslate-x

UPDATE: qtranslate-x v. 3.2.9 removed the possibility to use the plugin as stated. Look at the bottom for an updated version!

So, some days ago, my favorite multilanguage plugin for wordpress, mqtranslate, was declared deprecated in favor of qtranslate-x. As i was just now coding a website for a client that uses mqtranslate and qtranslate slug, i thought „Better switch now than when the website is online and it has to be updated.“

So i installed qtranslate-x, migrated the settings from mqtranslate into qtranslate-x, and it seemed to work.

Until i noted that something strange happened: i could switch from my default language (german) to my secondary language (english), but not back to the default language. This only happened, when i activated the option „Hide URL information for default language“.

The reason for this is neither the fault of qtranslate-x nor from qtranslate-slug, but something in between them, maybe a miscommunication:

Qtranslate-x works different than qtranslate or mqtranslate in terms of detecting the language the user shall be shown.

mqtranslate (and qtranslate) got the information about the language to be shown from the URL: if there is a folder „/de/“ in the path, show german, if there is a folder „/en/“, show english. If the above mentioned option is active and no language folder is in the URL, show the default language.

So far so good.

qtranslate-x takes a different approach: the language is detected primarily by a cookie that is set when you visit a specified language. If you visit a page with the language path „/en/“ in the url, this cookie is set to „English“. As long as you don’t visit a page that sets this cookie back to „German“ by having „/de/“ in its URL, you don’t get german language. If you have the above mentioned Option active to hide the default language path, you still have to visit, for instance, http://www.mysamplesite.de/de/pagename, to switch back to german. The system itself redirects and rewrites the URL THEN to http://www.mysamplesite.de/pagename (removes the „/de/“).

Using qtranslate-slug, this leads to a problem: If the option to hide the URL language information for default language is active, the language information is not put in the link for the qtranslate-slug language switcher.

Because i couldn’t figure out, where to fix this within qtranslate-slug, i wrote my own little fix. This Plugin replaces the „language-detection-function“ of qtranslate-x with a function, that works nearly the same, except that IF a) the cookie is set and b) the option to hide the URL info for default language is active and c) there is no language info in the URL, it redirects you to the default language Variant of the page you requested. So, all in all, the language switching of mqtranslate is recreated, more or less.

This Plugin should be used ONLY if you
a) already used qtranslate or mqtranslate AND qtranslate-slug
b) use the option „Hide URL language information for default language.“
c) migrated to qtranslate-x already and found yourself in the same problem.

THIS IS ONLY A TEMPORARY FIX! The language detection that qtranslate-x uses, is a good one and improves many things. But if we want to keep our active websites unbroken, we sometimes need to stick to the „old“ solutions😉

HOW TO APPLY:
-Create a file named mqtranslate-migration-helper.php
-Insert the following code in it:

<?php
/**
Plugin Name: mqtranslate/qtranslate-slug 2 qtranslate-x migration helper
Description: Recreates the language switching like mqtranslate in qtranslate-x
Version: 1.0.0
Author: kuchenundkakao
Author URI: https://kuchenundkakao.wordpress.com
License: GPL2
*
*/

if(!function_exists(‚qtranxf_detect_language_front‘)){
function qtranxf_detect_language_front(&$url_info) {
global $q_config;
//assert($url_info[‚doing_front_end‘]);
while(true){
if( isset($_COOKIE[QTX_COOKIE_NAME_FRONT]) ){
if($q_config[‚hide_default_language‘]){
$lang = $q_config[‚default_language‘];
} else {
$cs=null;
$lang=qtranxf_resolveLangCase($_COOKIE[QTX_COOKIE_NAME_FRONT],$cs);
}
$url_info[‚lang_cookie_front‘] = $lang;
if($lang) break;
}

if($q_config[‚detect_browser_language‘]
&& ( !isset($_SERVER[‚HTTP_REFERER‘]) || strpos($_SERVER[‚HTTP_REFERER‘],$url_info[‚host‘])===FALSE ) ){
$urlunslashed=untrailingslashit($url_info[‚wp-path‘]);
if(empty($urlunslashed)){
$lang=qtranxf_http_negotiate_language();
$url_info[‚lang_browser‘] = $lang;
if($lang) break;
}
}

$lang = $q_config[‚default_language‘];
break;
}
if( !isset($url_info[‚doredirect‘])
//&& !defined(‚WP_ADMIN‘) && !defined(‚DOING_CRON‘) && !defined(‚DOING_AJAX‘)//will check later
&& (!$q_config[‚hide_default_language‘] || $lang != $q_config[‚default_language‘])
//&& !$url_info[‚language_neutral_path‘]//already so
){
$url_info[‚doredirect‘]=’language needs to be shown in url‘;
}
return $lang;
}
}

/* Add a function to remove false duplicate alternate hreflang tags if qtranslate_slug is installed */
add_action(‚get_header‘,’remove_qts_slug_canonical‘);
function remove_qts_slug_canonical(){
global $qtranslate_slug;
remove_action(‚wp_head‘,array($qtranslate_slug, ‚qtranslate_slug_header_extended‘));
}

-Save the file
-Upload the file directly into your wp-content/plugins/ folder
-Activate the plugin in your Plugins settings
Et voila!

If you have problems, please leave a comment.

Happy Coding,
Kuchenundkakao

UPDATE:

qtranslate-x just removed the possibility to overwrite the function with v 3.2.9. Instead, we get a filter. yay.😉
Here is an updated Version of the Plugin
THIS IS A QUICK FIX, IF SOMETHING BREAKS, I’M NOT LIABLE!!!

 

<?php
/**
Plugin Name: mqtranslate/qtranslate-slug 2 qtranslate-x migration helper
Description: Recreates the language switching like mqtranslate in qtranslate-x
Version: 1.1.0
Author: kuchenundkakao
Author URI: https://kuchenundkakao.wordpress.com
License: GPL2
*
*/

add_filter('qtranslate_detect_language', 'make_qtranslate_slug_compatible_again');
function make_qtranslate_slug_compatible_again($url_info){
if(!defined('DOING_AJAX')){
if( isset($url_info['doing_front_end']) ){
if($url_info['doing_front_end'] === TRUE){
global $q_config;
if(($q_config['hide_default_language']) && (!isset($url_info['lang_url']))){
$url_info['language'] = $q_config['default_language'];
}
}
}
}
return $url_info;
}

/* Add a function to remove false duplicate alternate hreflang tags if qtranslate_slug is installed */
add_action('get_header','remove_qts_slug_canonical');
function remove_qts_slug_canonical(){
global $qtranslate_slug;
remove_action('wp_head',array($qtranslate_slug, 'qtranslate_slug_header_extended'));
}

18 Gedanken zu „UPDATE : Problems migrating from mqtranslate with qtranslate-slug to qtranslate-x

    • Nope.
      They are thinking about merging the two plugins, but until now, if you want multilingual slugs, you need qtranslate-slug.

      Happy Coding

      • Merging the plugins is the best option!!

        Testing qtranslate-x I notice /admin not redirects to /wp-admin and you don’t have possibility for remove the /es ONLY for the home page in spanish (my website have spanish default language) for SEO optimization😦

      • Well, we’ll have to wait and see what the developers will do😉

  1. Well, thanks a lot!
    I just made the mistake to update the qtranslate-X to the newest version 3.2.9. Using your hack now i get an error:

    Fatal error: Cannot redeclare qtranxf_detect_language_front() (previously declared in /home/www/km-wordpress/wp-content/plugins/mqtranslate-migration-helper.php:13) in /home/www/km-wordpress/wp-content/plugins/qtranslate-x/qtranslate_core.php on line 367

    Any idea?
    Thank you!

    • Great, just great… The plugin author removed the possibility to overwrite the function qtranxf_detect_language_front(), which is why the fix doesn’t work anymore… I will look into this asap

  2. hello, i dont care about the slug, but i am afraid now that wp 4.3. is there and ready to install, my mq translate (i previously used q translate) will then not work anymore. which plugin should i install? qtranslate-x does not exist anymore, when i search for it…

    • Hi,

      Qtranslate and all plugins, that base on qtranslate (including -x) were pulled from the repository because of a Cross-Site Scripting Vulnerability. As far as i know, the plugin authors are working to check if the Vulnerability is in -x also, and try to get the plugin back to the repository.

      If you want to use qtranslate-x (while i would recommend waiting for it to come back to the repository, to be sure the vulnerability is fixed), you can get it on their github:
      https://github.com/qTranslate-Team/qtranslate-x

      • okay, thank you for your quick reply.
        i don´t mind waiting at all, as long as the english option/translations do not automatically disappear from my (otherwise german) website!🙂
        is it safe to upgrade to wp 4.3. in the meantime (and mq translate will keep on working)?

      • Well, while i can’t give you guarantee for that, i have a website running with qtranslate-x 3.4.3 + qtranslate-slug 1.1.17 on WordPress 4.3 and it seems to work.

        Be sure to make a backup of your db + files before upgrading (e.g. with backwpup)

        Happy Coding!

      • thanks, but i am not such a pro. wouldnt know how to recover my website from backups (i do them via ftp and the database/datenbank backup system)… i just sit and wait for now (dont have the -slug installed). as long as there is no automatic update to wp 4.3., as on all my other pages, i might be safe🙂, fingers crossed.

  3. Hello i also use q-translate slug(for everything tags category posts) and have redirection problem with returning visitors on my site, cause they get redirected to another language with main language tags post-names etc. will this trick work with lates q-translate x wordpress etc. Also i never used mqtranslate

    • Hm. Not quite sure what you mean by „cause they get redirected to another language with main language tags post-names“.

      Maybe the option „Detect the language of the browser and redirect accordingly“ is active? I know that this sometimes causes problems…

Schreibe einen Kommentar

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s