Example #1
0
 /**
  *
  * @param type $str
  */
 public function setLocale($str = "")
 {
     $lang = strtolower($str == "" ? $this->getParameter("lang") : $str);
     $langAvail = array_keys($this->LanguagesAvailable());
     // Get the languages to check
     if (!empty($str)) {
         $checkLang = array($str);
     } else {
         if (!empty($lang)) {
             $checkLang = array($lang);
         } else {
             $checkLang = $langAvail;
         }
     }
     // Language Negotiator
     $langNeg = new LanguageNegotiator();
     $langAccepted = $langNeg->getBest(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : "en-us", $checkLang);
     if (is_null($langAccepted)) {
         $currentLanguage = !empty($str) ? $str : (!empty($lang) ? $lang : $langAvail[0]);
     } else {
         $currentLanguage = strtolower($langAccepted->getValue());
     }
     // Get the correct language
     $selected = array_merge(preg_grep("/^" . $currentLanguage . "*/", $langAvail), preg_grep("/^" . substr($currentLanguage, 0, 2) . "*/", $langAvail));
     if (count($selected) == 0) {
         $currentLanguage = $langAvail[0];
     } else {
         $currentLanguage = array_shift($selected);
     }
     $this->_lang = LocaleFactory::GetLocale($currentLanguage);
     $this->addPairToConfig("LANGUAGE", $this->_lang->getName());
     $this->addPairToConfig("LANGUAGENAME", $this->_lang->getLanguage());
 }