Add UA::getLocale()

This commit is contained in:
Kijin Sung 2020-07-05 20:21:44 +09:00
parent 154bddfd18
commit dfc1082c85
3 changed files with 33 additions and 10 deletions

View file

@ -177,6 +177,21 @@ class UA
return self::$_robot_cache[$ua] = false;
}
/**
* This method parses the Accept-Language header to guess the browser's default locale.
*
* @param string $header (optional)
* @return string
*/
public static function getLocale($header = null)
{
// Get the Accept-Language header if the caller did not specify $header.
$header = $header ?: (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : 'en-US');
// Return the first locale name found.
return preg_match('/^([a-z0-9_-]+)/i', $header, $matches) ? $matches[1] : 'en-US';
}
/**
* This method parses the User-Agent string to guess what kind of browser it is.
*