getApplication()->attachEventHandler( 'OnAuthenticationComplete', [$this, 'setUserCulture'] ); } public function setUserCulture() { $culture = $this->_getUserCulture($this->getUser()) ?: $this->_autodetectCulture(); Locale::setDefault($culture); $this->setCulture($culture); } protected function _getUserCulture(DbUser $user) { if (!$user->IsGuest) { $preference = UserFacade::getInstance()->getLanguagePreference($user); if ($preference) { $preference = $preference->Name; } if (!in_array($preference, $this->AllowedCultures)) { $preference = $this->_getNeutralCulture($this->DefaultCulture); } return $preference; } return NULL; } protected function _autodetectCulture() { $culture = $this->_getNeutralCulture($this->DefaultCulture); $http = new HTTPNegotiator(); foreach ($http->getLanguages() as $language) { $language = $this->_getNeutralCulture($language); if (in_array($language, $this->AllowedCultures)) { $culture = $language; break; } } return $culture; } private function _getNeutralCulture(string $culture) { return explode('_', $culture)[0]; } public function getCulture() { return $this->_getNeutralCulture(parent::getCulture()); } public function setCulture($culture) { parent::setCulture($this->_getNeutralCulture($culture)); } protected $_allowedCultures = []; public function setAllowedCultures(string $cultures) { $this->_allowedCultures = array_map('trim', explode(',', $cultures)); } public function getAllowedCultures() { return $this->_allowedCultures; } } ?>