<?php
namespace Drupal\tmgmt_test\Plugin\tmgmt\Translator;
use Drupal\Core\Form\FormStateInterface;
use Drupal\tmgmt\ContinuousTranslatorInterface;
use Drupal\tmgmt\Translator\AvailableResult;
use Drupal\tmgmt\Translator\TranslatableResult;
use Drupal\tmgmt\JobInterface;
use Drupal\tmgmt\JobItemInterface;
use Drupal\tmgmt\TranslatorInterface;
use Drupal\tmgmt\TranslatorPluginBase;
use Drupal\tmgmt\TranslatorRejectDataInterface;
class TestTranslator extends TranslatorPluginBase implements TranslatorRejectDataInterface, ContinuousTranslatorInterface {
protected $escapeStart = '[[[';
protected $escapeEnd = ']]]';
public function getDefaultRemoteLanguagesMappings() {
return array(
'en' => 'en-us',
'de' => 'de-ch',
);
}
public function hasCheckoutSettings(JobInterface $job) {
return $job
->getTranslator()
->getSetting('expose_settings');
}
function requestTranslation(JobInterface $job) {
$job
->addMessage('Test translator called.', array(), 'debug');
$action = $job
->getSetting('action') ?: '';
switch ($action) {
case 'submit':
$job
->submitted('Test submit.');
break;
case 'reject':
$job
->rejected('This is not supported.');
break;
case 'fail':
$job
->addMessage('Service not reachable.', array(), 'error');
break;
case 'translate':
default:
$job
->submitted('Test translation created.');
$this
->requestJobItemsTranslation($job
->getItems());
break;
}
}
function checkTranslatable(TranslatorInterface $translator, JobInterface $job) {
if ($job
->getSetting('action') == 'not_translatable') {
return TranslatableResult::no(t('@translator can not translate from @source to @target.', array(
'@translator' => $job
->getTranslator()
->label(),
'@source' => $job
->getSourceLanguage()
->getName(),
'@target' => $job
->getTargetLanguage()
->getName(),
)));
}
return parent::checkTranslatable($translator, $job);
}
function checkAvailable(TranslatorInterface $translator) {
if ($translator
->getSetting('action') == 'not_available') {
return AvailableResult::no(t('@translator is not available. Make sure it is properly <a href=:configured>configured</a>.', [
'@translator' => $translator
->label(),
':configured' => $translator
->toUrl()
->toString(),
]));
}
return parent::checkAvailable($translator);
}
public function getSupportedTargetLanguages(TranslatorInterface $translator, $source_language) {
$languages = array(
'en',
'de',
'es',
'it',
'pt',
'zh-hans',
'gsw-berne',
);
$languages = array_combine($languages, $languages);
unset($languages[$source_language]);
return $languages;
}
public function rejectDataItem(JobItemInterface $job_item, array $key, array $values = NULL) {
$key = '[' . implode('][', $key) . ']';
$job_item
->addMessage('Rejected data item @key for job item @item in job @job.', array(
'@key' => $key,
'@item' => $job_item
->id(),
'@job' => $job_item
->getJobId(),
));
return TRUE;
}
public function rejectForm(array $form, FormStateInterface $form_state) {
return $form;
}
public function requestJobItemsTranslation(array $job_items) {
$data_service = \Drupal::service('tmgmt.data');
$group = [];
foreach ($job_items as $job_item) {
$job = $job_item
->getJob();
$target_langcode = $job
->getTargetLangcode();
$remote_target_langcode = $job
->getRemoteTargetLanguage();
$group[] = [
'item_id' => $job_item
->id(),
'job_id' => $job_item
->getJobId(),
];
$job_item
->active('Requested translation to the continuous translator.', [], 'debug');
$data = $data_service
->filterTranslatable($job_item
->getData());
$tdata = [];
foreach ($data as $key => $value) {
if (strpos($value['#text'], '/' . $job
->getSourceLangcode()) === 0) {
$tdata[$key]['#text'] = str_replace('/' . $job
->getSourceLangcode(), '/' . $job
->getTargetLangcode(), $value['#text']);
}
elseif ($target_langcode != $remote_target_langcode) {
$tdata[$key]['#text'] = $target_langcode . '(' . $remote_target_langcode . '): ' . $value['#text'];
}
else {
$tdata[$key]['#text'] = $target_langcode . ': ' . $value['#text'];
}
}
foreach ($data_service
->getTranslatableFiles($job_item
->getData()) as $key => $source_file) {
$source_uri = $source_file
->getFileUri();
$source_content = file_get_contents($source_uri);
if ($target_langcode != $remote_target_langcode) {
$translation_content = $target_langcode . '(' . $remote_target_langcode . '): ' . $source_content;
}
else {
$translation_content = $target_langcode . ': ' . $source_content;
}
$translation_file = $data_service
->createFileTranslation($source_file, $target_langcode, $translation_content);
$tdata[$key]['#file'] = $translation_file
->id();
}
$job_item
->addTranslatedData($data_service
->unflatten($tdata));
}
$groups = \Drupal::state()
->get('job_item_groups') ?: [];
$groups[] = $group;
\Drupal::state()
->set('job_item_groups', $groups);
}
}