Skip to content

Commit

Permalink
Merge pull request #131 from Vediovis/master
Browse files Browse the repository at this point in the history
detect language by apache mod_geoip country code
  • Loading branch information
mikehaertl authored Feb 13, 2018
2 parents b9c2c4d + 2b92787 commit 06b9294
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions UrlManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ class UrlManager extends BaseUrlManager
*/
public $languageParam = 'language';

/**
* @var string the key in $_SERVER that contains the detected GeoIP country.
* Default is 'HTTP_X_GEO_COUNTRY' as used by mod_geoip in apache.
*/
public $geoIpServerVar = 'HTTP_X_GEO_COUNTRY';

/**
* @var array list of GeoIP countries indexed by corresponding language code
* e.g. 'ru' => ['RUS','AZE','ARM','BLR','KAZ','KGZ','MDA','TJK','TKM','UZB','UKR']
* will set app language to ru for listed countries.
* The default is an empty list which disables GeoIP detection.
*/
public $geoIpLanguageCountries = [];

/**
* @var \yii\web\Request
*/
Expand Down Expand Up @@ -380,6 +394,14 @@ protected function processLocaleUrl($normalized)
}
}
}
if ($language===null && isset($_SERVER[$this->geoIpServerVar])) {
foreach ($this->geoIpLanguageCountries as $key => $codes) {
if (in_array($_SERVER[$this->geoIpServerVar], $codes)) {
$language = $key;
break;
}
}
}
if ($language===null || $language===$this->_defaultLanguage) {
if (!$this->enableDefaultLanguageUrlCode) {
return;
Expand Down

0 comments on commit 06b9294

Please sign in to comment.