8000 bug #40283 [Translation] Make `name` attribute optional in xliff2 (Ma… · symfony/symfony@91121ac · GitHub
[go: up one dir, main page]

Skip to content

Commit 91121ac

Browse files
bug #40283 [Translation] Make name attribute optional in xliff2 (MarieMinasyan)
This PR was submitted for the 5.2 branch but it was merged into the 4.4 branch instead. Discussion ---------- [Translation] Make `name` attribute optional in xliff2 Do not set a fake `name` attribute on `unit` element from xliff2 to allow using `source` attribute and avoid missing translation error | Q | A | ------------- | --- | Branch? | 5.2 | Bug fix? | yes/no | New feature? | no | Deprecations? | no | Tickets | Fix #37055 | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> When `xlf` translations are loaded, if a name exists on `unit` element, the segment's source is ignored: ```foreach ($xml->xpath('//xliff:unit') as $unit) { foreach ($unit->segment as $segment) { $attributes = $unit->attributes(); $source = $attributes['name'] ?? $segment->source; ``` At the same time, when dumping translations, the segment's source is copied into the unit's name attribute, unless it's longer than 80 characters. In that case, `substr(md5($source), -7)` is set into the name attribute. This results in a missing translation error, because the source is ignored and the name is a random string. Suggested solution: only set the name attribute if the string is less than 80 characters. Commits ------- 9705855 [Translation] Make `name` attribute optional in xliff2
2 parents 56194a4 + 9705855 commit 91121ac

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

src/Symfony/Component/Translation/Dumper/XliffFileDumper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,11 @@ private function dumpXliff2(string $defaultLocale, MessageCatalogue $messages, ?
150150
foreach ($messages->all($domain) as $source => $target) {
151151
$translation = $dom->createElement('unit');
152152
$translation->setAttribute('id', strtr(substr(base64_encode(hash('sha256', $source, true)), 0, 7), '/+', '._'));
153-
$name = $source;
154-
if (\strlen($source) > 80) {
155-
$name = substr(md5($source), -7);
153+
154+
if (\strlen($source) <= 80) {
155+
$translation->setAttribute('name', $source);
156156
}
157-
$translation->setAttribute('name', $name);
157+
158158
$metadata = $messages->getMetadata($source, $domain);
159159

160160
// Add notes section

src/Symfony/Component/Translation/Tests/Dumper/XliffFileDumperTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function testFormatCatalogueXliff2()
4343
'foo' => 'bar',
4444
'key' => '',
4545
'key.with.cdata' => '<source> & <target>',
46+
'translation.key.that.is.longer.than.eighty.characters.should.not.have.name.attribute' => 'value',
4647
]);
4748
$catalogue->setMetadata('key', ['target-attributes' => ['order' => 1]]);
4849

src/Symfony/Component/Translation/Tests/fixtures/resources-2.0-clean.xlf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,11 @@
1919
<target><![CDATA[<source> & <target>]]></target>
2020
</segment>
2121
</unit>
22+
<unit id="aF1tx51">
23+
<segment>
24+
<source>translation.key.that.is.longer.than.eighty.characters.should.not.have.name.attribute</source>
25+
<target>value</target>
26+
</segment>
27+
</unit>
2228
</file>
2329
</xliff>

0 commit comments

Comments
 (0)
0