src/Service/LocaleService.php line 76

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use Symfony\Component\HttpFoundation\RequestStack;
  4. use Symfony\Component\Intl;
  5. use Gedmo\Translatable\TranslatableListener;
  6. use Psr\Container\ContainerInterface as ParameterBag;
  7. class LocaleService
  8. {
  9.     private $_requestStack;
  10.     private $_parameterBag;
  11.     private $_translatableListener;
  12.     
  13.     public function __construct(
  14.             RequestStack $requestStack,
  15.             ParameterBag $parameterBag,
  16.             TranslatableListener $translatableListener)
  17.     {
  18.         $this->_requestStack $requestStack;
  19.         $this->_parameterBag $parameterBag;
  20.         $this->_translatableListener $translatableListener;
  21.     }
  22.     
  23.     private static $_contentLocales;
  24.     public function getAvailableLocales()
  25.     {
  26.         if (!self::$_contentLocales)
  27.         {
  28.             $contentLocales = array();
  29.             foreach($this->_parameterBag->get('content_locales') as $locale)
  30.             {
  31.                 $locale strtolower(trim($locale));
  32.                 if ($locale != '' && strlen($locale) == 2)
  33.                     $contentLocales[] = $locale;
  34.             }
  35.             $defaultLocale $this->getDefaultLocale();
  36.             $locales = array();
  37.             foreach($contentLocales as $locale)
  38.             {
  39.                 if ($locale == $defaultLocale)
  40.                     $locales array_merge(array($locale), $locales);
  41.                 else
  42.                     $locales[] = $locale;
  43.             }
  44.             
  45.             if (!$locales)
  46.                 throw new \Exception('No languages available.');
  47.             
  48.             self::$_contentLocales $locales;
  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->getMainRequest();
  83.         
  84.         if ($request)
  85.         {
  86.             $request->getSession()->set('_locale'$locale);
  87.             $request->setLocale($locale);
  88.         }
  89.         $this->_translatableListener->setTranslatableLocale($locale);
  90.     }
  91.     
  92.     public function getLocaleName($locale)
  93.     {
  94.         $locales Intl\Locales::getNames($this->getLocale());
  95.         
  96.         return isset($locales[$locale]) ? $locales[$locale] : '???';
  97.     }
  98.     public function getCountryList($locale null)
  99.     {
  100.         return Intl\Countries::getNames($locale ?? $this->getLocale());
  101.     }
  102.     
  103.     public function getCountryName($code$locale null)
  104.     {
  105.         $code strtoupper($code);
  106.         $countryList $this->getCountryList($locale);
  107.         
  108.         return isset($countryList[$code]) ? $countryList[$code] : null;
  109.     }
  110. }