Skip to content

Commit dd5dafa

Browse files
authored
Merge branch 'master' into multiple-domain-scan
2 parents 368722e + a202411 commit dd5dafa

5 files changed

Lines changed: 109 additions & 1 deletion

File tree

src/Extractors/Xliff.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public static function fromString($string, Translations $translations, array $op
3434
}
3535

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

src/Generators/Xliff.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace Gettext\Generators;
44

5+
use Gettext\Translation;
56
use Gettext\Translations;
67
use DOMDocument;
78

89
class Xliff extends Generator implements GeneratorInterface
910
{
11+
const UNIT_ID_REGEXP = '/^XLIFF_UNIT_ID: (.*)$/';
12+
1013
/**
1114
* {@inheritdoc}
1215
*/
@@ -34,8 +37,11 @@ public static function toString(Translations $translations, array $options = [])
3437
}
3538

3639
foreach ($translations as $translation) {
40+
//Find an XLIFF unit ID, if one is available; otherwise generate
41+
$unitId = self::getUnitID($translation)?:md5($translation->getContext().$translation->getOriginal());
42+
3743
$unit = $dom->createElement('unit');
38-
$unit->setAttribute('id', md5($translation->getContext().$translation->getOriginal()));
44+
$unit->setAttribute('id', $unitId);
3945

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

4652
foreach ($translation->getComments() as $comment) {
53+
//Skip XLIFF unit ID comments.
54+
if (preg_match(self::UNIT_ID_REGEXP, $comment)) {
55+
continue;
56+
}
57+
4758
$notes->appendChild(self::createTextNode($dom, 'note', $comment))
4859
->setAttribute('category', 'comment');
4960
}
@@ -91,4 +102,21 @@ private static function createTextNode(DOMDocument $dom, $name, $string)
91102

92103
return $node;
93104
}
105+
106+
/**
107+
* Gets the translation's unit ID, if one is available.
108+
*
109+
* @param Translation $translation
110+
*
111+
* @return string|null
112+
*/
113+
public static function getUnitID(Translation $translation)
114+
{
115+
foreach ($translation->getComments() as $comment) {
116+
if (preg_match(self::UNIT_ID_REGEXP, $comment, $matches)) {
117+
return $matches[1];
118+
}
119+
}
120+
return null;
121+
}
94122
}

tests/AssetsTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,4 +547,26 @@ public function testVueJs2MultipleDomainScanning()
547547
$this->assertContent($translations, 'vuejs2/' . $file, 'Po');
548548
}
549549
}
550+
551+
public function testXliff()
552+
{
553+
$translations = static::get('xliff/Xliff', 'Xliff');
554+
$countTranslations = 2;
555+
$countTranslated = 2;
556+
$countHeaders = 9;
557+
558+
$this->assertCount($countTranslations, $translations);
559+
$this->assertCount($countHeaders, $translations->getHeaders());
560+
$this->assertEquals(2, $translations->countTranslated());
561+
562+
$translation1 = $translations->find(null, 'one file');
563+
$this->assertEquals(\Gettext\Generators\Xliff::getUnitID($translation1), 'custom.unit.id');
564+
565+
$translation2 = $translations->find(null, 'one');
566+
$this->assertEquals(\Gettext\Generators\Xliff::getUnitID($translation2), 'second.custom.unit.id');
567+
568+
$this->assertContent($translations, 'xliff/Po');
569+
570+
$this->runTestFormat('xliff/Po', $countTranslations, $countTranslated, $countHeaders);
571+
}
550572
}

tests/assets/xliff/Po.po

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
msgid ""
2+
msgstr ""
3+
"Content-Transfer-Encoding: 8bit\n"
4+
"Content-Type: text/plain; charset=UTF-8\n"
5+
"Language: \n"
6+
"Language-Team: \n"
7+
"Last-Translator: \n"
8+
"MIME-Version: 1.0\n"
9+
"Plural-Forms: nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n"
10+
"Project-Id-Version: \n"
11+
"Report-Msgid-Bugs-To: \n"
12+
13+
# XLIFF_UNIT_ID: custom.unit.id
14+
# Taken from http://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/Plural-forms.html
15+
msgid "one file"
16+
msgstr "1 plik"
17+
18+
# XLIFF_UNIT_ID: second.custom.unit.id
19+
msgid "one"
20+
msgstr "1"

tests/assets/xliff/Xliff.xlf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="" trgLang="">
3+
<file id=".">
4+
<notes>
5+
<note id="Content-Transfer-Encoding">8bit</note>
6+
<note id="Content-Type">text/plain; charset=UTF-8</note>
7+
<note id="Language"></note>
8+
<note id="Language-Team"></note>
9+
<note id="Last-Translator"></note>
10+
<note id="MIME-Version">1.0</note>
11+
<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>
12+
<note id="Project-Id-Version"></note>
13+
<note id="Report-Msgid-Bugs-To"></note>
14+
</notes>
15+
<unit id="custom.unit.id">
16+
<notes>
17+
<note category="context"></note>
18+
<note category="comment">Taken from http://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/Plural-forms.html</note>
19+
</notes>
20+
<segment>
21+
<source>one file</source>
22+
<target>1 plik</target>
23+
</segment>
24+
</unit>
25+
<unit id="second.custom.unit.id">
26+
<notes>
27+
<note category="context"></note>
28+
</notes>
29+
<segment>
30+
<source>one</source>
31+
<target>1</target>
32+
</segment>
33+
</unit>
34+
</file>
35+
</xliff>

0 commit comments

Comments
 (0)