public function getBrowserLanguage() { // Detection code from Full Operating system language detection, by Harald Hope // Retrieved from http://techpatterns.com/downloads/php_language_detection.php $user_languages = array(); //check to see if language is set if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) { $languages = strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]); // $languages = ' fr-ch;q=0.3, da, en-us;q=0.8, en;q=0.5, fr;q=0.3'; // need to remove spaces from strings to avoid error $languages = str_replace(' ', '', $languages); $languages = explode(",", $languages); foreach ($languages as $language_list) { // pull out the language, place languages into array of full and primary // string structure: $temp_array = array(); // slice out the part before ; on first step, the part before - on second, place into array $temp_array[0] = substr($language_list, 0, strcspn($language_list, ';')); //full language $temp_array[1] = substr($language_list, 0, 2); // cut out primary language if (strlen($temp_array[0]) == 5 && (substr($temp_array[0], 2, 1) == '-' || substr($temp_array[0], 2, 1) == '_')) { $langLocation = strtoupper(substr($temp_array[0], 3, 2)); $temp_array[0] = $temp_array[1] . '-' . $langLocation; } //place this array into main $user_languages language array $user_languages[] = $temp_array; } } else { $user_languages[0] = array('', ''); //return blank array. } $this->language = null; $basename = basename(__FILE__, '.php') . '.ini'; // Try to match main language part of the filename, irrespective of the location, e.g. de_DE will do if de_CH doesn't exist. $fs = new AKUtilsLister(); $iniFiles = $fs->getFiles(dirname(__FILE__), '*.' . $basename); if (empty($iniFiles) && $basename != 'kickstart.ini') { $basename = 'kickstart.ini'; $iniFiles = $fs->getFiles(dirname(__FILE__), '*.' . $basename); } if (is_array($iniFiles)) { foreach ($user_languages as $languageStruct) { if (is_null($this->language)) { // Get files matching the main lang part $iniFiles = $fs->getFiles(dirname(__FILE__), $languageStruct[1] . '-??.' . $basename); if (count($iniFiles) > 0) { $filename = $iniFiles[0]; $filename = substr($filename, strlen(dirname(__FILE__)) + 1); $this->language = substr($filename, 0, 5); } else { $this->language = null; } } } } if (is_null($this->language)) { // Try to find a full language match foreach ($user_languages as $languageStruct) { if (@file_exists($languageStruct[0] . '.' . $basename) && is_null($this->language)) { $this->language = $languageStruct[0]; } else { } } } else { // Do we have an exact match? foreach ($user_languages as $languageStruct) { if (substr($this->language, 0, strlen($languageStruct[1])) == $languageStruct[1]) { if (file_exists($languageStruct[0] . '.' . $basename)) { $this->language = $languageStruct[0]; } } } } // Now, scan for full language based on the partial match }
public function getBrowserLanguage() { // Detection code from Full Operating system language detection, by Harald Hope // Retrieved from http://techpatterns.com/downloads/php_language_detection.php $user_languages = array(); //check to see if language is set if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) ) { $languages = strtolower( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ); // $languages = ' fr-ch;q=0.3, da, en-us;q=0.8, en;q=0.5, fr;q=0.3'; // need to remove spaces from strings to avoid error $languages = str_replace( ' ', '', $languages ); $languages = explode( ",", $languages ); foreach ( $languages as $language_list ) { // pull out the language, place languages into array of full and primary // string structure: $temp_array = array(); // slice out the part before ; on first step, the part before - on second, place into array $temp_array[0] = substr( $language_list, 0, strcspn( $language_list, ';' ) );//full language $temp_array[1] = substr( $language_list, 0, 2 );// cut out primary language if( (strlen($temp_array[0]) == 5) && ( (substr($temp_array[0],2,1) == '-') || (substr($temp_array[0],2,1) == '_') ) ) { $langLocation = strtoupper(substr($temp_array[0],3,2)); $temp_array[0] = $temp_array[1].'-'.$langLocation; } //place this array into main $user_languages language array $user_languages[] = $temp_array; } } else// if no languages found { $user_languages[0] = array( '','' ); //return blank array. } $this->language = null; // First scan for full languages $basename=basename(__FILE__, '.php') . '.ini'; foreach($user_languages as $languageStruct) { if (@file_exists($languageStruct[0].'.'.$basename) && is_null($this->language)) { $this->language = $languageStruct[0]; } } // If we matched a full filename, there's no point going on if(!is_null($this->language)) return; // Try to match main language part of the filename, irrespective of the location, e.g. de_DE will do if de_CH doesn't exist. $fs = new AKUtilsLister(); $iniFiles = $fs->getFiles( '.', '*.'.$basename ); if (!is_array($iniFiles)) return; // Get out of here if no Kickstart Translation INI's were found foreach($user_languages as $languageStruct) { if(is_null($this->language)) { // Get files matching the main lang part $iniFiles = $fs->getFiles( '.', $languageStruct[1].'-??.'.$basename ); if (count($iniFiles) > 0) { $this->language = substr(basename($iniFiles[0]['name']), 0, 5); } else $this->language = null; } } }