Skip to content

Commit c207cc7

Browse files
committed
feature #30472 [Translation] Add XLIFF 1 source to metadata to differentiate from attr (ostrolucky)
This PR was merged into the 4.3-dev branch. Discussion ---------- [Translation] Add XLIFF 1 source to metadata to differentiate from attr | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Here's our use case this patch solves: We have 2 translation files. One is created by developer and has empty `<target>`. Second one is created by translator and has non-empty `<target>`, but same `<source>`. Now, unlike Symfony fixtures, we also use `resname=` XLIFF1 attribute. This allows us to detect when translation is out of date, which happens when `<source>` in dev translation is changed, but `resname=` is not. As visualization which better illustrates problem: dev.xlf: ```xml <trans-unit id="source_is_different" resname="source_is_different"> <source>Welcome to Calida</source> </trans-unit> ``` real.xlf: ```xml <trans-unit id="source_is_different" resname="source_is_different"> <source>Welcome to Jacqueline</source> <target>Willkommen bei Jacqueline</target> </trans-unit> ``` We need to be able to tell `<source>` for this pair is different (so we prepend alert glyph to translation message, to alert developer something is wrong). In current XliffFileLoader there is no way for consumer to figure out if translation key came from `resname` or `<source>`, so we had to copy whole loader. Commits ------- ab04f25 [Translation] Add XLIFF 1 source to metadata to differentiate from attr
2 parents c1f2418 + ab04f25 commit c207cc7

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

src/Symfony/Component/Translation/Loader/XliffFileLoader.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ private function extractXliff1(\DOMDocument $dom, MessageCatalogue $catalogue, s
105105
$catalogue->set((string) $source, $target, $domain);
106106

107107
$metadata = [
108+
'source' => (string) $translation->source,
108109
'file' => [
109110
'original' => (string) $fileAttributes['original'],
110111
],

src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public function testEncoding()
8686
$this->assertEquals(utf8_decode('bär'), $catalogue->get('foo', 'domain1'));
8787
$this->assertEquals(
8888
[
89+
'source' => 'foo',
8990
'notes' => [['content' => utf8_decode('bäz')]],
9091
'id' => '1',
9192
'file' => [
@@ -175,6 +176,7 @@ public function testLoadNotes()
175176

176177
$this->assertEquals(
177178
[
179+
'source' => 'foo',
178180
'notes' => [['priority' => 1, 'content' => 'foo']],
179181
'id' => '1',
180182
'file' => [
@@ -186,6 +188,7 @@ public function testLoadNotes()
186188
// message without target
187189
$this->assertEquals(
188190
[
191+
'source' => 'extrasource',
189192
'notes' => [['content' => 'bar', 'from' => 'foo']],
190193
'id' => '2',
191194
'file' => [
@@ -197,6 +200,7 @@ public function testLoadNotes()
197200
// message with empty target
198201
$this->assertEquals(
199202
[
203+
'source' => 'key',
200204
'notes' => [
201205
['content' => 'baz'],
202206
['priority' => 2, 'from' => 'bar', 'content' => 'qux'],
@@ -304,6 +308,7 @@ public function testLoadWithMultipleFileNodes()
304308

305309
$this->assertEquals(
306310
[
311+
'source' => 'foo',
307312
'id' => '1',
308313
'file' => [
309314
'original' => 'file.ext',
@@ -313,6 +318,7 @@ public function testLoadWithMultipleFileNodes()
313318
);
314319
$this->assertEquals(
315320
[
321+
'source' => 'test',
316322
'notes' => [['content' => 'note']],
317323
'id' => '4',
318324
'file' => [

src/Symfony/Component/Translation/Tests/fixtures/withnote.xlf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<target>bar</target>
88
<note priority="1">foo</note>
99
</trans-unit>
10-
<trans-unit id="2">
11-
<source>extra</source>
10+
<trans-unit id="2" resname="extra">
11+
<source>extrasource</source>
1212
<note from="foo">bar</note>
1313
</trans-unit>
1414
<trans-unit id="123">

0 commit comments

Comments
 (0)