在运行时选择有条件的(主题)语言

时间:2012-02-23 作者:timaschew

我有两个问题需要在“运行时”更改主题的语言。我不确定我的问题标题,希望我的描述能有所帮助。

我已经使用了一个插件Polylang,它有很好的特性:链接内容相同但语言不同的页面,为每种语言配置主菜单,以及将添加到主菜单中的语言切换器。

(我也试过xLanguage和xili语言,但对我来说不太合适)

Polylang有很好的功能,但我还需要一些:

留言簿,它是一个像留言簿一样的页面,没有内容,只有评论。我需要两种语言的表单等(主题),但只有一个页面,我在页脚中有第二个菜单,我还想为每种语言使用不同的菜单元素(名称和链接),是否可以通过传递一些HTTP GET参数(和重定向)来更改语言,然后检查参数并用所需的语言更改/重新加载页面?

我已经试着改变wordpress_polylang cookie中的值,但没有效果。我找到了load_theme_textdomain 但是没有参数来传递语言。

我使用Starkers主题,基于二十世纪十年代。

//编辑:我在核心中找到了这个片段。polylang插件的php,也许会有所帮助。

// NOTE: I believe there are two ways for a plugin to force the WP language
// as done by xili_language and here: load text domains and reinitialize wp_locale 
//    with the action \'wp\'
// as done by qtranslate: define the locale with the action \'plugins_loaded\', but
//     in this case, the language must be specified in the url.
function load_textdomains() {
    // sets the current language
    if (!($this->curlang = $this->get_current_language()))
        return; // something went wrong

    // set a cookie to remember the language. check headers have not been sent to avoid ugly error
    if (!headers_sent())
        setcookie(\'wordpress_polylang\', $this->curlang->slug, time() + 31536000 /* 1 year */, COOKIEPATH, COOKIE_DOMAIN);

    // set all our language filters and actions
    $this->add_language_filters();

    // our override_load_textdomain filter has done its job. let\'s remove it before calling load_textdomain
    remove_filter(\'override_load_textdomain\', array(&$this, \'mofile\'));

    // now we can load text domains with the right language
    $new_locale = get_locale();
    foreach ($this->list_textdomains as $textdomain)
        load_textdomain( $textdomain[\'domain\'], str_replace($this->default_locale, $new_locale, $textdomain[\'mo\']));

    // and finally load user defined strings (check first that base64_decode is not disabled)
    if (function_exists(\'base64_decode\')) {
        global $l10n;
        $mo = new MO();
        $reader = new POMO_StringReader(base64_decode(get_option(\'polylang_mo\'.$this->curlang->term_id)));
        $mo->import_from_reader($reader);
        $l10n[\'pll_string\'] = &$mo;
    }

    // reinitializes wp_locale for weekdays and months, as well as for text direction
    global $wp_locale;
    $wp_locale->init();
    $wp_locale->text_direction = get_metadata(\'term\', $this->curlang->term_id, \'_rtl\', true) ? \'rtl\' : \'ltr\';
}

1 个回复
SO网友:timaschew

我找到了一个解决方案,虽然很脏,但很有效。

在核心。我找到的polylang插件的php文件如下:

// NOTE: I believe there are two ways for a plugin to force the WP language
// as done by xili_language and here: load text domains and reinitialize wp_locale with the action \'wp\'
// as done by qtranslate: define the locale with the action \'plugins_loaded\', but in this case, the language must be specified in the url.
function load_textdomains() {
    // sets the current language

    if (!($this->curlang = $this->get_current_language()))
    return; // something went wrong

    // --- modified --> check for guestbook, which should be display in different languages
    $this->check_joined_content();
    ...
}

define("GUESTBOOK_PAGE_ID", "12");
define("GUESTBOOK_HTTP_PARAM", "guestbook_lang");

function check_joined_content() {
    $gl = $_GET[GUESTBOOK_HTTP_PARAM];
    if(isset($gl)) {
        if ($this->curlang->object_id != GUESTBOOK_PAGE_ID) {
            return;
        }
        if ($gl == "de") {
            $this->curlang = $this->get_language("de");
        } else {
            $this->curlang = $this->get_language("en");
        }
    }
}
通过http get参数访问页面:?guestbook_lang=en?guestbook_lang=de

结束

相关推荐

How to change language?

很抱歉,这是一个完整的新手问题,但我在谷歌上搜索了它,找不到这个简单的信息。我今天刚安装了Wordpress。现在我的Wordpress博客上的所有文字都是英文:“Home”、“About”、“Comments”等。我怎样才能改变语言?更新:事实上,我还没有走出困境。我有:找到fr\\u fr.mo文件并将其复制到新目录“/wp content/languages/”中修改了wp配置。php文件中包含以下行:定义(\'WPLANG\',\'fr\\u fr\');不幸的是,我仍然得到英文界面。任何帮助都将