class FileTranslator

File translator.

Plugin annotation


@TranslatorPlugin(
  id = "file",
  label = @Translation("File exchange"),
  description = @Translation("Provider to export and import files."),
  ui = "Drupal\tmgmt_file\FileTranslatorUi"
)

Hierarchy

Expanded class hierarchy of FileTranslator

File

translators/tmgmt_file/src/Plugin/tmgmt/Translator/FileTranslator.php, line 21

Namespace

Drupal\tmgmt_file\Plugin\tmgmt\Translator
View source
class FileTranslator extends TranslatorPluginBase {

  /**
   * {@inheritdoc}
   */
  public function checkTranslatable(TranslatorInterface $translator, JobInterface $job) {

    // Anything can be exported.
    return TranslatableResult::yes();
  }

  /**
   * {@inheritdoc}
   */
  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 = \Drupal::service('file.repository')
        ->writeData($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' => \Drupal::service('file_url_generator')
          ->generateAbsoluteString($path),
      ));
    }
    else {
      $job
        ->rejected('Failed to create writable directory @dirname, check file system permissions.', [
        '@dirname' => $dirname,
      ]);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function hasCheckoutSettings(JobInterface $job) {
    return $job
      ->getTranslator()
      ->getSetting('allow_override');
  }

  /**
   * {@inheritdoc}
   */
  public function defaultSettings() {
    return array(
      'export_format' => 'xlf',
      'allow_override' => TRUE,
      'scheme' => 'public',
      // Making this setting TRUE by default is more appropriate, however we
      // need to make it FALSE due to backwards compatibility.
      'xliff_processing' => FALSE,
      'xliff_cdata' => FALSE,
      'format_configuration' => [],
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FileTranslator::checkTranslatable public function Check whether this service can handle a particular translation job. Overrides TranslatorPluginBase::checkTranslatable
FileTranslator::defaultSettings public function Defines default settings. Overrides TranslatorPluginBase::defaultSettings
FileTranslator::hasCheckoutSettings public function Returns if the translator has any settings for the passed job. Overrides TranslatorPluginBase::hasCheckoutSettings
FileTranslator::requestTranslation public function @abstract Overrides TranslatorPluginInterface::requestTranslation
TranslatorPluginBase::$escapeEnd protected property Characters that indicate the end of an escaped string. 1
TranslatorPluginBase::$escapeStart protected property Characters that indicate the beginning of an escaped string. 1
TranslatorPluginBase::abortTranslation public function Aborts a translation job. Overrides TranslatorPluginInterface::abortTranslation
TranslatorPluginBase::acceptedDataItem public function Accept a single data item. Overrides TranslatorPluginInterface::acceptedDataItem
TranslatorPluginBase::checkAvailable public function Checks whether a translator is available. Overrides TranslatorPluginInterface::checkAvailable 1
TranslatorPluginBase::escapeText public function Returns the escaped #text of a data item. Overrides TranslatorPluginInterface::escapeText
TranslatorPluginBase::getDefaultRemoteLanguagesMappings public function Specifies default mappings for local to remote language codes. Overrides TranslatorPluginInterface::getDefaultRemoteLanguagesMappings 1
TranslatorPluginBase::getEscapedString protected function Returns the escaped string.
TranslatorPluginBase::getSupportedLanguagePairs public function Default implementation that gets target languages for each remote language. This approach is ineffective and therefore it is advised that a plugin should provide own implementation. Overrides TranslatorPluginInterface::getSupportedLanguagePairs
TranslatorPluginBase::getSupportedRemoteLanguages public function Gets all supported languages of the translator. Overrides TranslatorPluginInterface::getSupportedRemoteLanguages
TranslatorPluginBase::getSupportedTargetLanguages public function Returns all available target languages that are supported by this service when given a source language. Overrides TranslatorPluginInterface::getSupportedTargetLanguages 1
TranslatorPluginBase::unescapeText public function Removes escape patterns from an escaped text. Overrides TranslatorPluginInterface::unescapeText