You are here

class TmgmtFileCommands in Translation Management Tool 8

A Drush commandfile.

In addition to this file, you need a drush.services.yml in root of your module, and a composer.json file that provides the name of the services file to use.

See these files for an example of injecting Drupal services:

Hierarchy

  • class \Drupal\tmgmt_file\Commands\TmgmtFileCommands extends \Drush\Commands\DrushCommands

Expanded class hierarchy of TmgmtFileCommands

1 string reference to 'TmgmtFileCommands'
drush.services.yml in translators/tmgmt_file/drush.services.yml
translators/tmgmt_file/drush.services.yml
1 service uses TmgmtFileCommands
tmgmt_file.commands in translators/tmgmt_file/drush.services.yml
\Drupal\tmgmt_file\Commands\TmgmtFileCommands

File

translators/tmgmt_file/src/Commands/TmgmtFileCommands.php, line 18

Namespace

Drupal\tmgmt_file\Commands
View source
class TmgmtFileCommands extends DrushCommands {

  /**
   * Import XLIFF translation files
   *
   * @param $name
   *   Directory path that is search for *.xlf files or a file name
   *
   * @command tmgmt_translate_import
   * @aliases tmti
   *
   * @throws \Exception
   *   If there is no file or if the file is not accessible, throws an exception.
   */
  public function tmgmtTranslateImport($name) {
    if (!file_exists($name)) {

      // Drush changes the current working directory to the drupal root directory.
      // Also check the current directory.
      if (!file_exists(getcwd() . '/' . $name)) {
        throw new \Exception(dt('@name does not exists or is not accessible.', array(
          '@name' => $name,
        )));
      }
      else {

        // The path is relative to the current directory, update the variable.
        $name = getcwd() . '/' . $name;
      }
    }
    if (is_dir($name)) {
      $this
        ->logger()
        ->notice(dt('Scanning dir @dir.', array(
        '@dir' => $name,
      )));
      $files = \Drupal::service('file_system')
        ->scanDirectory($name, '/.*\\.xlf$/');
      if (empty($files)) {
        throw new \Exception(dt('No files found to import in @name.', array(
          '@name' => $name,
        )));
      }
    }
    else {

      // Create the structure expected by the loop below.
      $files = array(
        $name => (object) array(
          'name' => basename($name),
        ),
      );
    }
    $plugin = \Drupal::service('plugin.manager.tmgmt_file.format')
      ->createInstance('xlf');
    foreach ($files as $path => $info) {
      $job = $plugin
        ->validateImport($path);
      if (empty($job)) {
        $this
          ->logger()
          ->error(dt('No translation job found for @filename.', array(
          '@filename' => $info->name,
        )));
        continue;
      }
      if ($job
        ->isFinished()) {
        $this
          ->logger()
          ->warning(dt('Skipping @filename for finished job @name (#@id).', array(
          '@filename' => $info->name,
          '@name' => $job
            ->label(),
          '@id' => $job
            ->id(),
        )));
        continue;
      }
      try {

        // Validation successful, start import.
        $job
          ->addTranslatedData($plugin
          ->import($path));
        $this
          ->logger()
          ->notice(dt('Successfully imported file @filename for translation job @name (#@id).', array(
          '@filename' => $info->name,
          '@name' => $job
            ->label(),
          '@id' => $job
            ->id(),
        )));
      } catch (\Exception $e) {
        $this
          ->logger()
          ->error(dt('Failed importing file @filename: @error', array(
          '@filename' => $info->name,
          '@error' => $e
            ->getMessage(),
        )));
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TmgmtFileCommands::tmgmtTranslateImport public function Import XLIFF translation files