Skip to content

Instantly share code, notes, and snippets.

@Shaz3e
Created July 4, 2015 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shaz3e/1a142cd244c6e6cf44d3 to your computer and use it in GitHub Desktop.
Save Shaz3e/1a142cd244c6e6cf44d3 to your computer and use it in GitHub Desktop.
Detect browser language

If your website is multilingual, it can be useful to detect the browser language to use this language as the default. The code below will return the language used by the client’s browser.

<?php
function get_client_language($availableLanguages, $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, $availableLanguages)){
return $choice;
}
}
}
return $default;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment