I quickly wrote a drush implementation - not patch yet, just code. I also changed the hook / module names, so I'm not even sure if the commands will run :)


/**
 * @file
 * Drush integration.
 */

/**
 * Implements hook_drush_command().
 */
function tmgmt_drush_command() {
  $items = array();

  $items['tmgmt_translate_import'] = array(
    'description' => 'Import XLIFF translation files',
    'arguments' => array(
      'name'    => 'Directory path.',
    ),
    'aliases' => array('tmti'),
  );

  return $items;
}

/**
 * Import XLIFF files from a directory.
 */
function tmgmt_translate_import($dir = NULL) {
  if (!$dir) {
    return drush_set_error(dt('You need to provide a directory path.'));
  }

  if (!file_exists($dir)) {
    return drush_set_error(dt('The directory does not exists or is not accessible.'));
  }

  drush_log('Scanning dir ' . $dir, 'success');
  $files = file_scan_directory($dir, '/.*\.xlf$/');
  foreach ($files as $path => $info) {
    $tjid = trim(str_replace('JobID', '', $info->name));
    $job = tmgmt_job_load((int) $tjid);
    drush_log('Loading job id ' . $tjid . ' from file ' . $info->name, 'success');
    if (empty($job)) {
      drush_log('Job id ' . $tjid . ' does not exists', 'error');
      continue;
    }
    $controller = tmgmt_file_format_controller('xlf');
    try {
      // Validation successful, start import.
      $job->addTranslatedData($controller->import($path));
      drush_log(dt('Successfully imported file ' . $info->name), 'success');
    }
    catch (Exception $e) {
      drush_log(dt('Failed importing file ' . $info->name . ': ' . $e->getMessage()), 'error');
    }
  }
}

Comments

miro_dietiker’s picture

Yes, that's a great start! I'd love to have useful drush commands in tmgmt.

berdir’s picture

Status: Active » Needs review
StatusFileSize
new1.69 KB

Refactored a bit to use validateImport() and made a patch file of it.

miro_dietiker’s picture

Status: Needs review » Needs work
+++ b/translators/file/tmgmt_file.drush.incundefined
@@ -0,0 +1,57 @@
+ * Drush integration.

I guess we should be more "TMGMT File Translator" specific.

+++ b/translators/file/tmgmt_file.drush.incundefined
@@ -0,0 +1,57 @@
+      'name'    => 'Directory path.',

I'm curious, why do we call it "directory" only? What if i only want to import a single file? I would expect a file argument plus a -r option to go through the directory tree recursively. Also add the file mask "*.xlf" to the description plus a final case "no file found" if none.

+++ b/translators/file/tmgmt_file.drush.incundefined
@@ -0,0 +1,57 @@
+  drush_log('Scanning dir ' . $dir, 'success');
...
+      drush_log('Job id ' . $info->name . ' does not exists', 'error');
...
+    drush_log('Loading job id ' . $job->tjid . ' from file ' . $info->name, 'success');
...
+      drush_log(dt('Successfully imported file ' . $info->name), 'success');
...
+      drush_log(dt('Failed importing file ' . $info->name . ': ' . $e->getMessage()), 'error');

Use placeholders such as @file like with t(). Always use drush_log(dt(..)).

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new1.71 KB

This one works.

Yes, comments and messages need work, I just uploaded it as a patch to get it started :)

berdir’s picture

StatusFileSize
new3.23 KB

Ok, lots of improvements.

- Improved messages, used dt() correctly for placeholders.
- The argument can now be an directory or a single file
- Support for both path relative to drupal root dir and current cwd
- Skipping files for finished jobs. Not sure what exactly is going to happen when importing something that is currently in review, but that's not a problem of the patch here but the file importer or maybe import in general even.

Here's how this looks now:

$ drush tmti sites
Scanning dir sites.                                                                                [success]
Successfully imported file JobID4 for translation job From English to Icelandic (#4).              [success]
Skipping JobID6 for finished job From English to Japanese (#6).                                    [warning]
Successfully imported file JobID1 for translation job From English to Chinese, Simplified (#1).    [success]
Successfully imported file JobID5 for translation job From English to Italian (#5).                [success]
Successfully imported file JobID3 for translation job From English to German (#3).                 [success]
Successfully imported file JobID2 for translation job From English to French (#2).                 [success]
Successfully imported file JobID7 for translation job From English to Korean (#7).                 [success]
Successfully imported file JobID8 for translation job From English to Spanish (#8).                [success]

That's quite neat I think :)

miro_dietiker’s picture

Status: Needs review » Reviewed & tested by the community
+++ b/translators/file/tmgmt_file.drush.incundefined
@@ -0,0 +1,80 @@
+    return drush_set_error(dt('You need to provide a directory path.'));

Then it's not necessarily a "directory path".

Rest looks great. (Didn't execute code, just did code review.)

berdir’s picture

Status: Reviewed & tested by the community » Fixed

Ok, commited.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.