tmgmt_file.drush.inc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * @file
  4. * Drush integration for tmgmt_file.
  5. */
  6. /**
  7. * Implements hook_drush_command().
  8. */
  9. function tmgmt_file_drush_command() {
  10. $items = array();
  11. $items['tmgmt_translate_import'] = array(
  12. 'description' => 'Import XLIFF translation files',
  13. 'arguments' => array(
  14. 'name' => 'Directory path that is search for *.xlf files or a file name',
  15. ),
  16. 'aliases' => array('tmti'),
  17. );
  18. return $items;
  19. }
  20. /**
  21. * Import XLIFF files from a directory or single file.
  22. */
  23. function drush_tmgmt_file_tmgmt_translate_import($name = NULL) {
  24. if (!$name) {
  25. return drush_set_error(dt('You need to provide a directory path or filename.'));
  26. }
  27. if (!file_exists($name)) {
  28. // Drush changes the current working directory to the drupal root directory.
  29. // Also check the current directory.
  30. if (!file_exists(drush_cwd() . '/' . $name)) {
  31. return drush_set_error(dt('@name does not exists or is not accessible.', array('@name' => $name)));
  32. }
  33. else {
  34. // The path is relative to the current directory, update the variable.
  35. $name = drush_cwd() . '/' . $name;
  36. }
  37. }
  38. if (is_dir($name)) {
  39. drush_log(dt('Scanning dir @dir.', array('@dir' => $name)), 'success');
  40. $files = file_scan_directory($name, '/.*\.xlf$/');
  41. if (empty($files)) {
  42. drush_set_error(dt('No files found to import in @name.', array('@name' => $name)));
  43. }
  44. }
  45. else {
  46. // Create the structure expected by the loop below.
  47. $files = array($name => (object)array('name' => basename($name)));
  48. }
  49. $controller = tmgmt_file_format_controller('xlf');
  50. foreach ($files as $path => $info) {
  51. $job = $controller->validateImport($path);
  52. if (empty($job)) {
  53. drush_log(dt('No translation job found for @filename.', array('@filename' => $info->name)), 'error');
  54. continue;
  55. }
  56. if ($job->isFinished()) {
  57. drush_log(dt('Skipping @filename for finished job @name (#@id).', array('@filename' => $info->name, '@name' => $job->label(), '@id' => $job->tjid)), 'warning');
  58. continue;
  59. }
  60. try {
  61. // Validation successful, start import.
  62. $job->addTranslatedData($controller->import($path));
  63. drush_log(dt('Successfully imported file @filename for translation job @name (#@id).', array('@filename' => $info->name, '@name' => $job->label(), '@id' => $job->tjid)), 'success');
  64. }
  65. catch (Exception $e) {
  66. drush_log(dt('Failed importing file @filename: @error', array('@filename' => $info->name, '@error' => $e->getMessage())), 'error');
  67. }
  68. }
  69. }
  70. ?>