qTranslate for Wordpress doesn't automatic give me the correct language

时光怂恿深爱的人放手 提交于 2019-12-07 03:14:46

问题


I use qTranslate for Wordpress to have my blog posts in English, Swedish and German. I have activated the "Detect Browser Language" so that the visitor will be forwarded to the correct URL for the language specified by his browser.

So if I visit blog.domain.com I get transfered to blog.domain.com/sv/ and my blog posts is in Swedish, that is great! But now to the problem, if I visit blog.domain.com again a second time from the same computer I don't get transfered and the blog post is in default language English.

Is there something I'm doing wrong here? Seems strange that I always need to specify the language, I need it to be automatic based on the browser.


回答1:


I had the same problem and I have modified the qTranslate to add this functionality. What I did was to save a cookie with the language info, this cookie is saved when the user clicks on the language's flag in the widget.

My logic is the following:

  • In the widget displaying all the languages, add the following param to each URL: ?save_lang
  • When this param exists, save a cookie with name 'save_lang' and value = $lang
  • Immediately redirect to the same page, but without that param 'save_lang'
  • When calling any page, right now qTranslate will set the default_language to the one in the settings. If the cookie 'save_lang' exists, then I will override the default_language with the one saved in the cookie

So few steps:

  1. Modify 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;
    }
    
  2. Modify file qtranslate_widget.php (to add the 'save_lang' param to each's language URL):

Every time you see this line:

qtrans_convertURL($url, $language)

replace it with:

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

And then add that 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;

}

I hope this helps :)




回答2:


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

It seems easier and works just fine:

Just alter 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 you have translation in your site.




回答3:


Difficult to point out what might be wrong with the above description

The possible error might be when you login first you are taking the input from "Detect Browser Language". And from next time retrieving it from a cookie. There might be something that's gone wrong with this process. Or you might not be using cookies at all.

You can save the user's language preset if you have a login database. Then change the url according to the desired language soon as they login.



来源:https://stackoverflow.com/questions/10618636/qtranslate-for-wordpress-doesnt-automatic-give-me-the-correct-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!