-
Notifications
You must be signed in to change notification settings - Fork 61
FlashMessenger translator does not support parameterised string #140
Comments
Not explicitly: public function addMessage($message, $namespace = null, $hops = 1) |
Yes you are right, not explicitly, but in the DocBlock the message is defined as string, which causes my IDE to flip out |
Sorry, I don't understand your problem.
Why do not you use: $this->translate(sprintf("Ticket '%s' has been saved", $ticket->getName())); or $flashMessenger->addMessage(sprintf("Ticket '%s' has been saved", $ticket->getName())); |
The As for the second part of your question: If I were to directly call the translator on the controller, before passing the string to the FlashMessenger, it would mean that I would have to inject the translator to the controller, or add a Translator controller plugin (which to my knowledge doesn't exists) thus negating the use of the translator in the FlashMessenger all together (and it would look awful) Even if I used the translator view helper I would have to iterate all the messages from the FlashMessenger manually so as to translate them (in the view(!)) As for the second option you propose:
In this case the message passed to the flashMessenger is "Ticket '123' has been saved" (considering that 123 is the name of the $ticket), thus when |
I know Gettext, but this is not the question. Why do you use two translators?
This irritates me.
Right, you must use: sprintf($flashMessenger->addMessage("Ticket '%s' has been saved"), $ticket->getName()); |
There aren't two translators anywhere. The What I want is to pass strings with parameters to the FlashMessenger in sprintf() format, i.e.:
as well as the parameters to substitute For that to work, the addMessage could perhaps expose another variable (i.e.
Check here to see how the translator is currently being used by the View Helper. Perhaps I am missing something really obvious here, or I am not explaining it properly (Sorry, English is not my native language) |
This repository has been closed and moved to laminas/laminas-view; a new issue has been opened at https://github.com/laminas/laminas-view/issues/10. |
The standard format (to my knowledge) of translating a string containing a variable would be:
<?= sprintf($this->translate(_("Ticket '%s' has been saved"), $ticket->getName()); ?>
The way FlashMessenger uses the Translator does not allow for such usage, and restricts the user to simple strings.
I extended the View\Helper\FlashMessenger to allow for the translator to
sprintf()
parameters into the string, but that required me to pass an array to the addMessage() like so:$this->flashMessenger->setNamespace('xxx')->addMessage([ _("Ticket '%s' has been saved"), $ticket->getName() ]);
I recently did an
composer update
and it came to my attention now that the flashMessenger addMessage() function has changed it's definition to accept only strings.Am I wrong on this? Should the FlashMessenger view helper be updated or could the addMessage definition become more loose?
The text was updated successfully, but these errors were encountered: