* @package MCMS\System */ class Context { protected $defaultLocale = 'en'; protected $localeoverride = false; /** * Set the default system locale setting to the context * * @param mixed $locale * a locale setting */ public function setDefaultLocale($locale) { $this->defaultLocale = $locale; } public function getDefaultLocale() { return $this->defaultLocale; } /** * Allows an additional locale with high priority from cookies or request params * * @param mixed $locale * a locale setting */ public function setLocaleOverride($locale) { $this->localeoverride = $locale; } public function getLocaleOverride() { return $this->localeoverride; } /** * * @return \MCMS\User\iUser the current user of the request context */ public function getCurrentUser() { return \MCMS\System::getInstance()->getUserSvc()->getUser(); } /** * Find the best locale setting in $haystack supported in the locale priority list * * @param array $haystack * a set of locales */ public function findBestLocale(array $haystack) { $default = $this->getDefaultLocale(); $browserlocales = Locale::getLocaleList($_SERVER['HTTP_ACCEPT_LANGUAGE']); $user = $this->getCurrentUser(); $locales = array_merge($browserlocales, ['0.0' => $default]); if(!empty($user->locale)) $locales = array_merge(['98.0' => $user->locale], $locales); if($this->localeoverride) $locales = array_merge(['99.0' => $this->localeoverride], $locales); $lang = $default; foreach($locales as $l) { if(in_array($l, $haystack)) { $lang = $l; break; } } return $lang; } }