Symfony\Component\HttpFoundation\Request::getLanguages PHP Method

getLanguages() public method

Gets a list of languages acceptable by the client browser.
public getLanguages ( ) : array
return array Languages ordered in the user browser preferences
    public function getLanguages()
    {
        if (null !== $this->languages) {
            return $this->languages;
        }

        $languages = AcceptHeader::fromString($this->headers->get('Accept-Language'))->all();
        $this->languages = array();
        foreach ($languages as $lang => $acceptHeaderItem) {
            if (false !== strpos($lang, '-')) {
                $codes = explode('-', $lang);
                if ('i' === $codes[0]) {
                    // Language not listed in ISO 639 that are not variants
                    // of any listed language, which can be registered with the
                    // i-prefix, such as i-cherokee
                    if (count($codes) > 1) {
                        $lang = $codes[1];
                    }
                } else {
                    for ($i = 0, $max = count($codes); $i < $max; ++$i) {
                        if ($i === 0) {
                            $lang = strtolower($codes[0]);
                        } else {
                            $lang .= '_'.strtoupper($codes[$i]);
                        }
                    }
                }
            }

            $this->languages[] = $lang;
        }

        return $this->languages;
    }

Usage Example

Example #1
0
 /**
  * Action for locale switch
  *
  * @param \Symfony\Component\HttpFoundation\Request $request
  * @param                                           $_locale The locale to set
  *
  * @return \Symfony\Bundle\FrameworkBundle\Controller\RedirectResponse
  *
  * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  */
 public function switchAction(Request $request, $_locale)
 {
     // Check if the Language is allowed
     if (!in_array(\Locale::getPrimaryLanguage($_locale), $this->allowedLanguages)) {
         throw new NotFoundHttpException('This language is not available');
     }
     // tries to detect a Region from the user-provided locales
     $providedLanguages = $request->getLanguages();
     $locales = array();
     foreach ($providedLanguages as $locale) {
         if (strpos($locale . '_', $_locale) !== false && strlen($locale) > 2) {
             $locales[] = $locale;
         }
     }
     if (count($locales) > 0) {
         $this->session->set('localeIdentified', $locales[0]);
     } else {
         $this->session->set('localeIdentified', $_locale);
     }
     // Add the listener
     $this->session->set('setLocaleCookie', true);
     // Redirect the User
     if ($request->headers->has('referer') && true === $this->useReferrer) {
         return new RedirectResponse($request->headers->get('referer'));
     }
     if (null !== $this->redirectToRoute) {
         return new RedirectResponse($this->router->generate($this->redirectToRoute));
     }
     return new RedirectResponse($request->getScheme() . '://' . $request->getHttpHost() . $this->redirectToUrl);
 }
All Usage Examples Of Symfony\Component\HttpFoundation\Request::getLanguages