Piwik\Common::getBrowserLanguage PHP Метод

getBrowserLanguage() публичный статический Метод

Returns the browser language code, eg. "en-gb,en;q=0.5"
public static getBrowserLanguage ( string | null $browserLang = null ) : string
$browserLang string | null Optional browser language, otherwise taken from the request header
Результат string
    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;
    }

Usage Example

Пример #1
0
 /**
  * Guesses a visitor's location using a visitor's browser language.
  *
  * @param array $info Contains 'ip' & 'lang' keys.
  * @return array Contains the guessed country code mapped to LocationProvider::COUNTRY_CODE_KEY.
  */
 public function getLocation($info)
 {
     $enableLanguageToCountryGuess = Config::getInstance()->Tracker['enable_language_to_country_guess'];
     if (empty($info['lang'])) {
         $info['lang'] = Common::getBrowserLanguage();
     }
     $country = Common::getCountry($info['lang'], $enableLanguageToCountryGuess, $info['ip']);
     $location = array(parent::COUNTRY_CODE_KEY => $country);
     $this->completeLocationResult($location);
     return $location;
 }
All Usage Examples Of Piwik\Common::getBrowserLanguage