當前位置: 首頁>>代碼示例>>PHP>>正文


PHP SimpleSAML_Utilities::getAcceptLanguage方法代碼示例

本文整理匯總了PHP中SimpleSAML_Utilities::getAcceptLanguage方法的典型用法代碼示例。如果您正苦於以下問題:PHP SimpleSAML_Utilities::getAcceptLanguage方法的具體用法?PHP SimpleSAML_Utilities::getAcceptLanguage怎麽用?PHP SimpleSAML_Utilities::getAcceptLanguage使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在SimpleSAML_Utilities的用法示例。


在下文中一共展示了SimpleSAML_Utilities::getAcceptLanguage方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: getHTTPLanguage

 /**
  * This function gets the prefered language for the user based on the Accept-Language http header.
  *
  * @return The prefered language based on the Accept-Language http header, or NULL if none of the
  *         languages in the header were available.
  */
 private function getHTTPLanguage()
 {
     $languageScore = SimpleSAML_Utilities::getAcceptLanguage();
     /* For now we only use the default language map. We may use a configurable language map
      * in the future.
      */
     $languageMap = self::$defaultLanguageMap;
     /* Find the available language with the best score. */
     $bestLanguage = NULL;
     $bestScore = -1.0;
     foreach ($languageScore as $language => $score) {
         /* Apply the language map to the language code. */
         if (array_key_exists($language, $languageMap)) {
             $language = $languageMap[$language];
         }
         if (!in_array($language, $this->availableLanguages, TRUE)) {
             /* Skip this language - we don't have it. */
             continue;
         }
         /* Some user agents use very limited precicion of the quality value, but order the
          * elements in descending order. Therefore we rely on the order of the output from
          * getAcceptLanguage() matching the order of the languages in the header when two
          * languages have the same quality.
          */
         if ($score > $bestScore) {
             $bestLanguage = $language;
             $bestScore = $score;
         }
     }
     return $bestLanguage;
 }
開發者ID:jerrcs,項目名稱:simplesamlphp,代碼行數:37,代碼來源:Template.php

示例2: guessBestLanguage

 /**
  * Guess the "best" language for a user. This should be called whenever a
  * decorated person object is availabe.
  * The "best" language is determined by the following order of steps:
  *
  * 1.) If there is already a language set (this->language) take that one.
  *     Thus the language settings can be functionaly overriden, e.g. in
  *     the framework.
  * 2.) The language stored in the cookie of the user dominates over everything else
  *     Thus, manually changing the language only means setting a cookie.
  * 3.) Try to take the language set by the subscriber, if the user is logged in
  * 4.) If the subscriber-language is NULL, take the language set by the NREN,
  *     if the user is logged in
  * 5.) If the user is not logged in and no session variable is set, take the
  *     first available language from the user's language accept-headers
  * 6.) If none of the languages in the user's accept header is available,
  *     take the default language of the Confusa instance (usually but not necessarily English)
  *
  * @param $person Person-oject (Decorated) Person, from the subscriber or
  *                             NREN of which translator can deduce the
  *                             best language
  * @return void
  */
 public function guessBestLanguage($person)
 {
     if ($this->languageOverridden) {
         return;
     }
     if (isset($_COOKIE['language'])) {
         $cookielang = Input::sanitizeLangCode($_COOKIE['language']);
         $this->language = $cookielang;
         return;
     }
     if ($person->isAuth()) {
         if (!is_null($person->getSubscriber())) {
             try {
                 $query = "SELECT lang FROM subscribers WHERE name=?";
                 $res = MDB2Wrapper::execute($query, array('text'), array($person->getSubscriber()->getIdPName()));
                 if (isset($res[0]['lang'])) {
                     setCookie('language', $res[0]['lang']);
                     $this->language = $res[0]['lang'];
                     return;
                 }
                 $query = "SELECT lang FROM nrens WHERE name=?";
                 $res = MDB2Wrapper::execute($query, array('text'), array($person->getNREN()));
                 if (isset($res[0]['lang'])) {
                     setCookie('language', $res[0]['lang']);
                     $this->language = $res[0]['lang'];
                     return;
                 }
             } catch (DBQueryException $dbqe) {
                 Logger::log_event(LOG_WARNING, "Could not query subscriber/NREN default language. " . "Falling back to system language default! " . $dbqe->getMessage());
             } catch (DBStatementException $dbse) {
                 Logger::log_event(LOG_WARNING, "Could not query subscriber/NREN default language. " . "Falling back to system default! " . $dbse->getMessage());
             }
         }
     }
     $sspdir = Config::get_config('simplesaml_path');
     /* turn off warnings to keep the page header tidy */
     $level = error_reporting(E_ERROR);
     /* poll the accept languages only, if we can load simplesamlphp
      * simplesamlphp *should* always be enabled (otherwise no authN :)),
      * But there can be configurations in bypass auth-mode without a working
      * simplesamlphp instance
      */
     if (file_exists($sspdir . "/lib/_autoload.php")) {
         require_once $sspdir . '/lib/_autoload.php';
         $accept_languages = SimpleSAML_Utilities::getAcceptLanguage();
         $available_languages = Config::get_config('language.available');
         if (empty($accept_languages)) {
             Logger::log_event(LOG_DEBUG, "Simplesamlphp instance seems to be not " . "configured, or not configured properly. Translator " . "will not use the browser's accept-header to determine " . "language settings.");
         }
         foreach ($accept_languages as $key => $value) {
             if (array_search($key, $available_languages) === FALSE) {
                 continue;
             } else {
                 $this->language = $key;
                 return;
             }
         }
     }
     /* turn on warnings again */
     error_reporting($level);
     $this->language = $this->defaultLanguage;
     return;
 }
開發者ID:henrikau,項目名稱:confusa,代碼行數:86,代碼來源:Translator.php


注:本文中的SimpleSAML_Utilities::getAcceptLanguage方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。