Beispiel #1
0
 /**
  * @return string
  */
 public function getPathRegex()
 {
     $regex = $this->_path;
     if ($this->hasPathData() > 0) {
         $data = $this->getPathData();
         $replaceMatches = function ($match) use($data) {
             if (isset($data[$match[1]])) {
                 if ($match[1] === self::DATA_LANG && $data[$match[1]] === true) {
                     return '(?<' . $match[1] . '>(' . implode('|', Language::getLanguages()) . '))';
                 }
                 return '(?<' . $match[1] . '>' . $data[$match[1]] . ')';
             }
             return $match[1];
         };
         $regex = preg_replace_callback('/{(\\w+)}/', $replaceMatches, $regex);
         if (isset($data[self::DATA_LANG]) && $data[self::DATA_LANG] === true && $this->_pathDefaultLang !== null) {
             $regex = '(?:' . $this->_pathDefaultLang . '|' . $regex . ')';
         }
     }
     return $regex;
 }
Beispiel #2
0
 /**
  * @param array $cookie PHP $_COOKIE variable
  * @param string|boolean $fallback (optional)
  */
 public function handleDictionary($cookie, $fallback = false)
 {
     if (count(Language::getLanguages()) > 0) {
         $languageCookieIndex = 'plinth-language';
         $languageCookieAble = false;
         $languageCode = null;
         $this->_lang = Language::getDefault();
         //Get browser language, Accept-Language overrules default language
         if ($this->getSetting('localeaccept') && isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && count($this->config->get('language:locales')) > 0) {
             $regex = '/' . implode('|', $this->config->get('language:locales')) . '/';
             $languageViaAccept = preg_match_all($regex, $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches);
             if ($languageViaAccept > 0) {
                 $languageCode = $matches[0][0];
             }
         }
         //Get get language, Get-Language overrules Accept-Language
         if ($this->getSetting('localeget') !== false) {
             $languageViaGet = $this->getRequest()->get($this->getSetting('localeget'));
             if ($languageViaGet !== null) {
                 $languageCode = $languageViaGet;
                 $languageCookieAble = true;
             }
         }
         //Get route language, Route-Language overrules Get-Language
         if ($this->getRouter()->hasRoute()) {
             $languageViaRequest = $this->getRouter()->getRoute()->get(Route::DATA_LANG);
             if ($languageViaRequest !== false) {
                 $languageCode = $languageViaRequest;
                 $languageCookieAble = true;
             }
         }
         if ($languageCode !== null) {
             $this->_lang = Language::validate($languageCode);
             if ($this->getSetting('localecookie') && $languageCookieAble) {
                 //Save language cookie if it doesn't exist or if it's different from the previously saved one
                 if (!isset($cookie[$languageCookieIndex]) || isset($cookie[$languageCookieIndex]) && $cookie[$languageCookieIndex] !== $this->_lang) {
                     $cookie[$languageCookieIndex] = $this->_lang;
                     $this->getResponse()->saveCookie($languageCookieIndex, $this->_lang);
                 }
             }
         }
         if ($this->getSetting('localecookie') && isset($cookie[$languageCookieIndex]) && Language::validate($cookie[$languageCookieIndex]) && $cookie[$languageCookieIndex] !== $this->_lang) {
             $this->_lang = $cookie[$languageCookieIndex];
         }
         $this->getDict()->loadLanguage($this->_lang, $this->getSetting('localetype'));
         if ($fallback !== false) {
             if (Language::validate($fallback) === $fallback) {
                 $this->getDict()->loadLanguage($fallback, $this->getSetting('localetype'), true);
             } else {
                 throw new PlinthException("Your fallback locale, {$fallback}, doesn't exist");
             }
         }
     }
 }