Esempio n. 1
0
 /**
  * Check language choice tree in the following order:
  * - First: Construct override
  * - Second: Parameter override
  * - Third: Saved cookie
  * - Fourth: Preferences from Accept-Language header
  * - Fifth: A language which is a prefix for one of the
  * Accept-Language preferences.
  * - Sixth: English (default stays)
  *
  * @private
  *
  * @return true
  */
 private function initLangSelect($option)
 {
     $set = false;
     if (isset($option) && !empty($option)) {
         $set = $this->setLang($option);
     }
     if (!$set && $this->getUseRequestParam() === true && isset($_GET[$this->paramNames['userlang']])) {
         $set = $this->setLang($_GET[$this->paramNames['userlang']]);
     }
     if (!$set && isset($_COOKIE[$this->cookieNames['userlang']])) {
         $set = $this->setLang($_COOKIE[$this->cookieNames['userlang']]);
     }
     if (!$set) {
         $acceptableLanguages = TsIntuitionUtil::GetAcceptableLanguages();
         foreach ($acceptableLanguages as $acceptLang => $qVal) {
             if ($acceptLang === '*') {
                 /**
                  * We pick the first available language which is not in $acceptableLanguages.
                  * The special * range matches every tag not matched by any other range.
                  * Other language codes in $acceptableLanguages will either have a lower q-value,
                  * or be missing from availableLanguages.
                  * The order will be the one in the i18n file: en, af, ar...
                  */
                 foreach ($this->availableLanguages as $availableLang => $true) {
                     if (!isset($acceptableLanguages[$availableLang])) {
                         $n = strstr($availableLang, '-');
                         // Assumption: We won't have translations for languages with more than
                         // 1 dashe on the language tag
                         if ($n !== false && !isset($acceptableLanguages[substr($availableLang, 0, $n - 1)])) {
                             // if we have non-Chinese translations, zh-hans should not be
                             // picked for "fr,*;q=0.3,zh;q=0.1".
                             continue;
                         }
                         $set = $this->setLang($availableLang);
                         break;
                     }
                 }
                 if ($set) {
                     break;
                 }
             } elseif (isset($this->availableLanguages[$acceptLang])) {
                 $set = $this->setLang($acceptLang);
                 break;
             }
         }
     }
     /* From this point on, we are choosing amongst languages with a $qVal of 0 */
     if (!$set) {
         /**
          * Some browsers show (apparently by default) only a tag,
          * such as "ru-RU", "fr-FR" or "es-mx".
          * This is broken behavior! The browser should be providing
          * appropriate guidance.
          * Providing only a full tag is doing a disservice.
          * See RFC 2616 section 1.4 - http://tools.ietf.org/html/rfc2616#page-105
          */
         foreach ($acceptableLanguages as $acceptLang => $qVal) {
             if (!$qVal) {
                 continue;
             }
             while (($n = strstr($acceptLang, '-')) !== false) {
                 $acceptLang = substr($acceptLang, 0, $n - 1);
                 if (isset($this->availableLanguages[$acceptLang])) {
                     $set = $this->setLang($acceptLang);
                     break 2;
                 }
             }
         }
     }
     if (!$set) {
         $set = $this->setLang('en');
     }
     return $set;
 }