Detect Browser Language


/ Published in: PHP
Save to your folder(s)

Just provide $availableLanguages as an array('en', 'de', 'es')


Copy this code and paste it in your HTML
  1. function get_client_language($availableLanguages, $default='en'){
  2.  
  3. if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  4.  
  5. $langs=explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
  6.  
  7. //start going through each one
  8. foreach ($langs as $value){
  9.  
  10. $choice=substr($value,0,2);
  11. if(in_array($choice, $availableLanguages)){
  12. return $choice;
  13.  
  14. }
  15.  
  16. }
  17. }
  18. return $default;
  19. }

URL: php-detect-browser-language

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.