extractLanguageCodeFromBrowserLanguage() public static method

* The language tag is defined by ISO 639-1
public static extractLanguageCodeFromBrowserLanguage ( string $browserLanguage, array $validLanguages = [] ) : string
$browserLanguage string Browser's accepted langauge header
$validLanguages array array of valid language codes
return string 2 letter ISO 639 code 'es' (Spanish)
Example #1
0
 protected function aggregateByLanguage()
 {
     /** @var RegionDataProvider $regionDataProvider */
     $regionDataProvider = StaticContainer::get('Piwik\\Intl\\Data\\Provider\\RegionDataProvider');
     $query = $this->getLogAggregator()->queryVisitsByDimension(array("label" => self::LANGUAGE_DIMENSION));
     $countryCodes = $regionDataProvider->getCountryList($includeInternalCodes = true);
     $metricsByLanguage = new DataArray();
     while ($row = $query->fetch()) {
         $langCode = Common::extractLanguageCodeFromBrowserLanguage($row['label']);
         $countryCode = Common::extractCountryCodeFromBrowserLanguage($row['label'], $countryCodes, $enableLanguageToCountryGuess = true);
         if ($countryCode == 'xx' || $countryCode == $langCode) {
             $metricsByLanguage->sumMetricsVisits($langCode, $row);
         } else {
             $metricsByLanguage->sumMetricsVisits($langCode . '-' . $countryCode, $row);
         }
     }
     $report = $metricsByLanguage->asDataTable();
     $this->insertTable(self::LANGUAGE_RECORD_NAME, $report);
 }
Example #2
0
 /**
  * @dataProvider getLanguageDataToExtractLanguageCode
  */
 public function testExtractLanguageCodeFromBrowserLanguage($browserLanguage, $validLanguages, $expected)
 {
     $this->assertEquals($expected, Common::extractLanguageCodeFromBrowserLanguage($browserLanguage, $validLanguages), "test with {$browserLanguage} failed, expected {$expected}");
 }
 /**
  * @return string Two letters language code, eg. "fr"
  */
 public static function getLanguageCodeForCurrentUser()
 {
     $languageCode = self::getLanguageFromPreferences();
     if (!API::getInstance()->isLanguageAvailable($languageCode)) {
         $languageCode = Common::extractLanguageCodeFromBrowserLanguage(Common::getBrowserLanguage(), API::getInstance()->getAvailableLanguages());
     }
     if (!API::getInstance()->isLanguageAvailable($languageCode)) {
         $languageCode = Translate::getLanguageDefault();
     }
     return $languageCode;
 }
Example #4
0
 protected function aggregateByLanguage()
 {
     $query = $this->getLogAggregator()->queryVisitsByDimension(array("label" => self::LANGUAGE_DIMENSION));
     $languageCodes = array_keys(Common::getLanguagesList());
     $metricsByLanguage = new DataArray();
     while ($row = $query->fetch()) {
         $code = Common::extractLanguageCodeFromBrowserLanguage($row['label'], $languageCodes);
         $metricsByLanguage->sumMetricsVisits($code, $row);
     }
     $report = $metricsByLanguage->asDataTable();
     $this->insertTable(self::LANGUAGE_RECORD_NAME, $report);
 }
Example #5
0
 protected function aggregateByLanguage()
 {
     $query = $this->getLogAggregator()->queryVisitsByDimension(array("label" => self::LANGUAGE_DIMENSION));
     $languageCodes = array_keys(Common::getLanguagesList());
     $countryCodes = Common::getCountriesList($includeInternalCodes = true);
     $metricsByLanguage = new DataArray();
     while ($row = $query->fetch()) {
         $langCode = Common::extractLanguageCodeFromBrowserLanguage($row['label'], $languageCodes);
         $countryCode = Common::extractCountryCodeFromBrowserLanguage($row['label'], $countryCodes, $enableLanguageToCountryGuess = true);
         if ($countryCode == 'xx' || $countryCode == $langCode) {
             $metricsByLanguage->sumMetricsVisits($langCode, $row);
         } else {
             $metricsByLanguage->sumMetricsVisits($langCode . '-' . $countryCode, $row);
         }
     }
     $report = $metricsByLanguage->asDataTable();
     $this->insertTable(self::LANGUAGE_RECORD_NAME, $report);
 }