Skip to content
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

Closed
weierophinney opened this issue Dec 31, 2019 · 8 comments
Closed
Labels
Duplicate This issue or pull request already exists

Comments

@weierophinney
Copy link
Member

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

@weierophinney
Copy link
Member Author

…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


Originally posted by @froschdesign at zendframework/zend-view#140 (comment)

@weierophinney
Copy link
Member Author

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)

@weierophinney
Copy link
Member Author

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

Originally posted by @froschdesign at zendframework/zend-view#140 (comment)

@weierophinney
Copy link
Member 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).


Originally posted by @twmobius at zendframework/zend-view#140 (comment)

@weierophinney
Copy link
Member Author

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

Originally posted by @froschdesign at zendframework/zend-view#140 (comment)

@weierophinney
Copy link
Member Author

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)


Originally posted by @twmobius at zendframework/zend-view#140 (comment)

@samsonasik samsonasik transferred this issue from laminas/laminas-view Jul 17, 2020
@samsonasik
Copy link
Member

I transfered the issue. Now in laminas-mvc-plugin-flashmessenger

@froschdesign froschdesign added the Duplicate This issue or pull request already exists label Jun 21, 2022
@froschdesign
Copy link
Member

Duplicate of laminas/laminas-i18n#7

@froschdesign froschdesign marked this as a duplicate of laminas/laminas-i18n#7 Jun 21, 2022
@froschdesign froschdesign closed this as not planned Won't fix, can't repro, duplicate, stale Jun 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants