diff --git a/translators/tmgmt_file/config/schema/tmgmt_file.schema.yml b/translators/tmgmt_file/config/schema/tmgmt_file.schema.yml index f3ee335..032198c 100644 --- a/translators/tmgmt_file/config/schema/tmgmt_file.schema.yml +++ b/translators/tmgmt_file/config/schema/tmgmt_file.schema.yml @@ -13,3 +13,6 @@ tmgmt.translator.settings.file: xliff_processing: type: boolean label: Xliff HTML processing + xliff_cdata: + type: boolean + label: Xliff CDATA diff --git a/translators/tmgmt_file/src/FileTranslatorUi.php b/translators/tmgmt_file/src/FileTranslatorUi.php index 38de180..8b41fc3 100644 --- a/translators/tmgmt_file/src/FileTranslatorUi.php +++ b/translators/tmgmt_file/src/FileTranslatorUi.php @@ -41,6 +41,21 @@ class FileTranslatorUi extends TranslatorPluginUiBase { '#default_value' => $translator->getSetting('xliff_processing'), ); + $form['xliff_cdata'] = array( + '#type' => 'checkbox', + '#title' => t('XLIFF CDATA'), + '#description' => t('Check to use CDATA for import/export.'), + '#default_value' => $translator->getSetting('xliff_cdata'), + ); + + $form['xliff_message'] = array( + '#type' => 'container', + '#markup' => t('By selecting CDATA option, XLIFF processing will be ignored.'), + '#attributes' => array( + 'class' => array('messages messages--warning'), + ), + ); + $form['allow_override'] = array( '#type' => 'checkbox', '#title' => t('Allow to override the format per job'), diff --git a/translators/tmgmt_file/src/Format/FormatInterface.php b/translators/tmgmt_file/src/Format/FormatInterface.php index 49c89f8..82f547b 100644 --- a/translators/tmgmt_file/src/Format/FormatInterface.php +++ b/translators/tmgmt_file/src/Format/FormatInterface.php @@ -33,12 +33,14 @@ interface FormatInterface { * * @param $imported_file * File path to the file to be imported. + * @param bool $is_file + * (optional) Whether $imported_file is the path to a file or not. * * @return Job * Returns the corresponding translation job entity if the import file is * valid, FALSE otherwise. */ - function validateImport($imported_file); + function validateImport($imported_file, $is_file = TRUE); /** * Converts an exported file content back to the translated data. diff --git a/translators/tmgmt_file/src/Plugin/tmgmt/Translator/FileTranslator.php b/translators/tmgmt_file/src/Plugin/tmgmt/Translator/FileTranslator.php index 1a5bc86..41314af 100644 --- a/translators/tmgmt_file/src/Plugin/tmgmt/Translator/FileTranslator.php +++ b/translators/tmgmt_file/src/Plugin/tmgmt/Translator/FileTranslator.php @@ -67,6 +67,7 @@ class FileTranslator extends TranslatorPluginBase { // 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, ); } diff --git a/translators/tmgmt_file/src/Plugin/tmgmt_file/Format/Html.php b/translators/tmgmt_file/src/Plugin/tmgmt_file/Format/Html.php index 5c8a6d3..e015d41 100644 --- a/translators/tmgmt_file/src/Plugin/tmgmt_file/Format/Html.php +++ b/translators/tmgmt_file/src/Plugin/tmgmt_file/Format/Html.php @@ -83,7 +83,7 @@ class Html implements FormatInterface { /** * {@inheritdoc} */ - public function validateImport($imported_file) { + public function validateImport($imported_file, $is_file = TRUE) { $dom = new \DOMDocument(); if (!$dom->loadHTMLFile($imported_file)) { return FALSE; diff --git a/translators/tmgmt_file/src/Plugin/tmgmt_file/Format/Xliff.php b/translators/tmgmt_file/src/Plugin/tmgmt_file/Format/Xliff.php index 533b1b3..9067005 100644 --- a/translators/tmgmt_file/src/Plugin/tmgmt_file/Format/Xliff.php +++ b/translators/tmgmt_file/src/Plugin/tmgmt_file/Format/Xliff.php @@ -120,7 +120,7 @@ class Xliff extends \XMLWriter implements FormatInterface { $this->writeRaw($this->processForExport($element['#text'], $key_array)); } elseif ($job->getSetting('xliff_cdata')) { - $this->writeCdata($element['#text']); + $this->writeCdata(trim($element['#text'])); } else { $this->text($element['#text']); @@ -220,14 +220,14 @@ class Xliff extends \XMLWriter implements FormatInterface { /** * {@inheritdoc} */ - public function validateImport($imported_file) { + public function validateImport($imported_file, $is_file = TRUE) { // Validates imported XLIFF file. // Checks: // - Job ID // - Target ans source languages // - Content integrity. - $xml = $this->getImportedXML($imported_file); + $xml = $this->getImportedXML($imported_file, $is_file); if ($xml === FALSE) { drupal_set_message(t('The imported file is not a valid XML.'), 'error'); return FALSE; @@ -353,6 +353,11 @@ class Xliff extends \XMLWriter implements FormatInterface { if (empty($this->importedTransUnits)) { $reader = new \XMLReader(); foreach ($this->importedXML->xpath('//xliff:trans-unit') as $unit) { + if ($job->getSetting('xliff_cdata')) { + $this->importedTransUnits[(string) $unit['id']]['#text'] = (string) $unit->target; + continue; + } + $reader->XML($unit->target->asXML()); $reader->read(); $this->importedTransUnits[(string) $unit['id']]['#text'] = diff --git a/translators/tmgmt_file/src/Tests/FileTranslatorTest.php b/translators/tmgmt_file/src/Tests/FileTranslatorTest.php index f6d6c01..ef57aa1 100644 --- a/translators/tmgmt_file/src/Tests/FileTranslatorTest.php +++ b/translators/tmgmt_file/src/Tests/FileTranslatorTest.php @@ -139,6 +139,44 @@ class FileTranslatorTest extends TMGMTTestBase { } /** + * Test the CDATA option for XLIFF export and import. + */ + function testXLIFFCDATA() { + $translator = $this->createTranslator([ + 'plugin' => 'file', + 'settings' => [ + 'export_format' => 'xlf', + 'xliff_cdata' => TRUE, + ] + ]); + + // Get the source text. + $source_text = trim(file_get_contents(drupal_get_path('module', 'tmgmt') . '/tests/testing_html/sample.html')); + + // Create a new job with XLIFF CDATA option enabled. + $job = $this->createJob(); + $job->translator = $translator->id(); + $job->addItem('test_html_source', 'test', '1'); + $job->requestTranslation(); + $targets = $this->getTransUnitsContent($job); + + // Add CDATA prefix to source text to assert that target source data was + // properly updated. + $cdata_source_text = ''; + $this->assertEqual(trim(html_entity_decode($targets['0']['source'])), $cdata_source_text); + + // Disable XLIFF CDATA and assert that target source data was handled as + // text. + $translator->setSetting('xliff_cdata', FALSE)->save(); + $job = $this->createJob(); + $job->translator = $translator->id(); + $job->addItem('test_html_source', 'test', '1'); + $job->requestTranslation(); + $targets = $this->getTransUnitsContent($job); + $this->assertEqual(trim(html_entity_decode($targets['0']['source'])), $source_text); + } + + /** * Gets trans-unit content from the XLIFF file that has been exported for the * given job as last. */