Support name attribute on the xliff2 translator loader
This commit is contained in:
parent
af4035d4ec
commit
37b31149c6
@ -1,6 +1,11 @@
|
|||||||
CHANGELOG
|
CHANGELOG
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
5.1.0
|
||||||
|
-----
|
||||||
|
|
||||||
|
* added support for `name` attribute on `unit` element from xliff2 to be used as a translation key instead of always the `source` element
|
||||||
|
|
||||||
5.0.0
|
5.0.0
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
@ -135,11 +135,12 @@ class XliffFileLoader implements LoaderInterface
|
|||||||
|
|
||||||
foreach ($xml->xpath('//xliff:unit') as $unit) {
|
foreach ($xml->xpath('//xliff:unit') as $unit) {
|
||||||
foreach ($unit->segment as $segment) {
|
foreach ($unit->segment as $segment) {
|
||||||
$source = $segment->source;
|
$attributes = $unit->attributes();
|
||||||
|
$source = $attributes['name'] ?? $segment->source;
|
||||||
|
|
||||||
// If the xlf file has another encoding specified, try to convert it because
|
// If the xlf file has another encoding specified, try to convert it because
|
||||||
// simple_xml will always return utf-8 encoded values
|
// simple_xml will always return utf-8 encoded values
|
||||||
$target = $this->utf8ToCharset((string) (isset($segment->target) ? $segment->target : $source), $encoding);
|
$target = $this->utf8ToCharset((string) ($segment->target ?? $segment->source), $encoding);
|
||||||
|
|
||||||
$catalogue->set((string) $source, $target, $domain);
|
$catalogue->set((string) $source, $target, $domain);
|
||||||
|
|
||||||
|
@ -314,4 +314,12 @@ class XliffFileLoaderTest extends TestCase
|
|||||||
$catalogue->getMetadata('test', 'domain1')
|
$catalogue->getMetadata('test', 'domain1')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testLoadVersion2WithName()
|
||||||
|
{
|
||||||
|
$loader = new XliffFileLoader();
|
||||||
|
$catalogue = $loader->load(__DIR__.'/../fixtures/resources-2.0-name.xlf', 'en', 'domain1');
|
||||||
|
|
||||||
|
$this->assertEquals(['foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo', 'qux' => 'qux source'], $catalogue->all('domain1'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
28
src/Symfony/Component/Translation/Tests/fixtures/resources-2.0-name.xlf
vendored
Normal file
28
src/Symfony/Component/Translation/Tests/fixtures/resources-2.0-name.xlf
vendored
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="en-US">
|
||||||
|
<file id="f1" original="Graphic Example.psd">
|
||||||
|
<unit id="1" name="foo">
|
||||||
|
<segment>
|
||||||
|
<source></source>
|
||||||
|
<target>bar</target>
|
||||||
|
</segment>
|
||||||
|
</unit>
|
||||||
|
<unit id="2" name="bar">
|
||||||
|
<segment>
|
||||||
|
<source>bar source</source>
|
||||||
|
<target>baz</target>
|
||||||
|
</segment>
|
||||||
|
</unit>
|
||||||
|
<unit id="3">
|
||||||
|
<segment>
|
||||||
|
<source>baz</source>
|
||||||
|
<target>foo</target>
|
||||||
|
</segment>
|
||||||
|
</unit>
|
||||||
|
<unit id="4" name="qux">
|
||||||
|
<segment>
|
||||||
|
<source>qux source</source>
|
||||||
|
</segment>
|
||||||
|
</unit>
|
||||||
|
</file>
|
||||||
|
</xliff>
|
Reference in New Issue
Block a user