Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for XLIFF unit IDs #221

Merged
merged 4 commits into from
Sep 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Extractors/Xliff.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public static function fromString($string, Translations $translations, array $op
}

$translation = new Translation(null, (string) $segment->source);
if (isset($unit['id'])) {
$translation->addComment("XLIFF_UNIT_ID: $unit[id]");
}
$translation->setTranslation(array_shift($targets));
$translation->setPluralTranslations($targets);

Expand Down
30 changes: 29 additions & 1 deletion src/Generators/Xliff.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace Gettext\Generators;

use Gettext\Translation;
use Gettext\Translations;
use DOMDocument;

class Xliff extends Generator implements GeneratorInterface
{
const UNIT_ID_REGEXP = '/^XLIFF_UNIT_ID: (.*)$/';

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -34,8 +37,11 @@ public static function toString(Translations $translations, array $options = [])
}

foreach ($translations as $translation) {
//Find an XLIFF unit ID, if one is available; otherwise generate
$unitId = self::getUnitID($translation)?:md5($translation->getContext().$translation->getOriginal());

$unit = $dom->createElement('unit');
$unit->setAttribute('id', md5($translation->getContext().$translation->getOriginal()));
$unit->setAttribute('id', $unitId);

//Save comments as notes
$notes = $dom->createElement('notes');
Expand All @@ -44,6 +50,11 @@ public static function toString(Translations $translations, array $options = [])
->setAttribute('category', 'context');

foreach ($translation->getComments() as $comment) {
//Skip XLIFF unit ID comments.
if (preg_match(self::UNIT_ID_REGEXP, $comment)) {
continue;
}

$notes->appendChild(self::createTextNode($dom, 'note', $comment))
->setAttribute('category', 'comment');
}
Expand Down Expand Up @@ -91,4 +102,21 @@ private static function createTextNode(DOMDocument $dom, $name, $string)

return $node;
}

/**
* Gets the translation's unit ID, if one is available.
*
* @param Translation $translation
*
* @return string|null
*/
public static function getUnitID(Translation $translation)
{
foreach ($translation->getComments() as $comment) {
if (preg_match(self::UNIT_ID_REGEXP, $comment, $matches)) {
return $matches[1];
}
}
return null;
}
}
21 changes: 21 additions & 0 deletions tests/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,4 +501,25 @@ public function testPhpCode4()
$this->runTestFormat('phpcode4/YamlDictionary', $countTranslations, $countTranslated);
}

public function testXliff()
{
$translations = static::get('xliff/Xliff', 'Xliff');
$countTranslations = 2;
$countTranslated = 2;
$countHeaders = 9;

$this->assertCount($countTranslations, $translations);
$this->assertCount($countHeaders, $translations->getHeaders());
$this->assertEquals(2, $translations->countTranslated());

$translation1 = $translations->find(null, 'one file');
$this->assertEquals(\Gettext\Generators\Xliff::getUnitID($translation1), 'custom.unit.id');

$translation2 = $translations->find(null, 'one');
$this->assertEquals(\Gettext\Generators\Xliff::getUnitID($translation2), 'second.custom.unit.id');

$this->assertContent($translations, 'xliff/Po');

$this->runTestFormat('xliff/Po', $countTranslations, $countTranslated, $countHeaders);
}
}
20 changes: 20 additions & 0 deletions tests/assets/xliff/Po.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
msgid ""
msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Language: \n"
"Language-Team: \n"
"Last-Translator: \n"
"MIME-Version: 1.0\n"
"Plural-Forms: nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n"
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"

# XLIFF_UNIT_ID: custom.unit.id
# Taken from http://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/Plural-forms.html
msgid "one file"
msgstr "1 plik"

# XLIFF_UNIT_ID: second.custom.unit.id
msgid "one"
msgstr "1"
35 changes: 35 additions & 0 deletions tests/assets/xliff/Xliff.xlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="" trgLang="">
<file id=".">
<notes>
<note id="Content-Transfer-Encoding">8bit</note>
<note id="Content-Type">text/plain; charset=UTF-8</note>
<note id="Language"></note>
<note id="Language-Team"></note>
<note id="Last-Translator"></note>
<note id="MIME-Version">1.0</note>
<note id="Plural-Forms"><![CDATA[nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);]]></note>
<note id="Project-Id-Version"></note>
<note id="Report-Msgid-Bugs-To"></note>
</notes>
<unit id="custom.unit.id">
<notes>
<note category="context"></note>
<note category="comment">Taken from http://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/Plural-forms.html</note>
</notes>
<segment>
<source>one file</source>
<target>1 plik</target>
</segment>
</unit>
<unit id="second.custom.unit.id">
<notes>
<note category="context"></note>
</notes>
<segment>
<source>one</source>
<target>1</target>
</segment>
</unit>
</file>
</xliff>