QTranslate for Wordpress does not automatically give me the correct language

I use qTranslate for Wordpress to have my blog posts in English, Swedish and German. I activated "Detect browser language" so that the visitor is redirected to the correct URL for the language specified in his browser.

So, if I am on blog.domain.com, I get a translation on blog.domain.com/sv/, and my blog posts in Swedish are great! But now to the problem, if I switch to blog.domain.com again for the second time from the same computer, I do not receive the transfer, and the default blog post is in English.

Am I doing something wrong here? It seems strange that I always need to specify the language, I need it to be automatically based on the browser.

+6
source share
3 answers

I had the same problem and changed qTranslate to add this function. I did to save a cookie with language information, this cookie is saved when the user clicks on the language flag in the widgets.

My logic is this:

  • In widgets displaying all languages, add the following parameter to each URL :? save_lang
  • When this option exists, save the cookie with the name "save_lang" and the value = $ lang
  • Redirect to the same page immediately, but without this option save_lang
  • When you call any page, right now qTranslate will set the default value for the file in the settings. If the cookie 'save_lang' exists, then I will override the default_language file with the cookie saved

So a few steps:

  • Modify the qtranslate_core.php file:

    //Save the cookie if param ?save_lang is set, and then redirect to the same page without the param add_action('qtranslate_loadConfig', 'custom_qtranslate_loadConfig'); function custom_qtranslate_loadConfig() { global $q_config, $_COOKIE; // By default, if the save_lang cookie is set, use that one instead if(isset($_COOKIE['save_lang'])) { $q_config['default_language'] = $_COOKIE['save_lang']; } } // Priority 3: load after function qtrans_init (it has priority 2) add_action('plugins_loaded', 'custom_after_qtrans_init', 3); function custom_after_qtrans_init() { global $q_config, $_COOKIE; if (isset($_GET["save_lang"])) { // cookie will last 30 days setcookie('save_lang', $q_config['language'], time()+86400*30, $q_config['url_info']['home'], $q_config['url_info']['host']); wp_redirect(remove_url_param("save_lang", $q_config['url_info']['url'])); exit(); } } function remove_url_param($param_rm, $url) { $new_url = str_replace("?$param_rm", '', $url); $new_url = str_replace("&$param_rm", '', $new_url); return $new_url; } 
  • Modify the qtranslate_widget.php file (to add the save_lang parameter to each language URL):

Every time you see this line:

 qtrans_convertURL($url, $language) 

replace it with:

 add_url_param(qtrans_convertURL($url, $language), "save_lang") 

Then add this function:

 // Function to add a parameter to a URL function add_url_param($url, $name, $value = '') { // Pick the correct separator to use $separator = "?"; if (strpos($url,"?")!==false) $separator = "&"; // Find the location for the new parameter $insertPosition = strlen($url); if (strpos($url,"#")!==false) $insertPosition = strpos($url,"#"); $withValue = ($value == '' ? '' : "=$value"); // Build the new url $newUrl = substr_replace($url,"$separator$name$withValue",$insertPosition,0); return $newUrl; } 

Hope this helps :)

+2
source

I found a good .htaccess rule that can set a lang cookie here: http://tips.naivist.net/2012/11/09/remembering-the-user-language-choice/

It seems simpler and works fine:

Just change your main .htaccess to this:

 # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / # Language Cookie redirect RewriteCond %{REQUEST_URI} ^/$ [NC] RewriteCond %{HTTP_COOKIE} lang=(lang1|lang2) [NC] RewriteRule ^(.*)$ /%1/ [R=302,L] RewriteCond %{REQUEST_URI} ^/(lang1|lang2)/.*$ [NC] RewriteCond %{HTTP_COOKIE} !lang=%1 [NC] RewriteRule . - [cookie=lang:%1:.%{HTTP_HOST}:144000:/] RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress 

In lang1 and lang2 you can insert the languages โ€‹โ€‹in which you have a translation on your site.

+2
source

It is difficult to indicate what might be wrong with the description above.

A possible mistake may be that when you first log into the system, you enter the input with "Define browser language". And next time, extracting it from the cookie. Perhaps something happened to this process. Or you cannot use cookies at all.

You can save a preliminary set of user language if you have a database to log in. Then change the URL to the desired language shortly after logging in.

0
source

Source: https://habr.com/ru/post/915906/


All Articles