conditional (theme) language selection at ‘runtime’

I have two issues to change the language for the theme at ‘runtime’. I’m not sure about my question title, hope my description helps.

I already use a plugin Polylang and there nice features: link pages with equal content but different languages AND configure primary menu for each language AND a language switcher which will be added into the primary menu.

Read More

(I also tried xLanguage and xili-language, but they were not confortable for me)

Polylang has very nice features, but I need some more:

  • a guestbook, its a page which acts like a guestbook, no content only comments. I need the form, etc. (theme) in two languages, but is only one page
  • I have second menu in the footer, there I also want to use different menu elements (names and links) for each language

Is there a possibility to change the language by passing some HTTP GET params (and redirect) and then check the params and change/reload the page with needed language?

I already tried to change the wordpress_polylang value in the cookie, but no effect.
I found the reference for load_theme_textdomain but there is no parameter to pass an language.

I use Starkers theme, based on twentyten.

//edit:
I found this snippet in the core.php of the polylang plugin, perhaps it would help.

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

Related posts

Leave a Reply

1 comment

  1. I found a solution, realy dirty, but works.

    In the core.php file of the polylang plugin I found this stuff:

    // 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");
            }
        }
    }
    

    access to the pages via http get params:
    ?guestbook_lang=en and ?guestbook_lang=de