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
Comment #1
miro_dietikerYes, that's a great start! I'd love to have useful drush commands in tmgmt.
Comment #2
berdirRefactored a bit to use validateImport() and made a patch file of it.
Comment #3
miro_dietikerI guess we should be more "TMGMT File Translator" specific.
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.
Use placeholders such as @file like with t(). Always use drush_log(dt(..)).
Comment #4
berdirThis one works.
Yes, comments and messages need work, I just uploaded it as a patch to get it started :)
Comment #5
berdirOk, 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:
That's quite neat I think :)
Comment #6
miro_dietikerThen it's not necessarily a "directory path".
Rest looks great. (Didn't execute code, just did code review.)
Comment #7
berdirOk, commited.