View source
<?php
namespace Drupal\tmgmt_smartling\Plugin\tmgmt\Translator;
use Drupal;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\file\FileUsage\DatabaseFileUsageBackend;
use Drupal\tmgmt\Entity\Translator;
use Drupal\tmgmt\Translator\TranslatableResult;
use Drupal\tmgmt\TranslatorPluginBase;
use Drupal\tmgmt\TranslatorInterface;
use Drupal\tmgmt\JobInterface;
use Drupal\tmgmt_extension_suit\ExtendedTranslatorPluginInterface;
use Drupal\tmgmt_file\Format\FormatManager;
use Drupal\tmgmt_smartling\Smartling\SmartlingApi;
use GuzzleHttp\ClientInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\tmgmt\Translator\AvailableResult;
use Drupal\tmgmt\ContinuousTranslatorInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Drupal\tmgmt_smartling\Event\RequestTranslationEvent;
class SmartlingTranslator extends TranslatorPluginBase implements ExtendedTranslatorPluginInterface, ContainerFactoryPluginInterface, ContinuousTranslatorInterface {
protected $smartlingApi;
protected $client;
protected $formatPluginsManager;
protected $fileUsage;
protected $eventDispatcher;
public function __construct(ClientInterface $client, FormatManager $format_plugin_manager, DatabaseFileUsageBackend $file_usage, EventDispatcherInterface $event_dispatcher, array $configuration, $plugin_id, array $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->client = $client;
$this->formatPluginsManager = $format_plugin_manager;
$this->fileUsage = $file_usage;
$this->eventDispatcher = $event_dispatcher;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($container
->get('http_client'), $container
->get('plugin.manager.tmgmt_file.format'), $container
->get('file.usage'), $container
->get('event_dispatcher'), $configuration, $plugin_id, $plugin_definition);
}
public function checkAvailable(TranslatorInterface $translator) {
if ($translator
->getSetting('api_url') && $translator
->getSetting('project_id') && $translator
->getSetting('key')) {
return AvailableResult::yes();
}
return AvailableResult::no(t('@translator is not available. Make sure it is properly <a href=:configured>configured</a>.', [
'@translator' => $translator
->label(),
':configured' => $translator
->url(),
]));
}
public function checkTranslatable(TranslatorInterface $translator, JobInterface $job) {
return TranslatableResult::yes();
}
public function requestTranslation(JobInterface $job) {
$name = $this
->getFileName($job);
$export_format = pathinfo($name, PATHINFO_EXTENSION);
$export = $this->formatPluginsManager
->createInstance($export_format);
$path = $job
->getSetting('scheme') . '://tmgmt_sources/' . $name;
$dirname = dirname($path);
if (file_prepare_directory($dirname, FILE_CREATE_DIRECTORY)) {
$data = $export
->export($job);
$file = file_save_data($data, $path, FILE_EXISTS_REPLACE);
$this->fileUsage
->add($file, 'tmgmt_smartling', 'tmgmt_job', $job
->id());
$job
->submitted('Exported file can be downloaded <a href="@link">here</a>.', array(
'@link' => file_create_url($path),
));
}
else {
$e = new \Exception('It is not possible to create a directory ' . $dirname);
watchdog_exception('tmgmt_smartling', $e);
$job
->rejected('Job has been rejected with following error: @error', [
'@error' => $e
->getMessage(),
], 'error');
}
try {
$upload_params = [
'approved' => 0,
'smartling.placeholder_format_custom' => $job
->getSetting('custom_regexp_placeholder'),
];
if ($job
->getSetting('auto_authorize_locales')) {
$upload_params['localesToApprove[0]'] = $job
->getRemoteTargetLanguage();
}
if ($job
->getTranslator()
->getSetting('callback_url_use')) {
$upload_params['callbackUrl'] = Url::fromRoute('tmgmt_smartling.push_callback', [
'job' => $job
->id(),
])
->setOptions(array(
'absolute' => TRUE,
))
->toString();
}
$this
->getSmartlingApi($job
->getTranslator())
->uploadFile(\Drupal::service('file_system')
->realpath($file
->getFileUri()), $file
->getFilename(), $export_format === 'xlf' ? 'xliff' : $export_format, $upload_params);
$this->eventDispatcher
->dispatch(RequestTranslationEvent::REQUEST_TRANSLATION_EVENT, new RequestTranslationEvent($job));
} catch (\Exception $e) {
watchdog_exception('tmgmt_smartling', $e);
$job
->rejected('Job has been rejected with following error: @error uploading @file', array(
'@error' => $e
->getMessage(),
'@file' => $file
->getFileUri(),
), 'error');
}
}
public function getSupportedRemoteLanguages(TranslatorInterface $translator) {
$languages = [];
if (!$translator
->getSetting('project_id')) {
return $languages;
}
try {
$smartling_languages = $this
->getSmartlingApi($translator)
->getLocaleList();
foreach ($smartling_languages['locales'] as $language) {
$languages[$language['locale']] = $language['locale'];
}
} catch (\Exception $e) {
drupal_set_message($e
->getMessage(), 'Cannot get languages from the translator');
watchdog_exception('tmgmt_smartling', $e);
return $languages;
}
return $languages;
}
public function getDefaultRemoteLanguagesMappings() {
return array(
'zh-hans' => 'zh-CH',
'nl' => 'nl-NL',
'en' => 'en-EN',
);
}
public function getSupportedTargetLanguages(TranslatorInterface $translator, $source_language) {
$remote_languages = $this
->getSupportedRemoteLanguages($translator);
unset($remote_languages[$source_language]);
return $remote_languages;
}
public function hasCheckoutSettings(JobInterface $job) {
return FALSE;
}
protected function doRequest(Translator $translator, $path, array $query = array(), array $headers = array()) {
$response = $this->smartlingApi
->uploadFile($path);
return $response;
}
public function getSmartlingApi(TranslatorInterface $translator) {
if (empty($this->smartlingApi)) {
$this->smartlingApi = new SmartlingApi($translator
->getSetting('key'), $translator
->getSetting('project_id'), $this->client, $translator
->getSetting('api_url'));
}
return $this->smartlingApi;
}
public function getFileName(JobInterface $job) {
try {
$filename = $job
->get('job_file_name');
$filename = !empty($filename
->getValue()) ? $filename
->getValue()[0]['value'] : '';
} catch (\Exception $e) {
$filename = '';
}
if (empty($filename)) {
$extension = $job
->getSetting('export_format');
$filename = "JobID" . $job
->id() . '_' . $job
->getSourceLangcode() . '_' . $job
->getTargetLangcode() . '.' . $extension;
}
return $filename;
}
private function cleanFileName($filename, $allow_dirs = FALSE) {
$trim_filename = trim($filename);
if (empty($trim_filename)) {
return '';
}
$pattern = '/[^a-zA-Z0-9_\\-\\:]/i';
$info = pathinfo(trim($filename));
$filename = preg_replace($pattern, '_', $info['filename']);
if (isset($info['extension']) && !empty($info['extension'])) {
$filename .= '.' . preg_replace($pattern, '_', $info['extension']);
}
if ($allow_dirs && isset($info['dirname']) && !empty($info['dirname'])) {
$filename = preg_replace('/[^a-zA-Z0-9_\\/\\-\\:]/i', '_', $info['dirname']) . '/' . $filename;
}
return (string) $filename;
}
public function defaultSettings() {
return array(
'export_format' => 'xml',
'allow_override' => TRUE,
'scheme' => 'public',
'retrieval_type' => 'published',
'callback_url_use' => FALSE,
'auto_authorize_locales' => TRUE,
'xliff_processing' => TRUE,
'context_silent_user_switching' => FALSE,
'custom_regexp_placeholder' => '(@|%|!)[\\w-]+',
'context_skip_host_verifying' => FALSE,
'identical_file_name' => FALSE,
'enable_basic_auth' => FALSE,
'basic_auth' => [
'login' => '',
'password' => '',
],
);
}
public function requestJobItemsTranslation(array $job_items) {
$job = reset($job_items)
->getJob();
foreach ($job_items as $job_item) {
$this
->requestTranslation($job_item
->getJob());
if ($job
->isContinuous()) {
$job_item
->active();
}
}
}
public function downloadTranslation(JobInterface $job) {
return tmgmt_smartling_download_file($job);
}
public function isReadyForDownload(JobInterface $job) {
try {
$api = $this
->getSmartlingApi($job
->getTranslator());
$filename = $this
->getFileName($job);
$locale = $job
->getRemoteTargetLanguage();
$request_result = $api
->getStatus($filename, $locale);
} catch (\Exception $e) {
watchdog_exception('tmgmt_smartling', $e);
return FALSE;
}
\Drupal::logger('tmgmt_smartling')
->info('Check status for file: @filename found @approved approved words and @completed completed ones', [
'@filename' => $filename,
'@approved' => @$request_result['approvedStringCount'],
'@completed' => @$request_result['completedStringCount'],
]);
return $request_result['approvedStringCount'] > 0 && $request_result['approvedStringCount'] === $request_result['completedStringCount'];
}
public function cancelTranslation(JobInterface $job) {
}
}