Skip to content

Instantly share code, notes, and snippets.

@phpdistiller
Last active January 1, 2016 00:29
Show Gist options
  • Select an option

  • Save phpdistiller/8067155 to your computer and use it in GitHub Desktop.

Select an option

Save phpdistiller/8067155 to your computer and use it in GitHub Desktop.
This snippet detects the browser language and provides $available_languages as an array('en', 'fr', 'es').
<?php
// Source : http://snipplr.com/view/12631/detect-browser-language/
function get_client_language($available_languages, $default='en'){
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$langs=explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach ($langs as $value){
$choice=substr($value,0,2);
if(in_array($choice, $available_languages)){
return $choice;
}
}
}
return $default;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment