- Notifications
You must be signed in to change notification settings - Fork 112
detect language by apache mod_geoip country code #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -134,6 +134,18 @@ class UrlManager extends BaseUrlManager | |
| */ | ||
| public $languageParam = 'language'; | ||
| | ||
| /** | ||
| * @var string $_SERVER key set by apache mod_geoip | ||
| */ | ||
| public $geoipServerFieldName = 'HTTP_X_GEO_COUNTRY'; | ||
| | ||
| /** | ||
| * @var array list of countries for given language code | ||
| ||
| * e.g. 'ru' => ['RUS','AZE','ARM','BLR','KAZ','KGZ','MDA','TJK','TKM','UZB','UKR'] | ||
| * will set app language to ru for countries listed above | ||
| */ | ||
| public $geoipLanguageCountries = []; | ||
| ||
| | ||
| /** | ||
| * @var \yii\web\Request | ||
| */ | ||
| | @@ -370,6 +382,14 @@ protected function processLocaleUrl($normalized) | |
| if ($this->enableLanguagePersistence) { | ||
| $language = $this->loadPersistedLanguage(); | ||
| } | ||
| if ($language===null && isset($_SERVER[$this->geoipServerFieldName]) && !empty($this->geoipLanguageCountries)) { | ||
| ||
| foreach ($this->geoipLanguageCountries as $key => $codes) { | ||
| if (in_array($_SERVER[$this->geoipServerFieldName], $codes)) { | ||
| $language = $key; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| ||
| if ($language===null && $this->enableLanguageDetection) { | ||
| foreach ($this->_request->getAcceptableLanguages() as $acceptable) { | ||
| list($language,$country) = $this->matchCode($acceptable); | ||
| | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a better name is
$geoIpServerVarbecause most entries in$_SERVERare environment variables set by the webserver. We should also mention the default value in the description: