Ever wanted to give response to the users in the language they prefer? Most browsers pass the language preference using the HTTP header ‘Accept-Language’ which looks like ‘Accept-Language: en-us,en;q=0.5‘. We can use this to detect the users first preferred language and show the response in that language.

$languages_supported_by_browser=$_SERVER["HTTP_ACCEPT_LANGUAGE"];
$pos=strpos($languages_supported_by_browser,";");
if ($pos>0) {
    $languages_supported_by_browser =
        substr($languages_supported_by_browser,0,$pos);
}
$languages=split(",",$languages_supported_by_browser);
echo "First language supported by browser is: ".
        $languages[0].".";

Using the variable ‘$languages[0]‘ we can redirect the page to the language specific URL, include a perticular language file, etc to achieve the language specific response. We can also have a list of languages we support in an array and keep looping through the array ‘$languages‘ and comparing the two, we can easily find the languages that we support and is preferred by the browser.

Leave a Reply