Skip to content
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
[Translation] Skip state=needs-translation entries only when source =…
…= target
  • Loading branch information
nicolas-grekas committed Apr 11, 2024
commit 155d23f9c9a54089935b9eb024740bc38a71660e
12 changes: 8 additions & 4 deletions src/Symfony/Component/Translation/Loader/XliffFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,20 @@ private function extractXliff1(\DOMDocument $dom, MessageCatalogue $catalogue, s
continue;
}

if (isset($translation->target) && 'needs-translation' === (string) $translation->target->attributes()['state']) {
$source = (string) (isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source);

if (isset($translation->target)
&& 'needs-translation' === (string) $translation->target->attributes()['state']
&& \in_array((string) $translation->target, [$source, (string) $translation->source], true)
) {
continue;
}

$source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source;
// If the xlf file has another encoding specified, try to convert it because
// simple_xml will always return utf-8 encoded values
$target = $this->utf8ToCharset((string) ($translation->target ?? $translation->source), $encoding);

$catalogue->set((string) $source, $target, $domain);
$catalogue->set($source, $target, $domain);

$metadata = [
'source' => (string) $translation->source,
Expand All @@ -143,7 +147,7 @@ private function extractXliff1(\DOMDocument $dom, MessageCatalogue $catalogue, s
$metadata['id'] = (string) $attributes['id'];
}

$catalogue->setMetadata((string) $source, $metadata, $domain);
$catalogue->setMetadata($source, $metadata, $domain);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,17 @@ public function testLoadRawXliff()
</trans-unit>
<trans-unit id="4">
<source>test</source>
<target>with</target>
<target state="needs-translation">with</target>
<note>note</note>
</trans-unit>
<trans-unit id="5">
<source>baz</source>
<target state="needs-translation">baz</target>
</trans-unit>
<trans-unit id="6" resname="buz">
<source>baz</source>
<target state="needs-translation">buz</target>
</trans-unit>
</body>
</file>
</xliff>
Expand All @@ -65,6 +73,7 @@ public function testLoadRawXliff()
$this->assertEquals('en', $catalogue->getLocale());
$this->assertSame([], libxml_get_errors());
$this->assertContainsOnly('string', $catalogue->all('domain1'));
$this->assertSame(['foo', 'extra', 'key', 'test'], array_keys($catalogue->all('domain1')));
}

public function testLoadWithInternalErrorsEnabled()
Expand Down