Score:2

Can Drush display translated messages to users?

in flag

I'm working on a module which extended DrushCommands to create custom Drush commands. The maintainer wants those custom commands to display translatable custom messages (warnings, erros) upon usage.

(How) Can It be achieved?

Score:0
in flag

After some research i was able to achieve this using Drush's logger method together with t() method.

$this->logger()->error($this->t('message with @arg', ['@arg' => $arg]));

Also defined a const ERRORCODE = 0 and returned self::ERRORCODE; to stop logger from sending the complete stack trace on the screen and guarantee that the correct error code is being returned when an error is thrown.

I thank you all for your help and i hope this can be useful for someone in the future :D

leymannx avatar
ne flag
And all you wanted was to simply have these strings displayed on the command line in the site's default language?
Bruno Branco Bicudo avatar
in flag
Yes, It was very simple, but as a begginer i was really struggling with that y.y Now i have the strings available for translation in the site and showing translated on the command line too, as the maintainer wanted. Thanks a lot for your help also. I found logger while searching for dt ()
Score:-1
ne flag

In the past there was the system that dt() would simply print translated strings that happened to have a translation in Drupal, if you've set a default_language in a drushrc.php file.

Modern Drush now does the same simply following your site's default language. But there's no way of overriding this from drush.yml anymore. Although this shouldn't be too difficult to implement.

Quoting Drush's core maintainer Moshe Weitzman I asked about that on drupal.slack.com in the #drush channel:

There are no plans to add translation to drush output. Symfony Console apps have a standard but you have to separate your strings from your code and thats poor DX IMO.

I guess the only possible road to take now would be to create an issue to re-implement the possibility to override the language_default from drush.yml and provide that in a nice merge request.


Still you'd need to find a way to get the Drush core strings into the database somehow. Maybe from a nice new contrib module that parses all dt() strings and adds them to the database, so that you'd find them later on the interface translation page.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.