src/Admin/Service/LocaleService.php line 140

  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. 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.     public function getAvailableBackendLocales()
  28.     {
  29.         $locales $this->_parameterBag->get('backend_locales');
  30.         
  31.         if (!$locales)
  32.             throw new \Exception('No languages for the panel.');
  33.         
  34.         return $locales;
  35.     }
  36.     
  37.     public function getDefaultLocale()
  38.     {
  39.         return $this->_parameterBag->get('default_backend_locale');
  40.     }
  41.     public function getLocale()
  42.     {
  43.         $request $this->_requestStack->getMainRequest();
  44.         
  45.         return $request->getSession()->get('_backend_locale'$request->getLocale());
  46.     }
  47.     
  48.     public function setLocale($locale)
  49.     {
  50.         $availableLocales $this->getAvailableBackendLocales();
  51.         
  52.         if (!in_array($locale$availableLocales))
  53.         {
  54.             $locale $this->getDefaultLocale();
  55.             
  56.             if (!in_array($locale$availableLocales))
  57.                 throw new \Exception('Default language does not exist.');
  58.         }
  59.         
  60.         $request $this->_requestStack->getMainRequest();
  61.         $request->getSession()->set('_backend_locale'$locale);
  62.         $request->setLocale($locale);
  63.         
  64.         $this->_localeSwitcher->setLocale($locale);
  65.     }
  66.     private static $_contentLocales;
  67.     public function getAvailableContentLocales()
  68.     {
  69.         if (!self::$_contentLocales)
  70.         {
  71.             self::$_contentLocales = array();
  72.             foreach($this->_parameterBag->get('content_locales') as $locale)
  73.             {
  74.                 $locale strtolower(trim($locale));
  75.                 if ($locale != '' && strlen($locale) == 2)
  76.                     self::$_contentLocales[] = $locale;
  77.             }
  78.             $defaultLocale $this->getContentDefaultLocale();
  79.             $locales = array();
  80.             foreach(self::$_contentLocales as $locale)
  81.             {
  82.                 if ($locale == $defaultLocale)
  83.                     $locales array_merge(array($locale), $locales);
  84.                 else
  85.                     $locales[] = $locale;
  86.             }
  87.             if (!self::$_contentLocales)
  88.                 self::$_contentLocales = array($defaultLocale);
  89.         }
  90.         
  91.         return self::$_contentLocales;
  92.     }
  93.     
  94.     public function getContentDefaultLocale()
  95.     {
  96.         $locale $this->_parameterBag->get('default_content_locale');
  97.         
  98.         if ($locale == '')
  99.             throw new \Exception('No default language for the content.');
  100.         
  101.         return $locale;
  102.     }
  103.     
  104.     public function getContentLocale()
  105.     {
  106.         $request $this->_requestStack->getMainRequest();
  107.         $locale $request->getSession()->get('content_locale');
  108.         
  109.         if ($locale == '' || !in_array($locale$this->getAvailableContentLocales()))
  110.             $this->setContentDefaultLocale();
  111.         
  112.         return $request->getSession()->get('content_locale');
  113.     }
  114.     public function setContentLocale($locale)
  115.     {
  116.         $availableLocales $this->getAvailableContentLocales();
  117.         
  118.         if (!in_array($locale$availableLocales))
  119.         {
  120.             $locale $this->getContentDefaultLocale();
  121.             
  122.             if (!in_array($locale$availableLocales))
  123.                 throw new \Exception('Default language for content does not exist.');
  124.         }
  125.         
  126.         $request $this->_requestStack->getMainRequest();
  127.         $request->getSession()->set('content_locale'$locale);
  128.         $this->_translatableListener->setTranslatableLocale($locale);
  129.     }
  130.     
  131.     public function setContentDefaultLocale()
  132.     {
  133.         $this->setContentLocale($this->getContentDefaultLocale());
  134.     }
  135.     
  136.     public function getLocaleName($locale)
  137.     {
  138.         $locales Intl\Locales::getNames($this->getLocale());
  139.         
  140.         return isset($locales[$locale]) ? $locales[$locale] : '???';
  141.     }
  142.     public function getAllLocales($displayLocale null)
  143.     {
  144.         $locales Intl\Locales::getNames($displayLocale != '' $displayLocale $this->getLocale());
  145.         
  146.         foreach($locales as $code => $name)
  147.         {
  148.             if (strpos($code'_'))
  149.                 unset($locales[$code]);
  150.         }
  151.         
  152.         return $locales;
  153.     }
  154. }