Page MenuHomePhabricator

Use already-translated flash messages
Closed, ResolvedPublic

Description

At the moment we're passing a translation message name and set of parameters to Controller::addFlash() when we want to set a flash message. For example:

$this->addFlash('success', ['event-created', $event->getDisplayTitle()]);

In Symfony 4, this second parameter must be a string, not an array (i.e. it enforces what's already recommended), and so the above results in e.g.:

Argument 2 passed to Symfony\Bundle\FrameworkBundle\Controller\Controller::addFlash() must be of the type string, array given, called in grantmetrics/src/AppBundle/Controller/EventController.php on line 72

I think we should:

  1. Provide Intuition as a service (AppBundle\Service\Translator or something) that can be injected into its current location in the Twig extensions, and also into Controllers where required.
  2. Create a new common Controller base class that contains our own addFlashMessage($type, $msg, $params) that uses Intuition and passes the already-translated messages to Controller::addFlash().

Does that sound correct?

Related Objects

Event Timeline

Samwilson created this task.

Yeah, PHPStorm has been complaining about this since day one. I just did what we did in XTools, and indeed it is a hack. Intuition as a service is the correct way. I would move out the i18n-related methods from Extension.php, possibly even FormatExtension.php. The XTools variant (used as a Twig helper) is at https://github.com/x-tools/xtools/blob/master/src/AppBundle/Helper/I18nHelper.php

Provide Intuition as a service (AppBundle\Service\Translator or something) that can be injected into its current location in the Twig extensions, and also into Controllers where required.

👍

Create a new common Controller base class that contains our own addFlashMessage($type, $msg, $params) that uses Intuition and passes the already-translated messages to Controller::addFlash().

Sounds good, but if it's easier I think it's OK to translate directly in the Symfony addFlash calls, something like:

$this->addFlash('success', $this->i18n->msg(
    'event-updated',
    [$this->event->getDisplayTitle()],
));
Samwilson claimed this task.
Samwilson moved this task from Ready to Q1 2018-19 on the Community-Tech-Sprint board.

Merged.