當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。