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


PHP Request::acceptLanguage方法代碼示例

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


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

示例1: _autoLanguage

 /**
  * Attempts to find the locale settings based on the HTTP_ACCEPT_LANGUAGE variable
  *
  * @return bool Success
  */
 protected function _autoLanguage()
 {
     $_detectableLanguages = $this->_request->acceptLanguage();
     foreach ($_detectableLanguages as $langKey) {
         if (isset($this->_l10nCatalog[$langKey])) {
             $this->_setLanguage($langKey);
             return true;
         }
         if (strpos($langKey, '-') !== false) {
             $langKey = substr($langKey, 0, 2);
             if (isset($this->_l10nCatalog[$langKey])) {
                 $this->_setLanguage($langKey);
                 return true;
             }
         }
     }
     return false;
 }
開發者ID:ripzappa0924,項目名稱:carte0.0.1,代碼行數:23,代碼來源:L10n.php

示例2: testAcceptLanguage

 /**
  * Test accept language
  *
  * @return void
  */
 public function testAcceptLanguage()
 {
     $request = new Request();
     // Weird language
     $request->env('HTTP_ACCEPT_LANGUAGE', 'inexistent,en-ca');
     $result = $request->acceptLanguage();
     $this->assertEquals(['inexistent', 'en-ca'], $result, 'Languages do not match');
     // No qualifier
     $request->env('HTTP_ACCEPT_LANGUAGE', 'es_mx,en_ca');
     $result = $request->acceptLanguage();
     $this->assertEquals(['es-mx', 'en-ca'], $result, 'Languages do not match');
     // With qualifier
     $request->env('HTTP_ACCEPT_LANGUAGE', 'en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4');
     $result = $request->acceptLanguage();
     $this->assertEquals(['en-us', 'en', 'pt-br', 'pt'], $result, 'Languages do not match');
     // With spaces
     $request->env('HTTP_ACCEPT_LANGUAGE', 'da, en-gb;q=0.8, en;q=0.7');
     $result = $request->acceptLanguage();
     $this->assertEquals(['da', 'en-gb', 'en'], $result, 'Languages do not match');
     // Checking if requested
     $request->env('HTTP_ACCEPT_LANGUAGE', 'es_mx,en_ca');
     $result = $request->acceptLanguage();
     $result = $request->acceptLanguage('en-ca');
     $this->assertTrue($result);
     $result = $request->acceptLanguage('en-CA');
     $this->assertTrue($result);
     $result = $request->acceptLanguage('en-us');
     $this->assertFalse($result);
     $result = $request->acceptLanguage('en-US');
     $this->assertFalse($result);
 }
開發者ID:alexunique0519,項目名稱:Blog_Cakephp_association,代碼行數:36,代碼來源:RequestTest.php

示例3: detectLanguage

 /**
  * Get languages accepted by browser and return the one matching one of
  * those in config var `I18n.languages`.
  *
  * You should set config var `I18n.languages` to an array containing
  * languages available in your app.
  *
  * @param \Psr\Http\Message\ServerRequestInterface $request The request.
  * @param string|null $default Default language to return if no match is found.
  *
  * @return string
  */
 public function detectLanguage(ServerRequestInterface $request, $default = null)
 {
     if (empty($default)) {
         $lang = $this->_config['defaultLanguage'];
     } else {
         $lang = $default;
     }
     $cakeRequest = new Request();
     $browserLangs = $cakeRequest->acceptLanguage();
     foreach ($browserLangs as $k => $langKey) {
         if (strpos($langKey, '-') !== false) {
             $browserLangs[$k] = substr($langKey, 0, 2);
         }
     }
     $acceptedLangs = array_intersect($browserLangs, array_keys($this->_config['languages']));
     if (!empty($acceptedLangs)) {
         $lang = reset($acceptedLangs);
     }
     return $lang;
 }
開發者ID:admad,項目名稱:cakephp-i18n,代碼行數:32,代碼來源:I18nMiddleware.php


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