• Home
  • Contact
translatable-commands-cache-rebuild.png

The Drupal Console is multilingual

Jesus Manuel Olivas
January 06, 2015

We are getting close hope by the end of this week, to a code-freeze on the development of this project, this code-freeze will be for about 4 weeks, in order to have time to work on the project documentation and prepare with David Flores aka @dmouse to present this project at DrupalCon Latino.

The first goal to complete before this code-freeze was add multilingual capabilities to the project, but why?, I can list a few reasons.

  • Drupal is multilingual and Drupal 8 is even more than ever, you can take a look a the D8MI at http://www.drupal8multilingual.org/ and we want to ship this project with similar capabilities.
  • It feels good and more natural to use a tool on your mother tongue, and by the way most of our project contributors are not native english speakers.
  • Separate messages from code will ease the text messages updates, no need to know/learn PHP or use an IDE to contribute.
  • We (David Flores & myself) will be presenting a sesion in Spanish, related to this project at the DrupalCon Latino in Bogota, and at some point we decide making project multilingual will be interesting for the event audience.

As I mentioned on twitter:

Code look a little hacky but got a translatable version of #Drupal Console commands, feature will be available on the next release. #drupal8

— Jesus Manuel Olivas (@jmolivas) January 2, 2015

But wee need a starting point.

Talking about code, this is what was required

Adding the Symfony Translation Component to the composer.json file

"require": { ... + "symfony/config": "2.6.*", + "symfony/translation": "2.6.*", ... },

For more information about the Translation Component look the awesome symfony documentation here.

Add translation files and messages

# extract of config/translations/console.en.yml command: cache: rebuild: description: Rebuild and clear all site caches. options: cache: Only clean a specific cache. messages: welcome: Welcome to the cache:rebuild command. rebuild: Rebuilding cache(s), wait a moment please. completed: Done cleaning cache(s). invalid_cache: Cache "%s" is invalid. questions: cache: Select cache.

Actually four languages files are available (en, es, fr and pt) you can find those files here, take note those files are only a copy of the console.en.yml file with few overrides for testing purposes.

Create a new Helper class

In order to take care of the translation the TranslatorHelper class was added see code here, the Helper was also registered at bin/conosle.php see code here

Inject the TranslatorHelper

For this task was necessary to modify the RegisterCommandsHelper class, obtaining the TranslatorHelper and Injecting via the constructor when creating and registering a new instance of each command.

if ($cmd->getConstructor()->getNumberOfRequiredParameters()>0) { $translator = $this->getHelperSet()->get('translator'); $command = $cmd->newInstance($translator); } else { $command = $cmd->newInstance(); } $this->console->add($command);

You can see the full class here

How can you help

Feel free to take a look at the messages at the github repo and send us fixes.

How to override the default language

As simple as creating a new YAML file at your home directory ~/.console/config.yml and override the language value.

#file path ~/.console/config.yml application: language: es

How to make a console.phar

We are using and recommend this great project http://box-project.org/

$ curl -LSs https://box-project.github.io/box2/installer.php | php $ mv box.phar /usr/local/bin/box // Run this inside your project directory to create a new console.phar file $ box build

Feel free to try this new multilingual feature on the latest release v0.6.0, and as usual feel free to ask any questions commenting on this page, or adding a new issue on the drupal project page or the github repository.

Ready to embrace the JAMstack revolution?

Build fast and secure sites and apps delivered by pre-rendering files and serving them directly from a CDN, removing the requirement to manage or run web servers, databases and worry about traffic spikes.
Work with us!