TmgmtFileCommands.php in Translation Management Tool 8
File
translators/tmgmt_file/src/Commands/TmgmtFileCommands.php
View source
<?php
namespace Drupal\tmgmt_file\Commands;
use Drush\Commands\DrushCommands;
class TmgmtFileCommands extends DrushCommands {
public function tmgmtTranslateImport($name) {
if (!file_exists($name)) {
if (!file_exists(getcwd() . '/' . $name)) {
throw new \Exception(dt('@name does not exists or is not accessible.', array(
'@name' => $name,
)));
}
else {
$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 {
$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 {
$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(),
)));
}
}
}
}