src/Cms/FrontendBundle/Service/Resolvers/LocaleResolver.php line 90

Open in your IDE?
  1. <?php
  2. namespace Cms\FrontendBundle\Service\Resolvers;
  3. use App\Util\Locales;
  4. use Cms\CoreBundle\Util\DateTimeUtils;
  5. use Cms\CoreBundle\Util\Doctrine\EntityManager;
  6. use Cms\FrontendBundle\Service\ResolverManager;
  7. use Cms\TenantBundle\Model\SimpleTenantableInterface;
  8. use DateTimeZone;
  9. use Products\NotificationsBundle\Entity\NotificationsConfig;
  10. use Products\NotificationsBundle\Entity\Profile;
  11. use Products\NotificationsBundle\Service\Notifications\NotificationsConfigService;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. final class LocaleResolver extends AbstractResolver
  15. {
  16.     private NotificationsConfigService $notificationsConfigService;
  17.     private TokenStorageInterface $tokenStorage;
  18.     private RequestStack $request;
  19.     /**
  20.      * {@inheritDoc}
  21.      * @param NotificationsConfigService $notificationsConfigService
  22.      * @param TokenStorageInterface $tokenStorage
  23.      * @param RequestStack $request
  24.      */
  25.     public function __construct(
  26.         ResolverManager $rm,
  27.         EntityManager $em,
  28.         NotificationsConfigService $notificationsConfigService,
  29.         TokenStorageInterface $tokenStorage,
  30.         RequestStack $request
  31.     )
  32.     {
  33.         parent::__construct($rm$em);
  34.         $this->notificationsConfigService $notificationsConfigService;
  35.         $this->tokenStorage $tokenStorage;
  36.         $this->request $request;
  37.     }
  38.     /**
  39.      * @param SimpleTenantableInterface $tenantable
  40.      * @return DateTimeZone|null
  41.      */
  42.     public function resolveTimezoneByTenant(SimpleTenantableInterface $tenantable): ?DateTimeZone
  43.     {
  44.         // attempt to get the timezone
  45.         $timezone $tenantable->getTenant()->getLocale()->getTimezone();
  46.         // if we did not find anything, fallback to utc
  47.         if ( ! $timezone) {
  48.             $timezone DateTimeUtils::ZONES__UTC;
  49.         }
  50.         // use datetime helper to convert into a timezone
  51.         // we do this to prevent making a bunch of extra objects
  52.         return DateTimeUtils::timezone($timezone);
  53.     }
  54.     /**
  55.      * NOTE: this is really just done for future use in the event we allow better multi-lingual UIs and such...
  56.      *
  57.      * @param SimpleTenantableInterface $tenantable
  58.      * @return string
  59.      */
  60.     public function resolvePrimaryLocaleByTenant(SimpleTenantableInterface $tenantable): string
  61.     {
  62.         return Locales::RFC4646__ENGLISH__UNITED_STATES;
  63.     }
  64.     /**
  65.      * @param SimpleTenantableInterface $tenantable
  66.      * @return array
  67.      */
  68.     public function resolveSecondaryLocalesByTenant(SimpleTenantableInterface $tenantable): array
  69.     {
  70.         $notificationsConfig $this->notificationsConfigService->getNotificationsConfig(
  71.             $tenantable,
  72.         );
  73.         return ($notificationsConfig instanceof NotificationsConfig) ? $notificationsConfig->getTranslationLocales() : [];
  74.     }
  75.     /**
  76.      * @param SimpleTenantableInterface $tenantable
  77.      * @return array
  78.      */
  79.     public function resolveSecondaryLanguagesByTenant(SimpleTenantableInterface $tenantable): array
  80.     {
  81.         $locales $this->resolveSecondaryLocalesByTenant($tenantable);
  82.         if (empty($locales)) {
  83.             return [];
  84.         }
  85.         $languages = [];
  86.         foreach ($locales as $locale) {
  87.             $language Locales::languageForLocale($locale);
  88.             $languages[$language] = locale_get_display_language($language$language);
  89.         }
  90.         asort($languages);
  91.         return $languages;
  92.     }
  93.     /**
  94.      * @param SimpleTenantableInterface $tenantable
  95.      * @return string
  96.      */
  97.     public function resolveUserEffectiveLanguage(SimpleTenantableInterface $tenantable): string
  98.     {
  99.         $user $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
  100.         if ($user instanceof Profile) {
  101.             return $user->getEffectiveLanguage();
  102.         }
  103.         $request $this->request->getCurrentRequest();
  104.         $googleCookie $request->cookies->get('googtrans');
  105.         if ( ! empty($googleCookie)) {
  106.             return explode('/'$googleCookie)[2] ?? Locales::ISO6391__ENGLISH;
  107.         }
  108.         $preferredLanguage $request->getPreferredLanguage();
  109.         if ($preferredLanguage !== null) {
  110.             $languageCode explode('_'$preferredLanguage)[0];
  111.             $supportedLanguagesCodes array_keys($this->resolveSecondaryLanguagesByTenant($tenantable));
  112.             return in_array($languageCode$supportedLanguagesCodes) ? $languageCode Locales::ISO6391__ENGLISH;
  113.         }
  114.         return Locales::ISO6391__ENGLISH;
  115.     }
  116. }