FileTranslator.php in Translation Management Tool 8
File
translators/tmgmt_file/src/Plugin/tmgmt/Translator/FileTranslator.php
View source
<?php
namespace Drupal\tmgmt_file\Plugin\tmgmt\Translator;
use Drupal\Core\File\FileSystemInterface;
use Drupal\tmgmt\JobInterface;
use Drupal\tmgmt\Translator\TranslatableResult;
use Drupal\tmgmt\TranslatorInterface;
use Drupal\tmgmt\TranslatorPluginBase;
class FileTranslator extends TranslatorPluginBase {
public function checkTranslatable(TranslatorInterface $translator, JobInterface $job) {
return TranslatableResult::yes();
}
public function requestTranslation(JobInterface $job) {
$name = "JobID" . $job
->id() . '_' . $job
->getSourceLangcode() . '_' . $job
->getTargetLangcode();
$export = \Drupal::service('plugin.manager.tmgmt_file.format')
->createInstance($job
->getSetting('export_format'), $job
->getSetting('format_configuration'));
$path = $job
->getSetting('scheme') . '://tmgmt_file/' . $name . '.' . $job
->getSetting('export_format');
$dirname = dirname($path);
if (\Drupal::service('file_system')
->prepareDirectory($dirname, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
$file = file_save_data($export
->export($job), $path, FileSystemInterface::EXISTS_REPLACE);
\Drupal::service('file.usage')
->add($file, 'tmgmt_file', 'tmgmt_job', $job
->id());
$job
->submitted('Exported file can be downloaded <a href="@link" download>here</a>.', array(
'@link' => file_create_url($path),
));
}
else {
$job
->rejected('Failed to create writable directory @dirname, check file system permissions.', [
'@dirname' => $dirname,
]);
}
}
public function hasCheckoutSettings(JobInterface $job) {
return $job
->getTranslator()
->getSetting('allow_override');
}
public function defaultSettings() {
return array(
'export_format' => 'xlf',
'allow_override' => TRUE,
'scheme' => 'public',
'xliff_processing' => FALSE,
'xliff_cdata' => FALSE,
'format_configuration' => [],
);
}
}