How do I change a plugin language of only one page?

My WordPress blog is set up as nl_NL. This means that my bookly plugin is also displayed in nl_NL.

In the Plugin directory there is a folder languages with loads of other languages.

I have one page in three different languages nl_NL, de_DE, en_EN, on this page I would like the booking plugin to be displayed in the correct language.

I changed the page language via page id from nl_NL to de_DE

Via the function.php, but this had no effect.

function get_top_parent_page_id() { 
    global $post; 

    if ($post-ancestors) { 
        return end($post-ancestors); 
    } else { 
        return $post-ID; 
    } 
}

function addLangMetaTag (){
    $postLanguage = "nl_NL";
    if (is_page()) {
    $svPageID = get_top_parent_page_id(); // ID of parent page
            if ($svPageID == "13040") { // ID of the "på svenska" page
                $postLanguage = "de_DE";    
            }
        echo "meta http-equiv=\"content-language\" content=\"" . $postLanguage . "\"";
    }
}
add_filter( 'wp_head', 'addLangMetaTag' );

function language_tagger_change_html_lang_tag() {
    return "dir=\"ltr\" lang=\"" . 
language_tagger_determin_lang_tag() . "\"";
}

function language_tagger_determin_lang_tag() {
    $postLanguage = 'nl_NL'; // default language
    if (is_page()) {
    $svPageID = get_top_parent_page_id(); // ID of parent page
    if ($svPageID == "13040") { // ID of the "på svenska" page
                $postLanguage = "de_DE";    
            }
    }
    return $postLanguage;
}

add_filter('language_attributes', 
'language_tagger_change_html_lang_tag');

I think it will only look at the WordPress config.php define('WPLANG', 'nl_NL'); I have also been reading this post maybe I could combine something?

Topic language multi-language plugins Wordpress

Category Web


What you're doing is changing the http-equiv meta tag. Changing the HTML tags doesn't change the language of the page content. It just tells browsers (and bots/spiders) what language the content is supposed to be.

If you want to change the language the content is displayed in, you need to change the textdomain for your bookly plugin.

You can do that with WP's plugin_locale filter. That filter runs when the language files are being loaded and it allows you to change the textdomain (de_DE or nl_NL) that is used.

add_filter( 'plugin_locale', function( $locale ) {
    if (is_page()) {
        $svPageID = get_top_parent_page_id(); // ID of parent page
        if ($svPageID == "13040") { // ID of the "på svenska" page
            $locale = "de_DE";    
        }
    }
    return $locale;
});

If you need to change more than just the plugin's output (i.e. the language of the entire page), then you probably determine_locale filter in the determine_locale() function. It would be the same as above, but with determine_locale as the filter hook.


A simple way to solve this is to use the "Ceceppa Multilingua" plugin. https://wordpress.org/plugins/ceceppa-multilingua/screenshots/

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.