Skip to content

Commit

Permalink
php-gettext#224 Allow for setting custom ID to avoid translation clob…
Browse files Browse the repository at this point in the history
…bering
  • Loading branch information
asmecher committed Sep 19, 2019
1 parent a545ffb commit 6889463
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/Extractors/Xliff.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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]");
$unitId = (string) $unit['id'];
$translation->addComment("XLIFF_UNIT_ID: $unitId");
$translation->setId($unitId);
}
$translation->setTranslation(array_shift($targets));
$translation->setPluralTranslations($targets);
Expand Down
19 changes: 18 additions & 1 deletion src/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
class Translation
{
protected $id;
protected $context;
protected $original;
protected $translation = '';
Expand Down Expand Up @@ -69,14 +70,30 @@ public function getClone($context = null, $original = null)
return $new;
}

/**
* Sets the id of this translation.
* @warning The use of this function to set a custom ID will prevent
* Translations::find from matching this translation.
*
* @param string $id
*/
public function setId($id)
{
$this->id = $id;
}


/**
* Returns the id of this translation.
*
* @return string
*/
public function getId()
{
return static::generateId($this->context, $this->original);
if ($this->id === null) {
return static::generateId($this->context, $this->original);
}
return $this->id;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ public function hasDomain()
*
* @param string|Translation $context The context of the translation or a translation instance
* @param string $original The original string
* @warning Translations with custom identifiers (e.g. XLIFF unit IDs) cannot be found using this function.
*
* @return Translation|false
*/
Expand Down
31 changes: 21 additions & 10 deletions tests/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,22 +551,33 @@ public function testVueJs2MultipleDomainScanning()
public function testXliff()
{
$translations = static::get('xliff/Xliff', 'Xliff');
$countTranslations = 2;
$countTranslated = 2;
$countTranslations = 3;
$countTranslated = 3;
$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');
$this->assertEquals($countTranslated, $translations->countTranslated());

$translation2 = $translations->find(null, 'one');
$this->assertEquals(\Gettext\Generators\Xliff::getUnitID($translation2), 'second.custom.unit.id');
foreach ($translations as $translation) {
switch (\Gettext\Generators\Xliff::getUnitID($translation)) {
case 'custom.unit.id':
$this->assertEquals($translation->getOriginal(), 'one file');
$this->assertEquals($translation->getTranslation(), '1 plik');
break;
case 'second.custom.unit.id':
$this->assertEquals($translation->getOriginal(), 'one');
$this->assertEquals($translation->getTranslation(), '1');
break;
case 'duplicate.source.unit.id':
$this->assertEquals($translation->getOriginal(), 'one');
$this->assertEquals($translation->getTranslation(), 'uno');
break;
default:
$this->assertFalse(true);
}
}

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

$this->runTestFormat('xliff/Po', $countTranslations, $countTranslated, $countHeaders);
}
}
4 changes: 4 additions & 0 deletions tests/assets/xliff/Po.po
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ msgstr "1 plik"
# XLIFF_UNIT_ID: second.custom.unit.id
msgid "one"
msgstr "1"

# XLIFF_UNIT_ID: duplicate.source.unit.id
msgid "one"
msgstr "uno"
9 changes: 9 additions & 0 deletions tests/assets/xliff/Xliff.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,14 @@
<target>1</target>
</segment>
</unit>
<unit id="duplicate.source.unit.id">
<notes>
<note category="context"></note>
</notes>
<segment>
<source>one</source>
<target>uno</target>
</segment>
</unit>
</file>
</xliff>

0 comments on commit 6889463

Please sign in to comment.