0

I'm setting up a multi-language blog using WPML. I works well using the button to change the language. Is there any way to automatically jump according to the visitor's language?

2
  • how you get visitor's language? Oct 14, 2011 at 9:12
  • I think we can get it using $_SERVER['HTTP_ACCEPT_LANGUAGE'] Oct 16, 2011 at 10:22

3 Answers 3

4

To improve on the excellent answer by Seh Horng, here is the same code to add to your functions.php, except you don't need to manually set $default_lang and $supported_lang (they are retrieved automatically):

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
}
2

If you are looking to customize WPML to allow remember of last language preference selected by your visitor, you can place the following codes in WP functions.php:

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

function my_icl_set_current_language() {

    $default_lang = 'en'; //set the default language code
    $supported_lang = array('zh-hans', 'en'); //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), '/');

        global $sitepress;
        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

}

With this customization, the visitor only needs to access http://www.yoursite.com/?lang=zh-hans once then it will remember the preference and all future requested pages should show the selected language i.e. Simplified Chinese accordingly unless other language has been selected or the lang cookie has expired.

1
  • I have posted an answer based on yours, with a small improvement to avoid having to enter $default_lang and $supported_lang manually. Feb 19, 2019 at 8:13
0

We use WPML 2.4.1 (Multilingual CMS version) and it includes this functionality.

WPML->Languages->Browser language redirect

Here is what the on-screen documentation has to say:

WPML can automatically redirect visitors according to browser language.

Important notes:

This feature should never be used when caching plugins are enabled. Visitors who have cookies disabled may have trouble switching between languages.

  • Disable browser language redirect

  • Redirect visitors based on browser language only if translations exist

  • Always redirect visitors based on browser language (redirect to home page if translations are missing)

Remember visitors' language preference for 24 hours.

Not the answer you're looking for? Browse other questions tagged or ask your own question.