-
-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FlashMessenger translator does not support parameterised string #7
Comments
Not explicitly: public function addMessage($message, $namespace = null, $hops = 1) Originally posted by @froschdesign at zendframework/zend-view#140 (comment) |
Yes you are right, not explicitly, but in the DocBlock the message is defined as string, which causes my IDE to flip out Originally posted by @twmobius at zendframework/zend-view#140 (comment) |
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())); Originally posted by @froschdesign at zendframework/zend-view#140 (comment) |
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 Originally posted by @twmobius at zendframework/zend-view#140 (comment) |
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()); Originally posted by @froschdesign at zendframework/zend-view#140 (comment) |
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) Originally posted by @twmobius at zendframework/zend-view#140 (comment) |
I transfered the issue. Now in |
Duplicate of laminas/laminas-i18n#7 |
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?
Originally posted by @twmobius at zendframework/zend-view#140
The text was updated successfully, but these errors were encountered: