src/Service/LocaleService.php line 80

  1. <?php
  2. namespace App\Service;
  3. use Symfony\Component\HttpFoundation\RequestStack;
  4. use Symfony\Component\Translation\LocaleSwitcher;
  5. use Symfony\Component\Intl;
  6. use Gedmo\Translatable\TranslatableListener;
  7. use Psr\Container\ContainerInterface as ParameterBag;
  8. class LocaleService
  9. {
  10.     private $_requestStack;
  11.     private $_localeSwitcher;
  12.     private $_parameterBag;
  13.     private $_translatableListener;
  14.     
  15.     public function __construct(
  16.             RequestStack $requestStack,
  17.             LocaleSwitcher $localeSwitcher,
  18.             ParameterBag $parameterBag,
  19.             TranslatableListener $translatableListener)
  20.     {
  21.         $this->_requestStack $requestStack;
  22.         $this->_localeSwitcher $localeSwitcher;
  23.         $this->_parameterBag $parameterBag;
  24.         $this->_translatableListener $translatableListener;
  25.     }
  26.     
  27.     private static $_contentLocales;
  28.     public function getAvailableLocales()
  29.     {
  30.         if (!self::$_contentLocales)
  31.         {
  32.             $contentLocales = array();
  33.             foreach($this->_parameterBag->get('content_locales') as $locale)
  34.             {
  35.                 $locale strtolower(trim($locale));
  36.                 if ($locale != '' && strlen($locale) == 2)
  37.                     $contentLocales[] = $locale;
  38.             }
  39.             $defaultLocale $this->getDefaultLocale();
  40.             $locales = array();
  41.             foreach($contentLocales as $locale)
  42.             {
  43.                 if ($locale == $defaultLocale)
  44.                     $locales array_merge(array($locale), $locales);
  45.                 else
  46.                     $locales[] = $locale;
  47.             }
  48.             
  49.             if (!$locales)
  50.                 throw new \Exception('No languages available.');
  51.             
  52.             self::$_contentLocales $locales;
  53.         }
  54.         return self::$_contentLocales;
  55.     }
  56.     
  57.     public function getDefaultLocale()
  58.     {
  59.         $locale $this->_parameterBag->get('default_locale');
  60.         
  61.         if ($locale == '')
  62.             throw new \Exception('No default language is set.');
  63.         
  64.         return $locale;        
  65.     }
  66.     
  67.     public function getLocale()
  68.     {
  69.         $request $this->_requestStack->getCurrentRequest();
  70.         
  71.         return $request->getSession()->get('_locale'$request->getLocale());
  72.     }
  73.     
  74.     public function setLocale($locale)
  75.     {
  76.         $availableLocales $this->getAvailableLocales();
  77.         
  78.         if (!in_array($locale$availableLocales))
  79.         {
  80.             $locale $this->getDefaultLocale();
  81.             
  82.             if (!in_array($locale$availableLocales))
  83.                 throw new \Exception('Default language does not exist.');
  84.         }
  85.         
  86.         $request $this->_requestStack->getCurrentRequest();
  87.         
  88.         $request->getSession()->set('_locale'$locale);
  89.         $request->setLocale($locale);
  90.         
  91.         $this->_translatableListener->setTranslatableLocale($locale);
  92.         $this->_localeSwitcher->setLocale($locale);
  93.     }
  94.     
  95.     public function getLocaleName($locale)
  96.     {
  97.         $locales Intl\Locales::getNames($this->getLocale());
  98.         
  99.         return isset($locales[$locale]) ? $locales[$locale] : '???';
  100.     }
  101. }