Skip to content

[Translation] Added segment-attributes metadata for Xliff2 files to preserve the `state´ attribute of translations #58205

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

Merged
merged 1 commit into from
Sep 18, 2024
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
2 changes: 2 additions & 0 deletions src/Symfony/Component/Translation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ CHANGELOG
* Make `ProviderFactoryTestCase` and `ProviderTestCase` compatible with PHPUnit 10+
* Add `lint:translations` command
* Deprecate passing an escape character to `CsvFileLoader::setCsvControl()`
* Make Xliff 2.0 attributes in segment element available as `segment-attributes`
metadata returned by `XliffFileLoader` and make `XliffFileDumper` write them to the file

7.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ private function dumpXliff2(string $defaultLocale, MessageCatalogue $messages, ?

$segment = $translation->appendChild($dom->createElement('segment'));

if ($this->hasMetadataArrayInfo('segment-attributes', $metadata)) {
foreach ($metadata['segment-attributes'] as $name => $value) {
$segment->setAttribute($name, $value);
}
}

$s = $segment->appendChild($dom->createElement('source'));
$s->appendChild($dom->createTextNode($source));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ private function extractXliff2(\DOMDocument $dom, MessageCatalogue $catalogue, s
$catalogue->set((string) $source, $target, $domain);

$metadata = [];
if ($segment->attributes()) {
$metadata['segment-attributes'] = [];
foreach ($segment->attributes() as $key => $value) {
$metadata['segment-attributes'][$key] = (string) $value;
}
}

if (isset($segment->target) && $segment->target->attributes()) {
$metadata['target-attributes'] = [];
foreach ($segment->target->attributes() as $key => $value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,22 @@ public function testDumpCatalogueWithXliffExtension()
$dumper->formatCatalogue($catalogue, 'messages', ['default_locale' => 'fr_FR'])
);
}

public function testFormatCatalogueXliff2WithSegmentAttributes()
{
$catalogue = new MessageCatalogue('en_US');
$catalogue->add([
'foo' => 'bar',
'key' => '',
]);
$catalogue->setMetadata('foo', ['segment-attributes' => ['state' => 'translated']]);
$catalogue->setMetadata('key', ['segment-attributes' => ['state' => 'translated', 'subState' => 'My Value']]);

$dumper = new XliffFileDumper();

$this->assertStringEqualsFile(
__DIR__.'/../Fixtures/resources-2.0-segment-attributes.xlf',
$dumper->formatCatalogue($catalogue, 'messages', ['default_locale' => 'fr_FR', 'xliff_version' => '2.0'])
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="fr-FR" trgLang="en-US">
<file id="messages.en_US">
<unit id="ea75LoN" name="foo">
<segment state="translated">
<source>foo</source>
<target>bar</target>
</segment>
</unit>
<unit id="pL305WR" name="key">
<segment state="translated" subState="My Value">
<source>key</source>
<target></target>
</segment>
</unit>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,29 @@ public function testLoadVersion2WithName()

$this->assertEquals(['foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo', 'qux' => 'qux source'], $catalogue->all('domain1'));
}

public function testLoadVersion2WithSegmentAttributes()
{
$loader = new XliffFileLoader();
$resource = __DIR__.'/../Fixtures/resources-2.0-segment-attributes.xlf';
$catalogue = $loader->load($resource, 'en', 'domain1');

// test for "foo" metadata
$this->assertTrue($catalogue->defines('foo', 'domain1'));
$metadata = $catalogue->getMetadata('foo', 'domain1');
$this->assertNotEmpty($metadata);
$this->assertCount(1, $metadata['segment-attributes']);
$this->assertArrayHasKey('state', $metadata['segment-attributes']);
$this->assertSame('translated', $metadata['segment-attributes']['state']);

// test for "key" metadata
$this->assertTrue($catalogue->defines('key', 'domain1'));
$metadata = $catalogue->getMetadata('key', 'domain1');
$this->assertNotEmpty($metadata);
$this->assertCount(2, $metadata['segment-attributes']);
$this->assertArrayHasKey('state', $metadata['segment-attributes']);
$this->assertSame('translated', $metadata['segment-attributes']['state']);
$this->assertArrayHasKey('subState', $metadata['segment-attributes']);
$this->assertSame('My Value', $metadata['segment-attributes']['subState']);
}
}
Loading