Forked from LucaRosaldi/get-browser-language-code.php
Created
November 23, 2015 15:50
-
-
Save nicolasverlhiac/086ff0d226b022a8a2a8 to your computer and use it in GitHub Desktop.
PHP: Detect Browser Language
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Gets the user’s browser language, given an array of avalaible languages. | |
* @param array $availableLanguages avalaible languages for the site | |
* @param string $default default language for the site | |
* @return string language prefix | |
*/ | |
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