当前位置: 首页>>代码示例>>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;未经允许,请勿转载。