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


PHP Request::getPreferredLanguage方法代码示例

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


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

示例1: switchLocaleForRequest

 /**
  * Chooses a language for a request
  *
  * @return $response or null
  **/
 public function switchLocaleForRequest(Request $request)
 {
     $session = $request->getSession();
     $parts = explode('.', $request->getHost());
     if (count($parts) === 3) {
         $locale = $parts[0];
         if ($locale == $session->getLocale()) {
             return;
         }
         if ($this->manager->isAvailable($locale)) {
             $session->setLocale($locale);
             $preferred = $request->getPreferredLanguage($this->manager->getAvailableLanguageCodes());
             if ($preferred != $locale) {
                 $session->setFlash('locale_change_adjust', $preferred);
             } else {
                 $session->setFlash('locale_change_contribute', $locale);
             }
             return;
         }
         $host = $parts[1] . '.' . $parts[2];
     } else {
         $host = $parts[0] . '.' . $parts[1];
     }
     $locale = $request->getPreferredLanguage($this->manager->getAvailableLanguageCodes());
     $url = sprintf('http://%s.%s%s', $locale, $host, $request->getRequestUri());
     $response = new RedirectResponse($url);
     $preferredLanguage = $this->getRequestPreferredLanguage($request);
     if ($preferredLanguage && $locale != $preferredLanguage) {
         $allLanguageCodes = array_keys($this->manager->getLanguages());
         if (in_array($preferredLanguage, $allLanguageCodes)) {
             $session->setFlash('locale_missing', $preferredLanguage);
         }
     }
     return $response;
 }
开发者ID:hafeez3000,项目名称:lichess,代码行数:40,代码来源:Switcher.php

示例2: guessLocale

 /**
  * Guess the locale based on the browser settings
  *
  * @param Request $request
  *
  * @return boolean
  */
 public function guessLocale(Request $request)
 {
     $validator = $this->metaValidator;
     // Get the preferred locale from the Browser.
     $preferredLocale = $request->getPreferredLanguage();
     $availableLocales = $request->getLanguages();
     if (!$preferredLocale or count($availableLocales) === 0) {
         return false;
     }
     // If the preferred primary locale is allowed, return the locale.
     if ($validator->isAllowed($preferredLocale)) {
         $this->identifiedLocale = $preferredLocale;
         return true;
     }
     // Get a list of available and allowed locales and return the first result
     $matchLocale = function ($v) use($validator) {
         return $validator->isAllowed($v);
     };
     $result = array_values(array_filter($availableLocales, $matchLocale));
     if (!empty($result)) {
         $this->identifiedLocale = $result[0];
         return true;
     }
     return false;
 }
开发者ID:Sententiaregum,项目名称:LocaleBundle,代码行数:32,代码来源:BrowserLocaleGuesser.php

示例3: defaultLanguageAction

 /**
  * action for / to redirect to the best language based on the request language order
  */
 public function defaultLanguageAction(Request $request, $contentDocument)
 {
     if (!$contentDocument instanceof RouteAwareInterface) {
         throw new \Exception('The route passed to the language selection action must emulate content to have the correct route generated.');
     }
     // TODO: use lunetics/LocaleBundle https://github.com/symfony-cmf/cmf-sandbox/issues/54
     $defaultPreferredLangs = $this->chooser->getDefaultLocalesOrder();
     $bestLang = $request->getPreferredLanguage($defaultPreferredLangs);
     // we only care about the first 2 characters, even if the user's preference is de_CH.
     $bestLang = substr($bestLang, 0, 2);
     /*
      * Let the router generate the route for the requested language. The
      * route provides its children, which should be the urls for each locale
      * as content.
      */
     $routeParams = $request->query->all();
     // do not lose eventual get parameters
     $routeParams['_locale'] = $bestLang;
     // and set the locale
     $routeParams['content'] = $contentDocument;
     // and the content for the router
     $url = $this->router->generate('', $routeParams, true);
     /* Note: I wanted to send a 300 "Multiple Choices" header along with a
      * Location header, but user agents may behave inconsistently in
      * response to this.
      *
      * For example Chrome was not redirecting unless the headers were
      * carefully tailored for it. (In particular, it doesn't like the
      * lowercase 'location' header that results from calling
      * $response->headers->set('Location', '...')
      */
     $response = new RedirectResponse($url, 301);
     $response->setVary('accept-language');
     return $response;
 }
开发者ID:richardmiller,项目名称:symfony-cmf,代码行数:38,代码来源:LanguageSelectorController.php

示例4: getLocale

 /**
  * Returns the locale from the request, the session or the HTTP header.
  *
  * @param Request $request
  *
  * @return string
  */
 private function getLocale(Request $request)
 {
     if (null !== $request->attributes->get('_locale')) {
         return $this->formatLocaleId($request->attributes->get('_locale'));
     }
     if (null !== ($session = $request->getSession()) && $session->has('_locale')) {
         return $session->get('_locale');
     }
     return $request->getPreferredLanguage($this->availableLocales);
 }
开发者ID:contao,项目名称:core-bundle,代码行数:17,代码来源:LocaleListener.php

示例5: mapAction

 /**
  * @Route("/map")
  */
 public function mapAction(Request $request)
 {
     $request->isXmlHttpRequest();
     // is it an Ajax request?
     $request->getPreferredLanguage(array('en', 'fr'));
     $request->query->get('page');
     // get a $_GET parameter
     $request->request->get('page');
     // get a $_POST parameter
     return $this->redirect('http://maps.google.com');
 }
开发者ID:WChoy,项目名称:symfony_book,代码行数:14,代码来源:HelloController.php

示例6: requestAction

 public function requestAction(Request $request)
 {
     $page = $request->query->get('page', 1);
     $session = $request->getSession();
     $session->set('foo', 'bar');
     $session->get('foo');
     $request->isXmlHttpRequest();
     // is it an Ajax request?
     $request->getPreferredLanguage(array('en', 'fr'));
     $request->query->get('page');
     // get a $_GET parameter
     $request->request->get('page');
     // get a $_POST parameter
 }
开发者ID:psosulski,项目名称:meta,代码行数:14,代码来源:MetaController.php

示例7: indexAction

 /**
  * @Route("/app/example", name="homepage")
  */
 public function indexAction(Request $request)
 {
     // is it an Ajax request?
     $isAjax = $request->isXmlHttpRequest();
     // what's the preferred language of the user?
     $language = $request->getPreferredLanguage(array('en', 'fr'));
     // get the value of a $_GET parameter
     $pageName = $request->query->get('page');
     // get the value of a $_POST parameter
     $pageName = $request->request->get('page');
     // store a message for the very next request
     $this->addFlash('notice', 'Congratulations, your action succeeded!');
     return $this->render('default/index.html.twig');
 }
开发者ID:jujyfruits,项目名称:taskor,代码行数:17,代码来源:DefaultController.php

示例8: defaultLanguageAction

 /**
  * action for / to redirect to the best language based on the request language order
  */
 public function defaultLanguageAction(Request $request)
 {
     $defaultPreferredLangs = $this->chooser->getPreferredLanguages($request->getLocale());
     $bestLang = $request->getPreferredLanguage($defaultPreferredLangs);
     // we only care about the first 2 characters, even if the user's preference is de_CH.
     $bestLang = substr($bestLang, 0, 2);
     /* Note: I wanted to send a 300 "Multiple Choices" header along with a
      * Location header, but user agents may behave inconsistently in
      * repsonse to this.
      *
      * For example Chrome was not redirecting unless the headers were
      * carefully tailored for it. (In particular, it doesn't like the
      * lowercase 'location' header that results from calling
      * $response->headers->set('Location', '...')
      */
     $url = $this->router->generate($this->routename, array('_locale' => $bestLang, '/'), true);
     $response = new RedirectResponse($url, 301);
     $response->setVary('accept-language');
     return $response;
 }
开发者ID:ronnylt,项目名称:symfony-cmf,代码行数:23,代码来源:LanguageSelectorController.php

示例9: indexLangRedirectAction

 /**
  * Redirects from base path to localized one depending on client preferred language
  * @Route("/")
  */
 public function indexLangRedirectAction(Request $request)
 {
     $lang = $request->getPreferredLanguage($this->supportedLanguages);
     return $this->redirect($this->generateUrl('homepage', ['_locale' => $lang]));
 }
开发者ID:chteuchteu,项目名称:Apache-Fallback-Page,代码行数:9,代码来源:DefaultController.php

示例10: getPreferredSite

 /**
  * Gets the preferred site based on the given request
  *
  * @param array   $sites   An array of enabled sites
  * @param Request $request A request instance
  *
  * @return SiteInterface|null
  */
 protected function getPreferredSite(array $sites, Request $request)
 {
     if (count($sites) === 0) {
         return null;
     }
     $sitesLocales = array_map(function (SiteInterface $site) {
         return $site->getLocale();
     }, $sites);
     $language = $request->getPreferredLanguage($sitesLocales);
     $host = $request->getHost();
     foreach ($sites as $site) {
         if (in_array($site->getHost(), array('localhost', $host)) && $language === $site->getLocale()) {
             return $site;
         }
     }
     return reset($sites);
 }
开发者ID:saberyounis,项目名称:Sonata-Project,代码行数:25,代码来源:BaseSiteSelector.php

示例11: searchInHeaders

 /**
  * Searches a valid locale in HTTP headers.
  *
  * @param Request $request the request to search in
  *
  * @return string found locale
  */
 protected function searchInHeaders(Request $request)
 {
     if (empty($this->locales)) {
         return $request->getPreferredLanguage();
     }
     return $request->getPreferredLanguage($this->locales);
 }
开发者ID:alexandresalome,项目名称:web-bundle,代码行数:14,代码来源:LocaleListener.php

示例12: negotiate

 /**
  * @param Request $request
  */
 public function negotiate(Request $request)
 {
     return $request->getPreferredLanguage();
 }
开发者ID:gointegro,项目名称:hateoas,代码行数:7,代码来源:DefaultLocaleNegotiator.php

示例13: requestObjectAction

 /**
  * The Request Object
  *
  * @Route("/lesson5/request", name="request_object")
  *
  * @param Request $request
  * @return Response
  */
 public function requestObjectAction(Request $request)
 {
     $request->isXmlHttpRequest();
     // is it an Ajax request?
     $request->getPreferredLanguage(array('en', 'fr'));
     $request->query->get('page');
     // get a $_GET parameter
     $request->request->get('page');
     // get a $_POST parameter
     return new Response('Hello world!');
 }
开发者ID:binhnguyenhuu,项目名称:SymfonyLesson,代码行数:19,代码来源:DefaultController.php

示例14: detect

 public function detect(Request $request)
 {
     return $request->getPreferredLanguage();
 }
开发者ID:sitesupra,项目名称:sitesupra,代码行数:4,代码来源:AcceptLanguageHeaderDetector.php

示例15: getUserLocale

 /**
  * This method returns the user locale and store it in session, if there is no user this method return default
  * language or the browser language if it is present in translations.
  *
  * @param \Symfony\Component\HttpFoundation\Request $request
  *
  * @return string The locale string as en, fr, es, etc.
  */
 public function getUserLocale(Request $request)
 {
     $locales = $this->getAvailableLocales();
     $preferred = explode('_', $request->getPreferredLanguage());
     if ($request->attributes->get('_locale')) {
         $locale = $request->attributes->get('_locale');
     } elseif (($user = $this->getCurrentUser()) && $user->getLocale()) {
         $locale = $user->getLocale();
     } elseif ($request->getSession() && ($sessionLocale = $request->getSession()->get('_locale'))) {
         $locale = $sessionLocale;
     } elseif (count($preferred) > 0 && isset($locales[$preferred[0]])) {
         $locale = $preferred[0];
     } else {
         $locale = $this->defaultLocale;
     }
     if ($session = $request->getSession()) {
         $session->set('_locale', $locale);
     }
     return $locale;
 }
开发者ID:claroline,项目名称:distribution,代码行数:28,代码来源:LocaleManager.php


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