コード例 #1
0
 function it_returns_master_request_locale_code(RequestStack $requestStack, LocaleProviderInterface $localeProvider, Request $request)
 {
     $requestStack->getMasterRequest()->willReturn($request);
     $request->attributes = new ParameterBag(['_locale' => 'pl_PL']);
     $localeProvider->getAvailableLocalesCodes()->willReturn(['pl_PL', 'de_DE']);
     $this->getLocaleCode()->shouldReturn('pl_PL');
 }
コード例 #2
0
 function it_throws_an_exception_if_locale_taken_from_storage_is_not_available(ChannelContextInterface $channelContext, LocaleStorageInterface $localeStorage, LocaleProviderInterface $localeProvider, ChannelInterface $channel)
 {
     $channelContext->getChannel()->willReturn($channel);
     $localeStorage->get($channel)->willReturn('pl_PL');
     $localeProvider->getAvailableLocalesCodes()->willReturn(['en_US', 'en_UK']);
     $this->shouldThrow(LocaleNotFoundException::class)->during('getLocaleCode');
 }
コード例 #3
0
 /**
  * @param string $name
  *
  * @return string
  */
 protected function getPropertyName($name)
 {
     if (in_array($name, $this->translatableFields, true)) {
         return 'translations.' . $this->localeProvider->getDefaultLocaleCode() . '.' . $name;
     }
     return $name;
 }
コード例 #4
0
 function it_builds_form(LocaleProviderInterface $localeProvider, FormBuilderInterface $builder)
 {
     $localeProvider->getDefinedLocalesCodes()->willReturn(['pl_PL', 'en_EN', 'en_GB']);
     $localeProvider->getDefaultLocaleCode()->willReturn('en_EN');
     $builder->addEventSubscriber(Argument::type(ResourceTranslationsSubscriber::class))->shouldBeCalled()->willReturn($builder);
     $this->buildForm($builder, ['type' => 'text']);
 }
コード例 #5
0
 /**
  * @param string $locale
  * @param array $options
  *
  * @return bool
  */
 private function isLocaleRequired($locale, array $options = [])
 {
     if (isset($options['required_locales'])) {
         return in_array($locale, $options['required_locales']);
     }
     return $this->localeProvider->getDefaultLocaleCode() === $locale;
 }
コード例 #6
0
 function it_creates_translatable_and_sets_locales(FactoryInterface $factory, LocaleProviderInterface $localeProvider, TranslatableInterface $resource)
 {
     $localeProvider->getDefaultLocaleCode()->willReturn('pl_PL');
     $factory->createNew()->willReturn($resource);
     $resource->setCurrentLocale('pl_PL')->shouldBeCalled();
     $resource->setFallbackLocale('pl_PL')->shouldBeCalled();
     $this->createNew()->shouldReturn($resource);
 }
コード例 #7
0
 /**
  * @param Request $request
  * @param string $code
  *
  * @return Response
  */
 public function switchAction(Request $request, $code)
 {
     if (!in_array($code, $this->localeProvider->getAvailableLocalesCodes())) {
         throw new HttpException(Response::HTTP_NOT_ACCEPTABLE, sprintf('The locale code "%s" is invalid.', $code));
     }
     $this->localeChangeHandler->handle($code);
     return new RedirectResponse($request->headers->get('referer', $request->getBaseUrl()));
 }
コード例 #8
0
 /**
  * {@inheritdoc}
  */
 public function getLocaleCode()
 {
     $availableLocalesCodes = $this->localeProvider->getAvailableLocalesCodes();
     $localeCode = $this->localeProvider->getDefaultLocaleCode();
     if (!in_array($localeCode, $availableLocalesCodes, true)) {
         throw LocaleNotFoundException::notAvailable($localeCode, $availableLocalesCodes);
     }
     return $localeCode;
 }
コード例 #9
0
 function it_sets_locale_and_default_locale_on_request(LocaleContextInterface $localeContext, LocaleProviderInterface $localeProvider, GetResponseEvent $event, Request $request)
 {
     $event->getRequest()->willReturn($request);
     $localeContext->getLocaleCode()->willReturn('pl_PL');
     $localeProvider->getDefaultLocaleCode()->willReturn('en_US');
     $request->setLocale('pl_PL')->shouldBeCalled();
     $request->setDefaultLocale('en_US')->shouldBeCalled();
     $this->onKernelRequest($event);
 }
コード例 #10
0
 function it_sets_default_locale_on_request_even_if_locale_context_fails(LocaleContextInterface $localeContext, LocaleProviderInterface $localeProvider, GetResponseEvent $event, Request $request)
 {
     $event->getRequest()->willReturn($request);
     $localeContext->getLocaleCode()->willThrow(LocaleNotFoundException::class);
     $localeProvider->getDefaultLocaleCode()->willReturn('en_US');
     $request->setLocale(Argument::any())->shouldNotBeCalled();
     $request->setDefaultLocale('en_US')->shouldBeCalled();
     $this->onKernelRequest($event);
 }
コード例 #11
0
ファイル: LocaleMenuBuilder.php プロジェクト: bcremer/Sylius
 /**
  * Builds frontend locale menu.
  *
  * @return ItemInterface
  */
 public function createMenu()
 {
     $menu = $this->factory->createItem('root', array('childrenAttributes' => array('class' => 'nav nav-pills')));
     foreach ($this->localeProvider->getAvailableLocales() as $locale) {
         $code = $locale->getCode();
         $menu->addChild($code, array('route' => 'sylius_locale_change', 'routeParameters' => array('locale' => $code)))->setLabel(Intl::getLocaleBundle()->getLocaleName($code));
     }
     return $menu;
 }
コード例 #12
0
 /**
  * {@inheritdoc}
  *
  * @throws UnexpectedTypeException
  */
 public function createNew()
 {
     $resource = $this->factory->createNew();
     if (!$resource instanceof TranslatableInterface) {
         throw new UnexpectedTypeException($resource, TranslatableInterface::class);
     }
     $resource->setCurrentLocale($this->localeProvider->getDefaultLocaleCode());
     $resource->setFallbackLocale($this->localeProvider->getDefaultLocaleCode());
     return $resource;
 }
コード例 #13
0
 /**
  * @param GetResponseEvent $event
  */
 public function onKernelRequest(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     try {
         $request->setLocale($this->localeContext->getLocaleCode());
     } catch (LocaleNotFoundException $exception) {
     }
     try {
         $request->setDefaultLocale($this->localeProvider->getDefaultLocaleCode());
     } catch (LocaleNotFoundException $exception) {
     }
 }
コード例 #14
0
 /**
  * {@inheritdoc}
  */
 public function getLocaleCode()
 {
     $availableLocalesCodes = $this->localeProvider->getAvailableLocalesCodes();
     try {
         $localeCode = $this->localeStorage->get($this->channelContext->getChannel());
     } catch (ChannelNotFoundException $exception) {
         throw new LocaleNotFoundException(null, $exception);
     }
     if (!in_array($localeCode, $availableLocalesCodes, true)) {
         throw LocaleNotFoundException::notAvailable($localeCode, $availableLocalesCodes);
     }
     return $localeCode;
 }
コード例 #15
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $locales = $this->localeProvider->getAvailableLocalesCodes();
     $localesWithRequirement = [];
     foreach ($locales as $locale) {
         $localesWithRequirement[$locale] = false;
         if ($this->localeProvider->getDefaultLocaleCode() === $locale) {
             $localesWithRequirement[$locale] = true;
             $localesWithRequirement = array_reverse($localesWithRequirement, true);
         }
     }
     $builder->addEventSubscriber(new ResourceTranslationsSubscriber($localesWithRequirement));
 }
コード例 #16
0
 /**
  * Builds frontend locale menu.
  *
  * @return ItemInterface
  */
 public function createMenu()
 {
     $locales = $this->localeProvider->getAvailableLocalesCodes();
     $menu = $this->factory->createItem('root', ['childrenAttributes' => ['class' => 'nav nav-pills']]);
     if (1 === count($locales)) {
         $menu->setDisplay(false);
         return $menu;
     }
     foreach ($locales as $locale) {
         $menu->addChild($locale, ['route' => 'sylius_locale_change', 'routeParameters' => ['locale' => $locale]])->setLabel(Intl::getLocaleBundle()->getLocaleName($locale));
     }
     return $menu;
 }
コード例 #17
0
 /**
  * {@inheritdoc}
  */
 public function getLocaleCode()
 {
     $request = $this->requestStack->getMasterRequest();
     if (null === $request) {
         throw new LocaleNotFoundException('No master request available.');
     }
     $localeCode = $request->attributes->get('_locale');
     if (null === $localeCode) {
         throw new LocaleNotFoundException('No locale attribute is set on the master request.');
     }
     $availableLocalesCodes = $this->localeProvider->getAvailableLocalesCodes();
     if (!in_array($localeCode, $availableLocalesCodes, true)) {
         throw LocaleNotFoundException::notAvailable($localeCode, $availableLocalesCodes);
     }
     return $localeCode;
 }
コード例 #18
0
 /**
  * @param LifecycleEventArgs $args
  */
 public function postLoad(LifecycleEventArgs $args)
 {
     $entity = $args->getEntity();
     if (!$entity instanceof TranslatableInterface) {
         return;
     }
     try {
         $entity->setCurrentLocale($this->localeContext->getLocaleCode());
     } catch (LocaleNotFoundException $exception) {
         $entity->setCurrentLocale($this->localeProvider->getDefaultLocaleCode());
     }
     $entity->setFallbackLocale($this->localeProvider->getDefaultLocaleCode());
 }
コード例 #19
0
 /**
  * {@inheritdoc}
  */
 public function getLocales()
 {
     return $this->syliusLocaleProvider->getAvailableLocales();
 }
コード例 #20
0
 function it_returns_list_of_locales(LocaleProviderInterface $syliusLocaleProvider)
 {
     $listOfLocales = array('en_US', 'pl_PL');
     $syliusLocaleProvider->getAvailableLocales()->shouldBeCalled()->willReturn($listOfLocales);
     $this->getLocales()->shouldReturn($listOfLocales);
 }
コード例 #21
0
 /**
  * @return Response
  */
 public function renderSelectorAction()
 {
     return $this->templatingEngine->renderResponse('SyliusShopBundle:Locale:selector.html.twig', ['activeLocaleCode' => $this->localeContext->getLocaleCode(), 'availableLocalesCodes' => $this->localeProvider->getAvailableLocalesCodes()]);
 }
コード例 #22
0
ファイル: RequestLocaleSetter.php プロジェクト: sylius/sylius
 /**
  * @param GetResponseEvent $event
  *
  * @throws LocaleNotFoundException
  */
 public function onKernelRequest(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     $request->setLocale($this->localeContext->getLocaleCode());
     $request->setDefaultLocale($this->localeProvider->getDefaultLocaleCode());
 }
コード例 #23
0
 function it_throws_a_locale_not_found_exception_if_default_locale_is_not_available(LocaleProviderInterface $localeProvider)
 {
     $localeProvider->getAvailableLocalesCodes()->willReturn(['es_ES', 'en_US']);
     $localeProvider->getDefaultLocaleCode()->willReturn('pl_PL');
     $this->shouldThrow(LocaleNotFoundException::class)->during('getLocaleCode');
 }