public static function extractCountryCodeFromBrowserLanguage($browserLanguage, $validCountries, $enableLanguageToCountryGuess)
{
/** @var LanguageDataProvider $dataProvider */
$dataProvider = StaticContainer::get('Piwik\\Intl\\Data\\Provider\\LanguageDataProvider');
$langToCountry = $dataProvider->getLanguageToCountryList();
if ($enableLanguageToCountryGuess) {
if (preg_match('/^([a-z]{2,3})(?:,|;|$)/', $browserLanguage, $matches)) {
// match language (without region) to infer the country of origin
if (array_key_exists($matches[1], $langToCountry)) {
return $langToCountry[$matches[1]];
}
}
}
if (!empty($validCountries) && preg_match_all('/[-]([a-z]{2})/', $browserLanguage, $matches, PREG_SET_ORDER)) {
foreach ($matches as $parts) {
// match location; we don't make any inferences from the language
if (array_key_exists($parts[1], $validCountries)) {
return $parts[1];
}
}
}
return self::LANGUAGE_CODE_INVALID;
}