Detect Browser Language -- Ascoos Cms function

<?php /* __ _ ___ ___ ___ ___ ___ ____ _ __ ___ ___ / _` |/ / / __/ _ \ / _ \ / / / __/| '_ ` _ \ / / | (_| |\ \| (_| (_) | (_) |\ \ | (__ | | | | | |\ \ \__,_|/__/ \___\___/ \___/ /__/ \___\|_| |_| |_|/__/ */ function parseLanguageList($languageList) { if (is_null($languageList)) { if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { return array(); } $languageList = $_SERVER['HTTP_ACCEPT_LANGUAGE']; } $languages = array(); $languageRanges = explode(',', trim($languageList)); foreach ($languageRanges as $languageRange) { if (preg_match('/(\*|[a-zA-Z0-9]{1,8}(?:-[a-zA-Z0-9]{1,8})*)(?:\s*;\s*q\s*=\s*(0(?:\.\d{0,3})|1(?:\.0{0,3})))?/', trim($languageRange), $match)) { if (!isset($match[2])) { $match[2] = '1.0'; } else { $match[2] = (string) floatval($match[2]); } if (!isset($languages[$match[2]])) { $languages[$match[2]] = array(); } $languages[$match[2]][] = $match[1]; } } krsort($languages); return $languages; } /* EXAMPLE USE */ // for HEADER Accept-Language: el-GR,el;q=0.8,en-US;q=0.5,en; $accepted = parseLanguageList(); var_dump($accepted); echo "Browser Language : ".$accepted['1.0'][0]; /* EXAMPLE EXPORT var_dump($accepted); ==================== array(4) { ["1.0"]=> array(1) { [0]=> string(5) "el-GR" } ["0.8"]=> array(1) { [0]=> string(2) "el" } ["0.5"]=> array(1) { [0]=> string(5) "en-US" } ["0.3"]=> array(1) { [0]=> string(2) "en" } } echo "Browser Language : ".$accepted['1.0'][0]; ================================================= Browser Language : el-GR */ ?>
This function, is part of object $objLanguage of the Ascoos Cms and used for detect the browser language. Can be used and outside of the Ascoos Cms

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.