src/Admin/Service/LocaleService.php line 65
<?phpnamespace App\Admin\Service;use Symfony\Component\HttpFoundation\RequestStack;use Symfony\Component\Translation\LocaleSwitcher;use Symfony\Component\Intl;use Psr\Container\ContainerInterface as ParameterBag;use Gedmo\Translatable\TranslatableListener;class LocaleService{private $_requestStack;private $_localeSwitcher;private $_parameterBag;private $_translatableListener;public function __construct(RequestStack $requestStack,LocaleSwitcher $localeSwitcher,ParameterBag $parameterBag,TranslatableListener $translatableListener){$this->_requestStack = $requestStack;$this->_localeSwitcher = $localeSwitcher;$this->_parameterBag = $parameterBag;$this->_translatableListener = $translatableListener;}public function getAvailableBackendLocales(){$locales = $this->_parameterBag->get('backend_locales');if (!$locales)throw new \Exception('No languages for the panel.');return $locales;}public function getDefaultLocale(){return $this->_parameterBag->get('default_backend_locale');}public function getLocale(){$request = $this->_requestStack->getMainRequest();return $request->getSession()->get('_backend_locale', $request->getLocale());}public function setLocale($locale){$availableLocales = $this->getAvailableBackendLocales();if (!in_array($locale, $availableLocales)){$locale = $this->getDefaultLocale();if (!in_array($locale, $availableLocales))throw new \Exception('Default language does not exist.');}$request = $this->_requestStack->getMainRequest();$request->getSession()->set('_backend_locale', $locale);$request->setLocale($locale);$this->_localeSwitcher->setLocale($locale);}private static $_contentLocales;public function getAvailableContentLocales(){if (!self::$_contentLocales){self::$_contentLocales = array();foreach($this->_parameterBag->get('content_locales') as $locale){$locale = strtolower(trim($locale));if ($locale != '' && strlen($locale) == 2)self::$_contentLocales[] = $locale;}$defaultLocale = $this->getContentDefaultLocale();$locales = array();foreach(self::$_contentLocales as $locale){if ($locale == $defaultLocale)$locales = array_merge(array($locale), $locales);else$locales[] = $locale;}if (!self::$_contentLocales)self::$_contentLocales = array($defaultLocale);}return self::$_contentLocales;}public function getContentDefaultLocale(){$locale = $this->_parameterBag->get('default_content_locale');if ($locale == '')throw new \Exception('No default language for the content.');return $locale;}public function getContentLocale(){$request = $this->_requestStack->getMainRequest();$locale = $request->getSession()->get('content_locale');if ($locale == '' || !in_array($locale, $this->getAvailableContentLocales()))$this->setContentDefaultLocale();return $request->getSession()->get('content_locale');}public function setContentLocale($locale){$availableLocales = $this->getAvailableContentLocales();if (!in_array($locale, $availableLocales)){$locale = $this->getContentDefaultLocale();if (!in_array($locale, $availableLocales))throw new \Exception('Default language for content does not exist.');}$request = $this->_requestStack->getMainRequest();$request->getSession()->set('content_locale', $locale);$this->_translatableListener->setTranslatableLocale($locale);}public function setContentDefaultLocale(){$this->setContentLocale($this->getContentDefaultLocale());}public function getLocaleName($locale){$locales = Intl\Locales::getNames($this->getLocale());return isset($locales[$locale]) ? $locales[$locale] : '???';}public function getAllLocales($displayLocale = null){$locales = Intl\Locales::getNames($displayLocale != '' ? $displayLocale : $this->getLocale());foreach($locales as $code => $name){if (strpos($code, '_'))unset($locales[$code]);}return $locales;}}