The Drupal Console is Now Multilingual

The Drupal Console is Now Multilingual

default avatar
Thought byJesus Olivas
January 21, 2015
FFW Blog - illustration

We're very pleased to announce that the new Drupal Console project is now multilingual!

We put a lot of hours into this effort because we felt it was so important to broaden the base of Console users and help them get off to a great start developing modules for Drupal 8. It will be the last major feature to be added before an upcoming code freeze that will allow us to work on Console documentation - another effort to broaden the base of users that can benefit from the project.

Here are a few reasons why we felt it was so important to add multilingual capabilities to the Console project:

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

As I mentioned on twitter:

But we needed 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.

Create a new Helper class

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

Inject the TranslatorHelper

For this task it 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);

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.

I mentioned earlier that we are moving toward a code freeze so we can focus on documentation. The freeze is expected to be in place for about 4 weeks. I'll also use the time to prepare my DrupalCon Latino  Console presentation with David Flores aka @dmouse.

Stay tuned!

This post has been adapted from my personal blog.