/** * @param GetResponseEvent $event */ public function onKernelRequest(GetResponseEvent $event) { if ($this->session->has('_locale')) { $event->getRequest()->setLocale($this->session->get('_locale')); $this->translator->setLocale($this->session->get('_locale')); } }
/** * Set up the tests. */ public function setUp() { parent::setUp(); $this->translator = $this->app['translator']; $this->detector = new LanguageDetector($this->translator); $this->translator->setLocale('fr'); }
/** * @param HttpRequest $request * @param string $locale */ public function setLocale(HttpRequest $request, $locale) { setlocale(LC_ALL, $locale); $locale = substr($locale, 0, 2); $request->setLocale($locale); $this->translator->setLocale($locale); $this->translatable->setTranslatableLocale($locale); }
private function initialize() { if ($this->initialized || $this->requestAnalyzer->getCurrentLocalization() === null) { return; } $this->translator->setLocale($this->requestAnalyzer->getCurrentLocalization()->getLocale(Localization::LCID)); $this->initialized = true; }
public function sendEmail($type, $emailAddress, $arguments, $locale = 'en') { $this->eventDispatcher->dispatch(EmailEvent::BEFORE_SEND, new EmailEvent($type, $emailAddress, $arguments)); $this->translator->setLocale($locale); $message = $this->constructEmailMessage($type, $emailAddress, $arguments); $status = $this->emailService->send($message); $this->dispatchEmailSendingEvent($type, $arguments, $message, $status); return $status; }
/** * @param GenericEvent $event */ public function onPostUpdate(GenericEvent $event) { $user = $event->getSubject(); if ($user === $event->getArgument('current_user')) { $request = $this->requestStack->getMasterRequest(); $request->getSession()->set('_locale', $user->getUiLocale()->getCode()); $this->translator->setLocale($user->getUiLocale()->getCode()); } }
/** * onKernelRequest * * @param GetResponseEvent $event */ public function onKernelRequest(GetResponseEvent $event) { $url = $event->getRequest()->getRequestUri(); $token = $this->context->getToken(); if ($token && $this->isAdminToken($this->providerKey, $token) && $this->isAdminRoute($url)) { $locale = $token->getUser()->getAdminLocale(); if (!$locale) { $locale = $this->defaultAdminLocale; } $this->translator->setLocale($locale); } }
/** * Set new locale. * * @param string $locale * @throws LanguageNotFoundException */ public function setLocale($locale) { if (!in_array($locale, $this->getAvailableLanguages())) { throw new LanguageNotFoundException("Language {$locale} not found."); } $this->translator->setLocale($locale); }
public function setRequestLocale(GetResponseEvent $event) { $token = $this->tokenStorage->getToken(); if (!$token) { return; } /** @var Identity $identity */ $identity = $token->getUser(); $request = $event->getRequest(); $request->setLocale($identity->preferredLocale); // As per \Symfony\Component\HttpKernel\EventListener\TranslatorListener::setLocale() try { $this->translator->setLocale($request->getLocale()); } catch (\InvalidArgumentException $e) { $this->translator->setLocale($request->getDefaultLocale()); } }
public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); if (!$this->isApiRequest($request)) { return; } // request language if ($request->headers->has('Language')) { $this->translator->setLocale($request->headers->get('Language')); $request->setLocale($request->headers->get('Language')); } // request content type if ($type = $request->getContentType()) { switch ($type) { case 'json': $request->setRequestFormat('json'); break; default: $mime = $request->headers->get('Content-Type'); throw new HttpException(406, "The content type: \"{$type}\" specified as mime \"{$mime}\" - is not supported."); } } else { // default format is JSON $request->setRequestFormat('json'); } // request accept content type, currently only JSON $accepts = $request->getAcceptableContentTypes(); $types = array_filter(array_unique(array_map([$request, 'getFormat'], $accepts))); if ($types && !in_array('json', $types, true)) { $acceptable = implode(',', $accepts); throw new HttpException(406, "None of acceptable content types: {$acceptable} are supported."); } // if there is a body, decode it currently as JSON only if ($content = $request->getContent()) { $data = @json_decode($content, true); if (null === $data) { // the error may be important, log it if (null !== $this->logger) { $this->logger->error("Failed to parse json request content, err: " . json_last_error_msg()); } throw new HttpException(400, "The given content is not a valid json."); } $request->request = new ParameterBag($data); } }
/** * @param $fromEmail * @param BatchEntryMail $batchMail * @param $doSend */ private function sendBatchMail($fromEmail, BatchEntryMail $batchMail, $doSend) { $receivers = $batchMail->getReceiverEntries($this->em); $this->writeOutput('Sending "' . $batchMail->getName() . '" mail to ' . count($receivers) . ' people from ' . $fromEmail); $spool = $this->mailer->getTransport()->getSpool(); foreach ($receivers as $receiver) { $this->writeOutput(' -> ' . $receiver->getEmail()); $htmlTemplate = $batchMail->getHtmlTemplate($receiver); $plainTextTemplate = $batchMail->getPlainTextTemplate($receiver); $templateData = $batchMail->getTemplateData($receiver, $this->em); $this->translator->setLocale($receiver->getPool()->getLocale()); $plainTextBody = $this->twig->render($plainTextTemplate, $templateData); $htmlBody = $this->twig->render($htmlTemplate, $templateData); $message = \Swift_Message::newInstance()->setSubject($batchMail->getSubject($receiver, $this->translator))->setFrom($fromEmail, $batchMail->getFrom($receiver, $this->translator))->setTo($receiver->getEmail())->setBody($plainTextBody)->addPart($htmlBody, 'text/html'); if ($doSend) { $this->mailer->send($message); $spool->flushQueue($this->transport); $batchMail->handleMailSent($receiver, $this->em); } } }
/** * renders content with the real website controller. * * @param PageBridge $content * @param bool $partial * * @return string */ public function render(PageBridge $content, $partial = false) { // set active theme $webspace = $this->webspaceManager->findWebspaceByKey($content->getWebspaceKey()); $this->activeTheme->setName($webspace->getTheme()->getKey()); // get controller and invoke action $request = new Request(); $request->attributes->set('_controller', $content->getController()); $controller = $this->controllerResolver->getController($request); // prepare locale for translator and request $request->setLocale($content->getLanguageCode()); $localeBefore = $this->translator->getLocale(); $this->translator->setLocale($content->getLanguageCode()); $this->requestStack->push($request); /** @var Response $response */ $response = $controller[0]->{$controller[1]}($content, true, $partial); // roll back $this->requestStack->pop(); $this->translator->setLocale($localeBefore); return $response->getContent(); }
public function onKernelController(FilterControllerEvent $event) { $controller = $event->getController(); if (!is_array($controller)) { // not a object but a different kind of callable. Do nothing return; } $controllerObject = $controller[0]; if ($controllerObject instanceof CantigaController) { // set the time formatting settings $this->translator->setLocale($event->getRequest()->getLocale()); $this->timeFormatter->configure($this->translator, $event->getRequest()->getLocale(), $event->getRequest()->getSession()->get('timezone')); $this->localeProvider->setLocale($event->getRequest()->getLocale()); // initialize the controller $potentialResponse = $controllerObject->initialize($event->getRequest(), $this->authChecker); if (!empty($potentialResponse)) { $event->setController(function () use($potentialResponse) { return $potentialResponse; }); } } }
public function onConsoleCommand() { setlocale(LC_ALL, $this->locale); $this->translator->setLocale($this->locale); $this->translatable->setTranslatableLocale(substr($this->locale, 0, 2)); }
function it_proxies_setting_the_locale_to_the_decorated_translator(TranslatorInterface $translator) { $translator->setLocale('pl_PL')->shouldBeCalled(); $this->setLocale('pl_PL'); }
/** * @param string $text * @param string $localeCode * * @return string */ private function translate($text, $localeCode) { $this->translator->setLocale($localeCode); return $this->translator->trans($text); }
public function sendRemovedSecretSantaMail(Entry $entry) { $this->translator->setLocale($entry->getPool()->getLocale()); $this->mailer->send(\Swift_Message::newInstance()->setSubject($this->translator->trans('emails.removed_secret_santa.subject'))->setFrom($this->adminEmail, $this->translator->trans('emails.sender'))->setTo($entry->getEmail(), $entry->getName())->setBody($this->templating->render('IntractoSecretSantaBundle:Emails:removedsecretsanta.html.twig', ['entry' => $entry]), 'text/html')->addPart($this->templating->render('IntractoSecretSantaBundle:Emails:removedsecretsanta.txt.twig', ['entry' => $entry]), 'text/plain')); }
/** * @param string $locale * * @return TranslatorInterface */ private function retrieveTranslator($locale) { $this->translator->setLocale($locale); return $this->translator; }
/** * Set locale to the application. * * @param string $locale */ public function apply($locale) { $this->translator->setLocale($locale); $this->addCookieToQueue($locale); $this->applyCallbacks($locale); }
private function sendPendingConfirmationMail(Pool $pool) { $this->translator->setLocale($pool->getLocale()); $message = \Swift_Message::newInstance()->setSubject($this->translator->trans('emails.pendingconfirmation.subject'))->setFrom($this->adminEmail, $this->translator->trans('emails.sender'))->setTo($pool->getOwnerEmail())->setBody($this->templating->render('IntractoSecretSantaBundle:Emails:pendingconfirmation.txt.twig', array('pool' => $pool)))->addPart($this->templating->render('IntractoSecretSantaBundle:Emails:pendingconfirmation.html.twig', array('pool' => $pool)), 'text/html'); $this->mailer->send($message); }
/** * {@inheritdoc} */ public function getFrom(Entry $receiver, TranslatorInterface $translator) { $translator->setLocale($receiver->getPool()->getLocale()); return $translator->trans('emails.sender'); }
/** * {@inheritdoc} */ public function setLocale($locale) { $this->translator->setLocale($locale); }
/** * Set locale to translator. * * @param PreRenderEvent $event */ public function setLocaleOnPreviewPreRender(PreRenderEvent $event) { $this->translator->setLocale($event->getAttribute('locale')); }
/** * @param $fromEmail * @param BatchEntryMail $batchMail * @param $doSend */ private function sendBatchMail($fromEmail, BatchEntryMail $batchMail, $doSend) { $receivers = $batchMail->getReceiverEntries($this->em); $this->writeOutput('Sending "' . $batchMail->getName() . '" mail to ' . count($receivers) . ' people from ' . $fromEmail); $spool = $this->mailer->getTransport()->getSpool(); foreach ($receivers as $receiver) { try { $this->writeOutput(' -> ' . $receiver->getEmail()); $htmlTemplate = $batchMail->getHtmlTemplate($receiver); $plainTextTemplate = $batchMail->getPlainTextTemplate($receiver); $templateData = $batchMail->getTemplateData($receiver, $this->em); $this->translator->setLocale($receiver->getPool()->getLocale()); $plainTextBody = $this->twig->render($plainTextTemplate, $templateData); $htmlBody = $this->twig->render($htmlTemplate, $templateData); $message = \Swift_Message::newInstance()->setSubject($batchMail->getSubject($receiver, $this->translator))->setFrom($fromEmail, $batchMail->getFrom($receiver, $this->translator))->setTo($receiver->getEmail())->setBody($plainTextBody)->addPart($htmlBody, 'text/html'); if ($doSend) { $this->mailer->send($message); $batchMail->handleMailSent($receiver, $this->em); } } catch (\Exception $e) { $this->writeOutput(sprintf('<error>An error occurred while sending mail for email "%s"</error>', $receiver->getEmail())); // mark as handled, as otherwise the system will keep retrying over and over again $batchMail->handleMailSent($receiver, $this->em); } } if ($doSend) { // only flush queue at the end of a batch $spool->flushQueue($this->transport); } }