Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

FlashMessenger translator does not support parameterised string #140

Open
twmobius opened this issue Oct 13, 2017 · 7 comments
Open

FlashMessenger translator does not support parameterised string #140

twmobius opened this issue Oct 13, 2017 · 7 comments

Comments

@twmobius
Copy link

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?

@froschdesign
Copy link
Member

froschdesign commented Oct 13, 2017

…the flashMessenger addMessage() function has changed it's definition to accept only strings.

Not explicitly:

public function addMessage($message, $namespace = null, $hops = 1)

https://github.com/zendframework/zend-mvc-plugin-flashmessenger/blob/843654a029a19c38e0c3b2e940e59edec75c3e4f/src/FlashMessenger.php#L155-L157

@twmobius
Copy link
Author

Yes you are right, not explicitly, but in the DocBlock the message is defined as string, which causes my IDE to flip out

@froschdesign
Copy link
Member

froschdesign commented Oct 13, 2017

Sorry, I don't understand your problem.

_("Ticket '%s' has been saved") - what is this underscore character?

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()));

@twmobius
Copy link
Author

The _() notation is a typical gettext notation, which allows gettext (and tools like poedit) to extract the strings from the source code. (See here: https://en.wikipedia.org/wiki/Gettext)

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:

$flashMessenger->addMessage(sprintf("Ticket '%s' has been saved", $ticket->getName()));

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 $this->translator->translate() is being called on the string, during the rendering of the FlashMessenger, no exact match will be found in the translation files (=no translation).

@froschdesign
Copy link
Member

froschdesign commented Oct 13, 2017

The _() notation is a typical gettext notation, which allows gettext (and tools like poedit) to extract the strings from the source code.

I know Gettext, but this is not the question. Why do you use two translators?

  1. _()
  2. $translator->translate()

This irritates me.

In this case the message passed to the flashMessenger is "Ticket '123' has been saved"

Right, you must use:

sprintf($flashMessenger->addMessage("Ticket '%s' has been saved"), $ticket->getName());

@twmobius
Copy link
Author

twmobius commented Oct 13, 2017

There aren't two translators anywhere.

The _() is not a translator. I am just "marking" the strings so that gettext can extract them from the source code, so as to create the .po files required by the translator.

What I want is to pass strings with parameters to the FlashMessenger in sprintf() format, i.e.:

Ticket '%s' has been saved

as well as the parameters to substitute %s with after translating.

For that to work, the addMessage could perhaps expose another variable (i.e. $parameters) in the definition of the method, which the translate function could use like:

$item = $translator->translate( sprintf($item, $parameters), $translatorTextDomain );

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)

@weierophinney
Copy link
Member

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants