public static function getBrowserLanguage($browserLang = null)
{
static $replacementPatterns = array('/(\\\\.)/', '/(\\s+)/', '/(\\([^)]*\\))/', '/(;q=[0-9.]+)/', '/\\.(.*)/', '/^C$/');
if (is_null($browserLang)) {
$browserLang = self::sanitizeInputValues(@$_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (empty($browserLang) && self::isPhpCliMode()) {
$browserLang = @getenv('LANG');
}
}
if (empty($browserLang)) {
// a fallback might be to infer the language in HTTP_USER_AGENT (i.e., localized build)
$browserLang = "";
} else {
// language tags are case-insensitive per HTTP/1.1 s3.10 but the region may be capitalized per ISO3166-1;
// underscores are not permitted per RFC 4646 or 4647 (which obsolete RFC 1766 and 3066),
// but we guard against a bad user agent which naively uses its locale
$browserLang = strtolower(str_replace('_', '-', $browserLang));
// filters
$browserLang = preg_replace($replacementPatterns, '', $browserLang);
$browserLang = preg_replace('/((^|,)chrome:.*)/', '', $browserLang, 1);
// Firefox bug
$browserLang = preg_replace('/(,)(?:en-securid,)|(?:(^|,)en-securid(,|$))/', '$1', $browserLang, 1);
// unregistered language tag
$browserLang = str_replace('sr-sp', 'sr-rs', $browserLang);
// unofficial (proposed) code in the wild
}
return $browserLang;
}