Skip to content

Commit

Permalink
Merge pull request #53 from eternoendless/handle-xliff-metadata
Browse files Browse the repository at this point in the history
Handle read Xliff metadata when dumping files
  • Loading branch information
Pablo Borowicz committed Nov 9, 2018
2 parents 551e24b + 4c6a7fa commit 0de5ee1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
4 changes: 2 additions & 2 deletions Tests/Translation/Dumper/XliffFileDumperTest.php
Expand Up @@ -52,8 +52,8 @@ private function getFilledMessageCatalogue()
public function getNoteProvider()
{
return [
['Context:', [[]]],
['Context:'.PHP_EOL.'File: file:0', [['file' => 'file', 'line' => '0']]],
['', [[]]],
['Line: 0', [['file' => 'file', 'line' => '0']]],
];
}

Expand Down
21 changes: 7 additions & 14 deletions Tests/resources/dump/en/messages.xlf
Expand Up @@ -5,8 +5,7 @@
<trans-unit id="8b04d5e3775d298e78455efc5ca404d5">
<source>first</source>
<target>first</target>
<note>Context:
File: controllers/admin/AdminAccessController.php:6</note>
<note>Line: 6</note>
</trans-unit>
</body>
</file>
Expand All @@ -15,8 +14,7 @@ File: controllers/admin/AdminAccessController.php:6</note>
<trans-unit id="a9f0e61a137d86aa9db53465e0801612">
<source>second</source>
<target>second</target>
<note>Context:
File: override/controllers/admin/AdminAccessController.php:6</note>
<note>Line: 6</note>
</trans-unit>
</body>
</file>
Expand All @@ -25,8 +23,7 @@ File: override/controllers/admin/AdminAccessController.php:6</note>
<trans-unit id="dd5c8bf51558ffcbe5007071908e9524">
<source>third</source>
<target>third</target>
<note>Context:
File: classes/helper/Helper.php:6</note>
<note>Line: 6</note>
</trans-unit>
</body>
</file>
Expand All @@ -35,8 +32,7 @@ File: classes/helper/Helper.php:6</note>
<trans-unit id="c0759f2416498708841e7975566360ce">
<source>fourth</source>
<target>fourth</target>
<note>Context:
File: themes/classic/modules/blockcart/modal.tpl:0</note>
<note>Line: 0</note>
</trans-unit>
</body>
</file>
Expand All @@ -45,8 +41,7 @@ File: themes/classic/modules/blockcart/modal.tpl:0</note>
<trans-unit id="0883a6520e6eb6c9304dcfb71034d053">
<source>fifth</source>
<target>fifth</target>
<note>Context:
File: themes/classic/templates/catalog/category.tpl:0</note>
<note>Line: 0</note>
</trans-unit>
</body>
</file>
Expand All @@ -55,8 +50,7 @@ File: themes/classic/templates/catalog/category.tpl:0</note>
<trans-unit id="93c38fcf0d200be001335ef572650346">
<source>sixth</source>
<target>sixth</target>
<note>Context:
File: override/classes/pdf/PDF.php:0</note>
<note>Line: 0</note>
</trans-unit>
</body>
</file>
Expand All @@ -65,8 +59,7 @@ File: override/classes/pdf/PDF.php:0</note>
<trans-unit id="5098d245aa911c02a50e276087c0b4a9">
<source>seventh</source>
<target>seventh</target>
<note>Context:
File: none (skip):0</note>
<note>Line: 0</note>
</trans-unit>
</body>
</file>
Expand Down
23 changes: 8 additions & 15 deletions Tests/resources/sampleXliff.xlf
Expand Up @@ -5,8 +5,7 @@
<trans-unit id="8b04d5e3775d298e78455efc5ca404d5">
<source>first</source>
<target>first</target>
<note>Context:
File: controllers/admin/AdminAccessController.php:6</note>
<note>Line: 6</note>
</trans-unit>
</body>
</file>
Expand All @@ -15,8 +14,7 @@ File: controllers/admin/AdminAccessController.php:6</note>
<trans-unit id="a9f0e61a137d86aa9db53465e0801612">
<source>second</source>
<target>second</target>
<note>Context:
File: override/controllers/admin/AdminAccessController.php:6</note>
<note>Line: 6</note>
</trans-unit>
</body>
</file>
Expand All @@ -25,8 +23,7 @@ File: override/controllers/admin/AdminAccessController.php:6</note>
<trans-unit id="dd5c8bf51558ffcbe5007071908e9524">
<source>third</source>
<target>third</target>
<note>Context:
File: classes/helper/Helper.php:6</note>
<note>Line: 6</note>
</trans-unit>
</body>
</file>
Expand All @@ -35,8 +32,7 @@ File: classes/helper/Helper.php:6</note>
<trans-unit id="c0759f2416498708841e7975566360ce">
<source>fourth</source>
<target>fourth</target>
<note>Context:
File: themes/classic/modules/blockcart/modal.tpl:0</note>
<note>Line: 0</note>
</trans-unit>
</body>
</file>
Expand All @@ -45,8 +41,7 @@ File: themes/classic/modules/blockcart/modal.tpl:0</note>
<trans-unit id="0883a6520e6eb6c9304dcfb71034d053">
<source>fifth</source>
<target>fifth</target>
<note>Context:
File: themes/classic/templates/catalog/category.tpl:0</note>
<note>Line: 0</note>
</trans-unit>
</body>
</file>
Expand All @@ -55,8 +50,7 @@ File: themes/classic/templates/catalog/category.tpl:0</note>
<trans-unit id="93c38fcf0d200be001335ef572650346">
<source>sixth</source>
<target>sixth</target>
<note>Context:
File: override/classes/pdf/PDF.php:0</note>
<note>Line: 0</note>
</trans-unit>
</body>
</file>
Expand All @@ -65,9 +59,8 @@ File: override/classes/pdf/PDF.php:0</note>
<trans-unit id="5098d245aa911c02a50e276087c0b4a9">
<source>seventh</source>
<target>seventh</target>
<note>Context:
File: none (skip):0</note>
<note>Line: 0</note>
</trans-unit>
</body>
</file>
</xliff>
</xliff>
24 changes: 19 additions & 5 deletions Translation/Dumper/XliffFileDumper.php
Expand Up @@ -96,10 +96,20 @@ public function formatCatalogue(MessageCatalogue $messages, $domain, array $opti
foreach ($messages->all($domain) as $source => $target) {
if (!empty($source)) {
$metadata = $messages->getMetadata($source, $domain);

/**
* Handle original file information from xliff file.
* This is needed if at least part of the catalogue was read from xliff files
*/
if (is_array($metadata['file']) && !empty($metadata['file']['original'])) {
$metadata['file'] = $metadata['file']['original'];
}

$metadata['file'] = Configuration::getRelativePath(
$metadata['file'],
!empty($options['root_dir']) ? realpath($options['root_dir']) : false
);

$xliffBuilder->addFile($metadata['file'], $defaultLocale, $messages->getLocale());
$xliffBuilder->addTransUnit($metadata['file'], $source, $target, $this->getNote($metadata));
}
Expand All @@ -115,21 +125,25 @@ public function formatCatalogue(MessageCatalogue $messages, $domain, array $opti
*/
private function getNote($transMetadata)
{
$context = 'Context:';
$notes = [];

if (!empty($transMetadata['file'])) {
$context .= PHP_EOL.'File: '.$transMetadata['file'];

if (isset($transMetadata['line'])) {
$context .= ':'.$transMetadata['line'];
$notes['line'] = 'Line: '.$transMetadata['line'];
}

if (isset($transMetadata['comment'])) {
$context .= PHP_EOL.' Comment: '.$transMetadata['comment'];
$notes['comment'] = 'Comment: '.$transMetadata['comment'];
}
}

return $context;
if (empty($notes) && isset($transMetadata['notes'][0]['content'])) {
// use notes loaded from xliff file
return $transMetadata['notes'][0]['content'];
}

return implode(PHP_EOL, $notes);
}

/**
Expand Down

0 comments on commit 0de5ee1

Please sign in to comment.