简体   繁体   中英

How to detect browser language

I would like to detect browser language and then check if there is a language supported in my system and show them the page in that language.

<?php

$supportedLangs = array('en-GB', 'fr', 'de');

$languages = explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);

foreach($languages as $lang)
{
    if(in_array($lang, $supportedLangs))
    {
        // Set the page locale to the first supported language found
        $page->setLocale($lang);
        break;
    }
}
?>

In this way i was able to detect language from the HTTP_ACCEPT_LANGUAGE function.

But what if i want to check if there is just one language i need to check for example spanish ?

I need to check if the browser detect spanish language 'sp' so $supportedLangs is not anymore an array but just a variable containing a constant 'sp', and if this condition is verified i want to set a variable like $set=hola else $set=hello

How can i do that ?

Thank you so much !!

The language is stored in the following variable

     $language = $_SERVER['HTTP_ACCEPT_LANGUAGE']

You can get the language by adding a substr from 0 to 2. You'll have for instance = "fr", "en", etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM