gpt4 book ai didi

wordpress - qTranslate for Wordpress 不会自动为我提供正确的语言

转载 作者:行者123 更新时间:2023-12-02 23:21:35 32 4
gpt4 key购买 nike

我使用 qTranslate for Wordpress 来发布英语、瑞典语和德语的博客文章。我已激活“检测浏览器语言”,以便访问者将被转发到其浏览器指定语言的正确 URL。

因此,如果我访问 blog.domain.com,我会被转移到 blog.domain.com/sv/,并且我的博客文章是瑞典语,这太棒了!但现在问题来了,如果我从同一台计算机再次访问 blog.domain.com,我不会被转移,并且博客文章的默认语言为英语。

我在这里做错了什么吗?看起来很奇怪,我总是需要指定语言,我需要它根据浏览器自动指定。

最佳答案

我遇到了同样的问题,我修改了 qTranslate 以添加此功能。我所做的是保存带有语言信息的 cookie,当用户单击小部件中的语言标志时,就会保存该 cookie。

我的逻辑如下:

  • 在显示所有语言的小部件中,将以下参数添加到每个 URL:?save_lang
  • 当此参数存在时,保存名称为“save_lang”且值 = $lang 的 cookie
  • 立即重定向到同一页面,但没有该参数“save_lang”
  • 当调用任何页面时,qTranslate 现在会将 default_language 设置为设置中的语言。如果 cookie 'save_lang' 存在,那么我将使用 cookie 中保存的语言覆盖 default_language

只需几步:

  1. 修改qtranslate_core.php文件:

            //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. 修改文件 qtranslate_widget.php(将“save_lang”参数添加到每种语言的 URL):

每次看到这一行:

qtrans_convertURL($url, $language)

将其替换为:

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

然后添加该功能:

// 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;

}

我希望这有帮助:)

关于wordpress - qTranslate for Wordpress 不会自动为我提供正确的语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10618636/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com