src/Admin/Service/LocaleService.php line 117
<?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;use App\Service\SettingsService;class LocaleService{private $_requestStack;private $_localeSwitcher;private $_parameterBag;private $_translatableListener;private $_settingsService;public function __construct(RequestStack $requestStack,LocaleSwitcher $localeSwitcher,ParameterBag $parameterBag,TranslatableListener $translatableListener,SettingsService $settingsService){$this->_requestStack = $requestStack;$this->_localeSwitcher = $localeSwitcher;$this->_parameterBag = $parameterBag;$this->_translatableListener = $translatableListener;$this->_settingsService = $settingsService;}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();$langs = $this->_settingsService->getValue(SettingsService::APP_ADDITIONAL_LANGS, false);if ($langs != ''){$langs = json_decode($langs, true);if ($langs)self::$_contentLocales = $langs;}self::$_contentLocales = array_unique(array_merge([$this->getContentDefaultLocale()], self::$_contentLocales));}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 = $this->getAllLocales();return isset($locales[$locale]) ? $locales[$locale] : '???';}private static $_allLocales = array();public function getAllLocales($displayLocale = null){$displayLocale = $displayLocale != '' ? $displayLocale : $this->getLocale();if (!isset(self::$_allLocales[$displayLocale])){self::$_allLocales[$displayLocale] = Intl\Locales::getNames($displayLocale);foreach(self::$_allLocales[$displayLocale] as $code => $name){if (strpos($code, '_'))unset(self::$_allLocales[$displayLocale][$code]);}}return self::$_allLocales[$displayLocale];}}