Commit 79e051d8 authored by Milos Bovan's avatar Milos Bovan Committed by Sascha Grossenbacher
Browse files

Issue #2973057 by mbovan: Populate the target content with source in XLIFF exports

parent 3b3c171a
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -16,3 +16,10 @@ tmgmt.translator.settings.file:
    xliff_cdata:
      type: boolean
      label: Xliff CDATA
    format_configuration:
      type: mapping
      label: Format configuration
      mapping:
        target:
          type: string
          label: Target content
+27 −9
Original line number Diff line number Diff line
@@ -29,27 +29,45 @@ class FileTranslatorUi extends TranslatorPluginUiBase {
      '#description' => t('Please select the format you want to export data.'),
    );

    $form['xliff_cdata'] = array(
    $xliff_states = [
      '#states' => [
        'visible' => [
          ':input[name="settings[export_format]"]' => ['value' => 'xlf'],
        ],
      ],
    ];
    $form['format_configuration']['target'] = [
      '#type' => 'select',
      '#title' => t('Target content'),
      '#options' => [
        'source' => t('Same as source'),
      ],
      '#empty_option' => t('Empty'),
      '#default_value' => $translator->getSetting('format_configuration.target'),
      '#description' => t('Defines what the <target> in the XLIFF file should contain, either empty or the same as the source text.'),
    ] + $xliff_states;

    $form['xliff_cdata'] = [
      '#type' => 'checkbox',
      '#title' => t('XLIFF CDATA'),
      '#description' => t('Check to use CDATA for import/export.'),
      '#default_value' => $translator->getSetting('xliff_cdata'),
    );
    ] + $xliff_states;

    $form['xliff_processing'] = array(
    $form['xliff_processing'] = [
      '#type' => 'checkbox',
      '#title' => t('Extended XLIFF processing'),
      '#description' => t('Check to further process content semantics and mask HTML tags instead just escaping it.'),
      '#default_value' => $translator->getSetting('xliff_processing'),
    );
    ] + $xliff_states;

    $form['xliff_message'] = array(
    $form['xliff_message'] = [
      '#type' => 'container',
      '#markup' => t('By selecting CDATA option, XLIFF processing will be ignored.'),
      '#attributes' => array(
        'class' => array('messages messages--warning'),
      ),
    );
      '#attributes' => [
        'class' => ['messages messages--warning'],
      ],
    ] + $xliff_states;

    $form['allow_override'] = array(
      '#type' => 'checkbox',
+2 −1
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ class FileTranslator extends TranslatorPluginBase {
  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'));
    $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);
@@ -66,6 +66,7 @@ class FileTranslator extends TranslatorPluginBase {
      // need to make it FALSE due to backwards compatibility.
      'xliff_processing' => FALSE,
      'xliff_cdata' => FALSE,
      'format_configuration' => [],
    );
  }

+50 −15
Original line number Diff line number Diff line
@@ -69,6 +69,27 @@ class Xliff extends \XMLWriter implements FormatInterface {
  protected $importedXML;
  protected $importedTransUnits;

  /**
   * The Xliff configuration.
   *
   * @var array
   */
  protected $configuration;

  /**
   * Constructs an Xliff instance.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
    $this->configuration = $configuration;
  }

  /**
   * Adds a job item to the xml export.
   *
@@ -111,27 +132,18 @@ class Xliff extends \XMLWriter implements FormatInterface {
    $this->startElement('source');
    $this->writeAttribute('xml:lang', $this->job->getRemoteSourceLanguage());

    if ($job->getSetting('xliff_cdata')) {
      $this->writeCdata(trim($element['#text']));
    }
    elseif ($job->getSetting('xliff_processing')) {
      $this->writeRaw($this->processForExport($element['#text'], $key_array));
    }
    else {
      $this->text($element['#text']);
    }
    $this->writeData($element['#text'], $key_array);

    $this->endElement();
    $this->startElement('target');
    $this->writeAttribute('xml:lang', $this->job->getRemoteTargetLanguage());

    if (!empty($element['#translation']['#text'])) {
      if ($job->getSetting('xliff_processing')) {
        $this->writeRaw($this->processForExport($element['#translation']['#text'], $key_array));
      }
      else {
        $this->text($element['#translation']['#text']);
      $this->writeData($element['#text'], $key_array);
    }
    // Fill the target translation data with the source content.
    elseif (!empty($this->configuration['target']) && $this->configuration['target'] === 'source') {
      $this->writeData($element['#text'], $key_array);
    }

    $this->endElement();
@@ -141,6 +153,29 @@ class Xliff extends \XMLWriter implements FormatInterface {
    $this->endElement();
  }

  /**
   * Writes text according to the XLIFF export settings.
   *
   * @param string $text
   *   The contents of the text.
   * @param array $key_array
   *   The source item data key.
   *
   * @return bool
   *   TRUE on success or FALSE on failure.
   */
  protected function writeData($text, array $key_array) {
    if ($this->job->getSetting('xliff_cdata')) {
      return $this->writeCdata(trim($text));
    }

    if ($this->job->getSetting('xliff_processing')) {
      return $this->writeRaw($this->processForExport($text, $key_array));
    }

    return $this->text($text);
  }

  /**
   * {@inheritdoc}
   */
+7 −3
Original line number Diff line number Diff line
@@ -42,7 +42,10 @@ class FileTranslatorTest extends TMGMTTestBase {
      'settings' => [
        'export_format' => 'xlf',
        'xliff_processing' => TRUE,
      ]
        'format_configuration' => [
          'target' => '',
        ],
      ],
    ]);

    // Get the source text.
@@ -285,6 +288,7 @@ class FileTranslatorTest extends TMGMTTestBase {
    $translator = Translator::load('file');
    $translator
      ->setSetting('export_format', 'xlf')
      ->setSetting('format_configuration', ['target' => 'source'])
      ->save();

    // Set multiple data items for the source.
@@ -326,8 +330,8 @@ class FileTranslatorTest extends TMGMTTestBase {
    foreach ($xml->file->body->children() as $group) {
      foreach ($group->children() as $transunit) {
        if ($transunit->getName() == 'trans-unit') {
          // The target should be empty.
          $this->assertEqual($transunit->target, '');
          // The target should contain the source data.
          $this->assertEqual($transunit->target, $transunit->source);
          $transunit->target = $xml->file['target-language'] . '_' . (string) $transunit->source;
          // Store the text to allow assertions later on.
          $translated_text[(string) $group['id']][(string) $transunit['id']] = (string) $transunit->target;