Forums

WP_Multilingual and HTTP_ACCEPT_LANGUAGE (3 posts)

  1. pirusan
    Member
    Posted 4 years ago #

    Hi,

    In order to have an automatic detection of the visitor language, I'd like to be able to use the HTTP_ACCEPT_LANGUAGE header in the case of the first visit on the multilanguage site.

    At this moment, I use the following code:

    In the functions.php of my theme:

    if ( class_exists('WP_Multilingual') ) {
    
    function getAcceptedLanguage() {
       $languages = split(",", $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
       $lang_q = Array();
       foreach( $languages as $aLang ) {
           $lang_array = split(";q=", trim( $aLang ) );
           $lang = trim( $lang_array[0] );
           if( !isset( $lang_array[1] ) )
               $q = 1;
           else
               $q = trim($lang_array[1]);
           $lang_q["$lang"] = (float)$q;
       }
       arsort($lang_q);
       //extra code for making the languages key indexed
       $i = 0;
       $lang_index = Array();
       foreach($lang_q as $lang => $q) {
           $lang_index[$i] = $lang; //add to a new array the index key/language
           $i++;
       }
    
       return $lang_index;
    } // end of 'getAcceptedLanguage()'
    
    $siteLanguages = array_keys(WP_Multilingual::AvaibleLanguages());
    $acceptedLanguages = getAcceptedLanguage();
    foreach ( $acceptedLanguages as $key => $value ) {
        $language_ = explode('-', $value);
        $primaryLanguage = $language_[0];
        if ( in_array($primaryLanguage, $siteLanguages) ) {
            $theLang = $primaryLanguage;
            break;
        }
    } // endforeach
    
    function woodpok_set_lang(){
    
        global $theLang;
    
        if ( !isset($_COOKIE['language']) ) {
    
            setcookie("language", $theLang, time()+3600*24*365, '/');
            $link = WP_Multilingual::LanguageLink($_SERVER['REQUEST_URI'], $theLang);
    
            header('Location: '.$link);
            exit;
    
        } // endif
    
    } // end of 'woodpok_set_lang()'
    add_action('init', 'woodpok_set_lang', 100);
    
    } // endif

    In "multilingal.php", in the function "QueryString", I added:

    if ( isset($_COOKIE['language']) ) $language = $_COOKIE['language'];

    before:

    $link = WP_Multilingual::LanguageLink($_SERVER['REQUEST_URI'], $language);

    In "multilingal.php", in the function "SetLocale", I added:

    if ( $language && $_COOKIE['language'] != $language ) {
        setcookie("language", $language, time()+3600*24*365, '/');
    } // endif

    after:

    $language = isset($_GET['language']) ? $_GET['language'] : substr(str_replace(get_option('home'), '', 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']), 1, 2) ;

    It work for me at this moment but I can't upgrade for now :'(

    Do you think it will be possible for you to implement something like this directly in your plugin?

    Many thanks in advance for your positive answer :-)

    Cheers,
    David

  2. Oleg Butuzov
    Member
    Posted 4 years ago #

    i am smiling you my friend.
    first of all - i have add today language overide option. and you make yourself.

    why set cookie for language? is HTTP_ACCEPT_LANGUAGE not help you?

  3. pirusan
    Member
    Posted 4 years ago #

    OK, I just saw that (didn't update since my hack). It work fine.

    The use of the _COOKIE is simple: I'm french but (as developper), I want to test a new (version of a) brother in beta which is only available in english.

    The HTTP_ACCEPT_LANGUAGE is set to "en". When I visit my Multilingual Blog, it send me the page in english. I switch to french. With a cookie, next time I visit the blog, It'll still be in french.

    OK, not everybody use a brother in a language different of his mother language but I want to take in account this case.

    Thank for the "override" option any way, I can use a "non-hacked" version of your plugin.

    But, I would be nice to have an option (or hook) to force the language manually. What do you think of something like: add_action('wp_multilingual_setlocale' 'my_custom_setlocale');? ;-)

    Cheers,
    David

Topic Closed

This topic has been closed to new replies.

About this Topic