Using Google Translate to Automatically Translate a Symfony Language File

Recently the company I work for launched a new version of our product with multi-language capabilities. This was great for us but when we wanted to expand out to other languages we found that it was expensive to have the file translated.

Given that our language file is in a standard format (Symfony’s message YML format) we wondered if it would be possible to convert the file automatically via Google Translate and then give it to a translator to polish. It turns out that it is and this is quicker and cheaper than having all the file translated straight from English by a translator.

The routine itself is pretty straightforward but does require some set up beforehand. This includes creating a Google developer account, installation of Composer and the Google Translate API. All of this is described in detail with a step-by-step guide here.

I am going to assume that you have done all the necessary set-up. One important thing to remember is to setup you credentials on your machine for me (on a Mac but should be the same on Linux) you need to do the following:

export GOOGLE_APPLICATION_CREDENTIALS="/path/to/file/YOUR-GOOGLE-PROJECT.json"

The routine itself is written in PHP and runs from the command line and takes three parameters as follows:

  • name of the file you want translated
  • name of the output file
  • ISO code of the language you want to translate to (see here for list of supported languages)

So you might run it as follows:

php ./trans.php messages.en.yml messages.es.yml es

So here is the code, remember to change line 29 to include your Google project ID.

<php

// grab the file to be translated
if (empty($argv[1])) {
  die('You must give an input file'.PHP_EOL);
}else{
  $in = $argv[1];
}

// grab the file it is to be written to
if (empty($argv[2])) {
  die('You must give an output file'.PHP_EOL);
}else{
  $out = $argv[2];
}

// grab the target language
if (empty($argv[3])) {
  die('You must give a target language'.PHP_EOL);
}else{
  $targetLanguage = $argv[3];
}

require __DIR__ . '/vendor/autoload.php';

use Google\Cloud\Translate\TranslateClient;

$translate = new TranslateClient([
    'projectId' => 'YOUR-GOOGLE-PROJECT-ID'
]);
// open the input file to an array
$messages = file($in);

// create the output file
$translated = fopen($out, 'w');

// process each message
for ($i = 0; $i < count($messages); $i++) {

  // does this have any message text?
  if (strpos($messages[$i], ": ") !== FALSE){
    // translate the text
    $text = substr($messages[$i], strpos($messages[$i], ": ")+3,(strlen($messages[$i])-(strpos($messages[$i], ": "))-6));

    $result = $translate->translate($text, [
      'target' => $targetLanguage,
    ]);

    // write the translated line
    fwrite($translated, substr($messages[$i], 0, strpos($messages[$i], ": ")).": '".html_entity_decode($result[text])."'".PHP_EOL);
  }else{
    // rewrite the line
    fwrite($translated, $messages[$i]);
  }
}

// close the output file
fclose($translated);

echo 'Translated '.$i.' lines';

?>

In action it takes just a few minutes to translate the 1,000 or so stings we have in our language file.

Automatically Translate a Symfony Language in action

There would be a cost associated with this but I am currently using the free credits that I got when I created my Google account. Even so the costs are going to be tiny compared to having someone translate the whole file.

I am sure that there are ways that this could be improved but should prove useful as a starting point when you want a quick way to get a Symfony messages file translated.

Leave a Reply

Your email address will not be published. Required fields are marked *