• is it possible to separate the language of the site (menu, title etc) from the language of the content ?

    I explain :
    I use woocommerce to sell book in french and english.
    I need to display the books in english on the french version of the site (and vice versa). So I use this solution.
    But when I go on a page book in english from the french version of the site, the language change automatically of french to english.

    I want the users choose the language and it don’t change except if he switch himself with the language switcher.

    Is it possible ?

    https://www.ads-software.com/plugins/polylang/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Support Chrystl

    (@chrystl)

    This solution provides by Syllogic consultant doesn’t work anymore.

    Not sure that other users have proposed another solution.

    Thread Starter tolima

    (@tolima)

    The Syllogoc solution doesn’t work anymore but it’s possible to adapt it.
    here is what I done :

    add_filter('pre_get_posts', 'get_default_language_posts');
    
    function get_default_language_posts($query)
    {
        if ($query->is_main_query() && isset($query->query['lang']) && !is_admin()) {
            $terms = get_terms('post_translations'); //polylang stores translated post IDs in a serialized array in the description field of this custom taxonomy
            $curLang = $query->query['lang']; //current selected language requested on the broswer
            $otherLang = $curLang === "fr" ? "en" : "fr";
            $filterPostIDs = array();
            foreach ($terms as $translation) {
                $transPost = unserialize($translation->description);
                $filterPostIDs[] = $transPost[$otherLang];
            }
            $query->set('lang',  $curLang. ',' . $otherLang);  //select both default and current language post
            $query->set('post__not_in', $filterPostIDs); // remove the duplicate post
        }
        return $query;
    }

    But my problem is not to display post in other language. I want the language is not associated with the content but only the choice of the user.

    I have the same problem and have tested the approach. But it doesn’t work for me.

    Thread Starter tolima

    (@tolima)

    Are you sure it’s the same problem ?
    I want to deactivate the auto-language-switcher-depending-on-content of polylang and only switch language by the language switcher.

    ah ok sorry, my mistake. Currently, I’m looking for a solution where the default language is shown whenever no translation is available. And here no working solution seems to exist :-/

    Thread Starter tolima

    (@tolima)

    It’s work for me so it can work for you. But with my solution you’ll have the same problem than me : when the user will go to the “foreign” post, the language will switch (menu, static translations etc)

    Thread Starter tolima

    (@tolima)

    May be I found a workaround but not sure if this is the best solution or create bugs.
    the workaround :
    – in polylang settings I choose URL modifications>The language is set from content
    – in the plugin polylang>frontend>choose-lang-content.php, find the “get_language_from_content()” function and comment that :

    elseif ((is_single() || is_page() || (is_attachment() && $this->options['media_support'])) && ( ($var = get_queried_object_id()) || ($var = get_query_var('p')) || ($var = get_query_var('page_id')) || ($var = get_query_var('attachment_id')) ))
    $lang = $this->model->get_post_language($var);

    – in the end you have to use your own switcher. Here is mine (based on the Syllogic one):

    function sy_polylang_switcher()
    {
        $translations = array();
        $translations = pll_the_languages(array('raw' => 1));
        foreach ($translations as $key => $language) {
            if (!$language['current_lang']) {
                $alternate = $language;
                break;
            }
        }
        $actual_link = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        if ($alternate["no_translation"]) {
            $menu = '<a hreflang="'.$alternate['slug'].'" href="' . $actual_link . '&lang=' . $alternate['slug'] . '">' . $alternate['name'] . '</a></li>';
        } elseif (is_home() || is_shop()) {
            $menu = '<a hreflang="'.$alternate['slug'].'" href="' . $alternate['url'] . '">' . $alternate['name'] . '</a></li>';
        } else {
            $menu = '<a hreflang="'.$alternate['slug'].'" href="' . $alternate['url'] . '&lang=' . $alternate['slug'] . '">' . $alternate['name'] . '</a></li>';
        }
        $menu .= '</ul>';
        echo '<div id="polylang_switcher">' . $menu . '</div>';
    }

    put this code in functions.php and call the switcher in your theme. <?php sy_polylang_switcher(); ?>

    Not sure it’s perfect but seems to work. I come back here if I saw problems.

    Thanks for the code tolima. To get your code to work for me I had to replace the & symbols with the ? symbol.

    I also had an issue where the code would stack up and eventually would end up with something along the lines of ?lang=en?lang=pt?lang=pt?lang=pt which would keep growing as I clicked.

    To solve this I added this after your $actual_link line.

    $actual_link = substr($actual_link, 0, strpos($actual_link, "?"));

    So my code ended up like this:

    function sy_polylang_switcher()
    		{
    		    $translations = array();
    		    $translations = pll_the_languages(array('raw' => 1));
    		    foreach ($translations as $key => $language) {
    		        if (!$language['current_lang']) {
    		            $alternate = $language;
    		            break;
    		        }
    		    }
    		    $actual_link = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    				$actual_link = substr($actual_link, 0, strpos($actual_link, "?"));
    		    if ($alternate["no_translation"]) {
    		        $menu = '<a hreflang="'.$alternate['slug'].'" href="' . $actual_link . '?lang=' . $alternate['slug'] . '">' . $alternate['name'] . '</a></li>';
    		    } elseif (is_home() || is_shop()) {
    		        $menu = '<a hreflang="'.$alternate['slug'].'" href="' . $alternate['url'] . '">' . $alternate['name'] . '</a></li>';
    		    } else {
    		        $menu = '<a hreflang="'.$alternate['slug'].'" href="' . $alternate['url'] . '?lang=' . $alternate['slug'] . '">' . $alternate['name'] . '</a></li>';
    		    }
    		    $menu .= '</ul>';
    		    echo '<div id="polylang_switcher">' . $menu . '</div>';
    		}

    I’m not sure if what I have done is correct but it’s working for me, so I thought I’d post it here incase someone has the same issue as me.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘NOT change the language depending on the content’ is closed to new replies.