当前位置: 首页>>代码示例>>PHP>>正文


PHP CakeRequest::acceptLanguage方法代码示例

本文整理汇总了PHP中CakeRequest::acceptLanguage方法的典型用法代码示例。如果您正苦于以下问题:PHP CakeRequest::acceptLanguage方法的具体用法?PHP CakeRequest::acceptLanguage怎么用?PHP CakeRequest::acceptLanguage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CakeRequest的用法示例。


在下文中一共展示了CakeRequest::acceptLanguage方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: autoDetectLocale

 /**
  * autoDetectLocale
  *
  * If a string or array of cancicates are provided  -loop over them
  * otherwise get the candiate locales from the accept-language header
  *
  * Loop over the possible locales, account for regional dialects and
  * set the currentrequest language to that locale, and return that value
  *
  * @param mixed $candidates
  * @return string matched language
  */
 public static function autoDetectLocale($candidates = null)
 {
     $locales = static::locales();
     if ($candidates) {
         if (is_string($candidates)) {
             $candidates = explode(',', $candidates);
         }
     } else {
         $candidates = CakeRequest::acceptLanguage();
     }
     $candidates = array_filter($candidates, function ($in) {
         return strpos($in, 'q=') === false;
     });
     $permutations = array();
     foreach ($candidates as $langKey) {
         if (strlen($langKey) === 5) {
             $permutations[] = substr($langKey, 0, 2) . '_' . strtoupper(substr($langKey, -2, 2));
         }
         $permutations[] = substr($langKey, 0, 2);
     }
     $permutations = array_unique($permutations);
     $match = false;
     foreach ($permutations as $langKey) {
         if (!empty($locales[$langKey])) {
             Configure::write('Config.language', $langKey);
             $match = $langKey;
             break;
         }
     }
     return $match;
 }
开发者ID:nodesagency,项目名称:Translations,代码行数:43,代码来源:Translation.php

示例2: _autoLanguage

 /**
  * Attempts to find the locale settings based on the HTTP_ACCEPT_LANGUAGE variable
  *
  * @return boolean Success
  */
 protected function _autoLanguage()
 {
     $_detectableLanguages = CakeRequest::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:kuradakis,项目名称:cakephp-ex,代码行数:23,代码来源:L10n.php

示例3: testAcceptLanguage

 /**
  * Test accept language
  *
  * @return void
  */
 public function testAcceptLanguage()
 {
     // Weird language
     $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca';
     $result = CakeRequest::acceptLanguage();
     $this->assertEquals(array('inexistent', 'en-ca'), $result, 'Languages do not match');
     // No qualifier
     $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx,en_ca';
     $result = CakeRequest::acceptLanguage();
     $this->assertEquals(array('es-mx', 'en-ca'), $result, 'Languages do not match');
     // With qualifier
     $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4';
     $result = CakeRequest::acceptLanguage();
     $this->assertEquals(array('en-us', 'en', 'pt-br', 'pt'), $result, 'Languages do not match');
     // With spaces
     $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'da, en-gb;q=0.8, en;q=0.7';
     $result = CakeRequest::acceptLanguage();
     $this->assertEquals(array('da', 'en-gb', 'en'), $result, 'Languages do not match');
     // Checking if requested
     $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx,en_ca';
     $result = CakeRequest::acceptLanguage();
     $result = CakeRequest::acceptLanguage('en-ca');
     $this->assertTrue($result);
     $result = CakeRequest::acceptLanguage('en-CA');
     $this->assertTrue($result);
     $result = CakeRequest::acceptLanguage('en-us');
     $this->assertFalse($result);
     $result = CakeRequest::acceptLanguage('en-US');
     $this->assertFalse($result);
 }
开发者ID:xMyThoLoGyx,项目名称:centremedicaletp3,代码行数:35,代码来源:CakeRequestTest.php

示例4: testAcceptLanguage

 /**
  * test accept language
  *
  * @return void
  */
 public function testAcceptLanguage()
 {
     $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca';
     $result = CakeRequest::acceptLanguage();
     $this->assertEquals(array('inexistent', 'en-ca'), $result, 'Languages do not match');
     $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx;en_ca';
     $result = CakeRequest::acceptLanguage();
     $this->assertEquals(array('es-mx', 'en-ca'), $result, 'Languages do not match');
     $result = CakeRequest::acceptLanguage('en-ca');
     $this->assertTrue($result);
     $result = CakeRequest::acceptLanguage('en-us');
     $this->assertFalse($result);
 }
开发者ID:nabeelio,项目名称:CakePHP-Base,代码行数:18,代码来源:CakeRequestTest.php


注:本文中的CakeRequest::acceptLanguage方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。