WPML’s issue about remembering the language preference

WPML has an issue about remembering the user’s language preference specially if you are using plugins with dynamic content such as woocommerce.

Just to give an example lets say the site’s default language is spanish. You chose to visit the english version of the homepage from the language switcher. Once you are on the english homepage and click on cart icon it will take you to spanish version of the cart since that is the default language. For the above example theme of the site is built in Elementor Theme Builder.

So i tried using the following code (source) to set a cookie when language is changed.

add_action('init', 'my_icl_set_current_language');
add_action('wp_loaded', 'my_icl_set_current_language');

function my_icl_set_current_language() {
    global $sitepress;
    $default_lang = $sitepress->get_default_language(); //set the default language code
    $supported_lang = array_keys(icl_get_languages('skip_missing=0&orderby=code')); //set the allowed language codes 
    $get_lang = $default_lang;

    $langtemp = @$_COOKIE['lang'];
    if (in_array($langtemp, $supported_lang)) $get_lang = $langtemp;

    $langtemp = @$_GET['lang'];
    if (in_array($langtemp, $supported_lang)) $get_lang = $langtemp;

    if (in_array($get_lang, $supported_lang)) {
        //save cookie setting
        setcookie ('lang', $get_lang, time() + (10 * 365 * 24 * 60 * 60), '/');

        if ($sitepress->get_current_language() != $get_lang) {
            $sitepress->switch_lang($get_lang, true);
        }
    }
    define('CURRENT_LANGUAGE_CODE', $get_lang); //use this constant to check the current language code instead of ICL_LANGUAGE_CODE
}

Now the issue is that this code prevents the language switcher from switching back to default because there is no url parameter in case of default language and code replaces/creates a cookie if the there is a url variable for language.

To resolve the issue i had to include the url variable for the default language. Code on WPML’s site to modify language switcher does not work if the header is built in Elementor Theme Builder. Below is the modified version of the WPML’s custom language switcher that works with Elementor Theme Builder and adds url variable in case of default language as well.

add_filter('wp_nav_menu_main-navigation-menu_items', function ($items, $args) {
    // uncomment this to find your theme's menu location
    //echo "args:<pre>"; print_r($args); echo "</pre>";
 
    // get languages
    $languages = apply_filters( 'wpml_active_languages', NULL, 'skip_missing=0' );
 
        if(!empty($languages)){
 
            foreach($languages as $l){
                if($l['language_code'] == 'es'){
                    // flag with native name
                    $items = $items . '<li class="menu-item"><a href="' . $l['url'] . '?lang=es"><img src="' . $l['country_flag_url'] . '" height="12" alt="' . $l['language_code'] . '" width="18" /></a></li>';
                    //only flag
                    //$items = $items . '<li class="menu-item menu-item-language"><a href="' . $l['url'] . '"><img src="' . $l['country_flag_url'] . '" height="12" alt="' . $l['language_code'] . '" width="18" /></a></li>';
                }else{
                    $items = $items . '<li class="menu-item"><a href="' . $l['url'] . '"><img src="' . $l['country_flag_url'] . '" height="12" alt="' . $l['language_code'] . '" width="18" /></a></li>';
                }
            }
        }
 
    return $items;
}, 10, 2);

Adding both the above functions to your theme’s functions.php file should add the functionality for remembering the language preference with a cookie.

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *