src/Admin/Service/LocaleService.php line 112

  1. <?php
  2. namespace App\Admin\Service;
  3. use Symfony\Component\HttpFoundation\RequestStack;
  4. use Symfony\Component\Translation\LocaleSwitcher;
  5. use Symfony\Component\Intl;
  6. use Psr\Container\ContainerInterface as ParameterBag;
  7. use Gedmo\Translatable\TranslatableListener;
  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.     
  17.     public function __construct(
  18.             RequestStack $requestStack,
  19.             LocaleSwitcher $localeSwitcher,
  20.             ParameterBag $parameterBag,
  21.             TranslatableListener $translatableListener,
  22.             SettingsService $settingsService)
  23.     {
  24.         $this->_requestStack $requestStack;
  25.         $this->_localeSwitcher $localeSwitcher;
  26.         $this->_parameterBag $parameterBag;
  27.         $this->_translatableListener $translatableListener;
  28.         $this->_settingsService $settingsService;
  29.     }
  30.     
  31.     public function getAvailableBackendLocales()
  32.     {
  33.         $locales $this->_parameterBag->get('backend_locales');
  34.         
  35.         if (!$locales)
  36.             throw new \Exception('No languages for the panel.');
  37.         
  38.         return $locales;
  39.     }
  40.     
  41.     public function getDefaultLocale()
  42.     {
  43.         return $this->_parameterBag->get('default_backend_locale');
  44.     }
  45.     public function getLocale()
  46.     {
  47.         $request $this->_requestStack->getMainRequest();
  48.         
  49.         return $request->getSession()->get('_backend_locale'$request->getLocale());
  50.     }
  51.     
  52.     public function setLocale($locale)
  53.     {
  54.         $availableLocales $this->getAvailableBackendLocales();
  55.         
  56.         if (!in_array($locale$availableLocales))
  57.         {
  58.             $locale $this->getDefaultLocale();
  59.             
  60.             if (!in_array($locale$availableLocales))
  61.                 throw new \Exception('Default language does not exist.');
  62.         }
  63.         
  64.         $request $this->_requestStack->getMainRequest();
  65.         $request->getSession()->set('_backend_locale'$locale);
  66.         $request->setLocale($locale);
  67.         
  68.         $this->_localeSwitcher->setLocale($locale);
  69.     }
  70.     private static $_contentLocales;
  71.     public function getAvailableContentLocales()
  72.     {
  73.         if (!self::$_contentLocales)
  74.         {
  75.             self::$_contentLocales = array();
  76.             $langs $this->_settingsService->getValue(SettingsService::APP_ADDITIONAL_LANGSfalse);
  77.             if ($langs != '')
  78.             {
  79.                 $langs json_decode($langstrue);
  80.                 if ($langs)
  81.                     self::$_contentLocales $langs;
  82.             }
  83.             self::$_contentLocales array_unique(array_merge([$this->getContentDefaultLocale()], self::$_contentLocales));
  84.         }
  85.         
  86.         return self::$_contentLocales;
  87.     }
  88.     
  89.     public function getContentDefaultLocale()
  90.     {
  91.         $locale $this->_parameterBag->get('default_content_locale');
  92.         
  93.         if ($locale == '')
  94.             throw new \Exception('No default language for the content.');
  95.         
  96.         return $locale;
  97.     }
  98.     
  99.     public function getContentLocale()
  100.     {
  101.         $request $this->_requestStack->getMainRequest();
  102.         $locale $request->getSession()->get('content_locale');
  103.         
  104.         if ($locale == '' || !in_array($locale$this->getAvailableContentLocales()))
  105.             $this->setContentDefaultLocale();
  106.         
  107.         return $request->getSession()->get('content_locale');
  108.     }
  109.     public function setContentLocale($locale)
  110.     {
  111.         $availableLocales $this->getAvailableContentLocales();
  112.         
  113.         if (!in_array($locale$availableLocales))
  114.         {
  115.             $locale $this->getContentDefaultLocale();
  116.             
  117.             if (!in_array($locale$availableLocales))
  118.                 throw new \Exception('Default language for content does not exist.');
  119.         }
  120.         
  121.         $request $this->_requestStack->getMainRequest();
  122.         $request->getSession()->set('content_locale'$locale);
  123.         $this->_translatableListener->setTranslatableLocale($locale);
  124.     }
  125.     
  126.     public function setContentDefaultLocale()
  127.     {
  128.         $this->setContentLocale($this->getContentDefaultLocale());
  129.     }
  130.     
  131.     public function getLocaleName($locale)
  132.     {
  133.         $locales $this->getAllLocales();
  134.         return isset($locales[$locale]) ? $locales[$locale] : '???';
  135.     }
  136.     private static $_allLocales = array();
  137.     public function getAllLocales($displayLocale null)
  138.     {
  139.         $displayLocale $displayLocale != '' $displayLocale $this->getLocale();
  140.         if (!isset(self::$_allLocales[$displayLocale]))
  141.         {
  142.             self::$_allLocales[$displayLocale] = Intl\Locales::getNames($displayLocale);
  143.         
  144.             foreach(self::$_allLocales[$displayLocale] as $code => $name)
  145.             {
  146.                 if (strpos($code'_'))
  147.                     unset(self::$_allLocales[$displayLocale][$code]);
  148.             }
  149.         }
  150.         
  151.         return self::$_allLocales[$displayLocale];
  152.     }
  153. }