src/Service/LocaleService.php line 93

  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. use App\Service\SettingsService;
  9. class LocaleService
  10. {
  11.     private $_requestStack;
  12.     private $_localeSwitcher;
  13.     private $_parameterBag;
  14.     private $_translatableListener;
  15.     private $_settingsService;
  16.     public function __construct(
  17.             RequestStack $requestStack,
  18.             LocaleSwitcher $localeSwitcher,
  19.             ParameterBag $parameterBag,
  20.             TranslatableListener $translatableListener,
  21.             SettingsService $settingsService)
  22.     {
  23.         $this->_requestStack $requestStack;
  24.         $this->_localeSwitcher $localeSwitcher;
  25.         $this->_parameterBag $parameterBag;
  26.         $this->_translatableListener $translatableListener;
  27.         $this->_settingsService $settingsService;
  28.     }
  29.     
  30.     private static $_contentLocales;
  31.     public function getAvailableLocales()
  32.     {
  33.         if (!self::$_contentLocales)
  34.         {
  35.             self::$_contentLocales = array();
  36.             $langs $this->_settingsService->getValue(SettingsService::APP_ADDITIONAL_LANGSfalse);
  37.             if ($langs != '')
  38.             {
  39.                 $langs json_decode($langstrue);
  40.                 if ($langs)
  41.                     self::$_contentLocales $langs;
  42.             }
  43.             $defaultContentLocale $this->_parameterBag->get('default_content_locale');
  44.         
  45.             if ($defaultContentLocale == '')
  46.                 throw new \Exception('No default language is set.');            
  47.             self::$_contentLocales array_unique(array_merge([$defaultContentLocale], self::$_contentLocales));
  48.         }
  49.         
  50.         return self::$_contentLocales;
  51.     }
  52.     
  53.     public function getDefaultLocale()
  54.     {
  55.         $locale $this->_parameterBag->get('default_locale');
  56.         
  57.         if ($locale == '')
  58.             throw new \Exception('No default language is set.');
  59.         
  60.         return $locale;        
  61.     }
  62.     
  63.     public function getLocale()
  64.     {
  65.         $request $this->_requestStack->getCurrentRequest();
  66.         
  67.         return $request->getSession()->get('_locale'$request->getLocale());
  68.     }
  69.     
  70.     public function setLocale($locale)
  71.     {
  72.         $availableLocales $this->getAvailableLocales();
  73.         
  74.         if (!in_array($locale$availableLocales))
  75.         {
  76.             $locale $this->getDefaultLocale();
  77.             
  78.             if (!in_array($locale$availableLocales))
  79.                 throw new \Exception('Default language does not exist.');
  80.         }
  81.         
  82.         $request $this->_requestStack->getCurrentRequest();
  83.         
  84.         $request->getSession()->set('_locale'$locale);
  85.         $request->setLocale($locale);
  86.         
  87.         $this->_translatableListener->setTranslatableLocale($locale);
  88.         $this->_localeSwitcher->setLocale($locale);
  89.     }
  90.     
  91.     private static $_localeNames = array();
  92.     public function getLocaleName($locale)
  93.     {
  94.         $_locale $this->getLocale();
  95.         if (!isset(self::$_localeNames[$_locale]))
  96.             self::$_localeNames[$_locale] = Intl\Locales::getNames($_locale);
  97.         
  98.         return isset(self::$_localeNames[$_locale][$locale]) ? self::$_localeNames[$_locale][$locale] : '???';
  99.     }
  100. }