vendor/ezsystems/ezplatform-user/src/lib/UserSetting/Setting/Language.php line 19

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. declare(strict_types=1);
  7. namespace EzSystems\EzPlatformUser\UserSetting\Setting;
  8. use eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface;
  9. use EzSystems\EzPlatformUser\Form\ChoiceList\Loader\AvailableLocaleChoiceLoader;
  10. use EzSystems\EzPlatformUser\UserSetting\FormMapperInterface;
  11. use EzSystems\EzPlatformUser\UserSetting\ValueDefinitionInterface;
  12. use Symfony\Component\Form\Extension\Core\Type\LocaleType;
  13. use Symfony\Component\Form\FormBuilderInterface;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. class Language implements ValueDefinitionInterfaceFormMapperInterface
  16. {
  17.     /** @var \Symfony\Contracts\Translation\TranslatorInterface */
  18.     private $translator;
  19.     /** @var \eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface */
  20.     private $userLanguagePreferenceProvider;
  21.     /** @var \EzSystems\EzPlatformUser\Form\ChoiceList\Loader\AvailableLocaleChoiceLoader */
  22.     private $availableLocaleChoiceLoader;
  23.     /**
  24.      * @param \Symfony\Contracts\Translation\TranslatorInterface $translator
  25.      * @param \eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface $userLanguagePreferenceProvider
  26.      * @param \EzSystems\EzPlatformUser\Form\ChoiceList\Loader\AvailableLocaleChoiceLoader $availableLocaleChoiceLoader
  27.      */
  28.     public function __construct(
  29.         TranslatorInterface $translator,
  30.         UserLanguagePreferenceProviderInterface $userLanguagePreferenceProvider,
  31.         AvailableLocaleChoiceLoader $availableLocaleChoiceLoader
  32.     ) {
  33.         $this->translator $translator;
  34.         $this->userLanguagePreferenceProvider $userLanguagePreferenceProvider;
  35.         $this->availableLocaleChoiceLoader $availableLocaleChoiceLoader;
  36.     }
  37.     /**
  38.      * {@inheritdoc}
  39.      */
  40.     public function getName(): string
  41.     {
  42.         return $this->getTranslatedName();
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      */
  47.     public function getDescription(): string
  48.     {
  49.         return $this->getTranslatedDescription();
  50.     }
  51.     /**
  52.      * {@inheritdoc}
  53.      */
  54.     public function getDisplayValue(string $storageValue): string
  55.     {
  56.         return $storageValue;
  57.     }
  58.     /**
  59.      * {@inheritdoc}
  60.      */
  61.     public function getDefaultValue(): string
  62.     {
  63.         $preferredLocales $this->userLanguagePreferenceProvider->getPreferredLocales();
  64.         return reset($preferredLocales);
  65.     }
  66.     /**
  67.      * {@inheritdoc}
  68.      */
  69.     public function mapFieldForm(FormBuilderInterface $formBuilderValueDefinitionInterface $value): FormBuilderInterface
  70.     {
  71.         return $formBuilder->create(
  72.             'value',
  73.             LocaleType::class,
  74.             [
  75.                 'required' => true,
  76.                 'label' => $this->getTranslatedDescription(),
  77.                 'choice_loader' => $this->availableLocaleChoiceLoader,
  78.             ]
  79.         );
  80.     }
  81.     /**
  82.      * @return string
  83.      */
  84.     private function getTranslatedName(): string
  85.     {
  86.         return $this->translator->trans(
  87.             /** @Desc("Language") */
  88.             'settings.language.value.title',
  89.             [],
  90.             'user_settings'
  91.         );
  92.     }
  93.     /**
  94.      * @return string
  95.      */
  96.     private function getTranslatedDescription(): string
  97.     {
  98.         return $this->translator->trans(
  99.             /** @Desc("Back Office language") */
  100.             'settings.language.value.description',
  101.             [],
  102.             'user_settings'
  103.         );
  104.     }
  105. }