Skip to content

Commit 98fdb37

Browse files
committed
feature #15717 [Translator][Loader] added XLIFF 2.0 support. (xphere, aitboudad)
This PR was merged into the 2.8 branch. Discussion ---------- [Translator][Loader] added XLIFF 2.0 support. | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Fixed tickets | #11853, #12853 | Tests pass? | yes | License | MIT Commits ------- 0c24d55 [Translation][Dumper] added XLIFF 2.0 support. 7af4fc7 [XLIFF 2.0] added support for target attributes. ace6042 apply some fixes. ce540ae update changelog. ff5d6a3 [Translation][Loader] added XLIFF 2.0 support.
2 parents 5f026ce + e4a2b75 commit 98fdb37

File tree

8 files changed

+724
-51
lines changed

8 files changed

+724
-51
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* deprecated Translator::getMessages(), rely on TranslatorBagInterface::getCatalogue() instead.
88
* added option `json_encoding` to JsonFileDumper
99
* added options `as_tree`, `inline` to YamlFileDumper
10+
* added support for XLIFF 2.0.
1011
* added support for XLIFF target and tool attributes.
1112
* added message parameters to DataCollectorTranslator.
1213
* [DEPRECATION] The `DiffOperation` class has been deprecated and

Dumper/XliffFileDumper.php

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,45 @@ class XliffFileDumper extends FileDumper
2525
*/
2626
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
2727
{
28+
$xliffVersion = '1.2';
29+
if (array_key_exists('xliff_version', $options)) {
30+
$xliffVersion = $options['xliff_version'];
31+
}
32+
2833
if (array_key_exists('default_locale', $options)) {
2934
$defaultLocale = $options['default_locale'];
3035
} else {
3136
$defaultLocale = \Locale::getDefault();
3237
}
3338

39+
if ('1.2' === $xliffVersion) {
40+
return $this->dumpXliff1($defaultLocale, $messages, $domain, $options);
41+
}
42+
if ('2.0' === $xliffVersion) {
43+
return $this->dumpXliff2($defaultLocale, $messages, $domain, $options);
44+
}
45+
46+
throw new \InvalidArgumentException(sprintf('No support implemented for dumping XLIFF version "%s".', $xliffVersion));
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
protected function format(MessageCatalogue $messages, $domain)
53+
{
54+
return $this->formatCatalogue($messages, $domain);
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
protected function getExtension()
61+
{
62+
return 'xlf';
63+
}
64+
65+
private function dumpXliff1($defaultLocale, MessageCatalogue $messages, $domain, array $options = array())
66+
{
3467
$toolInfo = array('tool-id' => 'symfony', 'tool-name' => 'Symfony');
3568
if (array_key_exists('tool_info', $options)) {
3669
$toolInfo = array_merge($toolInfo, $options['tool_info']);
@@ -103,20 +136,46 @@ protected function formatCatalogue(MessageCatalogue $messages, $domain, array $o
103136
return $dom->saveXML();
104137
}
105138

106-
/**
107-
* {@inheritdoc}
108-
*/
109-
protected function format(MessageCatalogue $messages, $domain)
139+
private function dumpXliff2($defaultLocale, MessageCatalogue $messages, $domain, array $options = array())
110140
{
111-
return $this->formatCatalogue($messages, $domain);
112-
}
141+
$dom = new \DOMDocument('1.0', 'utf-8');
142+
$dom->formatOutput = true;
113143

114-
/**
115-
* {@inheritdoc}
116-
*/
117-
protected function getExtension()
118-
{
119-
return 'xlf';
144+
$xliff = $dom->appendChild($dom->createElement('xliff'));
145+
$xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:2.0');
146+
$xliff->setAttribute('version', '2.0');
147+
$xliff->setAttribute('srcLang', str_replace('_', '-', $defaultLocale));
148+
$xliff->setAttribute('trgLang', str_replace('_', '-', $messages->getLocale()));
149+
150+
$xliffFile = $xliff->appendChild($dom->createElement('file'));
151+
$xliffFile->setAttribute('id', $domain.'.'.$messages->getLocale());
152+
153+
foreach ($messages->all($domain) as $source => $target) {
154+
$translation = $dom->createElement('unit');
155+
$translation->setAttribute('id', md5($source));
156+
157+
$segment = $translation->appendChild($dom->createElement('segment'));
158+
159+
$s = $segment->appendChild($dom->createElement('source'));
160+
$s->appendChild($dom->createTextNode($source));
161+
162+
// Does the target contain characters requiring a CDATA section?
163+
$text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target);
164+
165+
$targetElement = $dom->createElement('target');
166+
$metadata = $messages->getMetadata($source, $domain);
167+
if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) {
168+
foreach ($metadata['target-attributes'] as $name => $value) {
169+
$targetElement->setAttribute($name, $value);
170+
}
171+
}
172+
$t = $segment->appendChild($targetElement);
173+
$t->appendChild($text);
174+
175+
$xliffFile->appendChild($translation);
176+
}
177+
178+
return $dom->saveXML();
120179
}
121180

122181
/**

Loader/XliffFileLoader.php

Lines changed: 154 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,49 @@ public function load($resource, $locale, $domain = 'messages')
4141
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
4242
}
4343

44-
list($xml, $encoding) = $this->parseFile($resource);
45-
$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');
46-
4744
$catalogue = new MessageCatalogue($locale);
45+
$this->extract($resource, $catalogue, $domain);
46+
47+
if (class_exists('Symfony\Component\Config\Resource\FileResource')) {
48+
$catalogue->addResource(new FileResource($resource));
49+
}
50+
51+
return $catalogue;
52+
}
53+
54+
private function extract($resource, MessageCatalogue $catalogue, $domain)
55+
{
56+
try {
57+
$dom = XmlUtils::loadFile($resource);
58+
} catch (\InvalidArgumentException $e) {
59+
throw new InvalidResourceException(sprintf('Unable to load "%s": %s', $resource, $e->getMessage()), $e->getCode(), $e);
60+
}
61+
62+
$xliffVersion = $this->getVersionNumber($dom);
63+
$this->validateSchema($xliffVersion, $dom, $this->getSchema($xliffVersion));
64+
65+
if ('1.2' === $xliffVersion) {
66+
$this->extractXliff1($dom, $catalogue, $domain);
67+
}
68+
69+
if ('2.0' === $xliffVersion) {
70+
$this->extractXliff2($dom, $catalogue, $domain);
71+
}
72+
}
73+
74+
/**
75+
* Extract messages and metadata from DOMDocument into a MessageCatalogue.
76+
*
77+
* @param \DOMDocument $dom Source to extract messages and metadata
78+
* @param MessageCatalogue $catalogue Catalogue where we'll collect messages and metadata
79+
* @param string $domain The domain
80+
*/
81+
private function extractXliff1(\DOMDocument $dom, MessageCatalogue $catalogue, $domain)
82+
{
83+
$xml = simplexml_import_dom($dom);
84+
$encoding = strtoupper($dom->encoding);
85+
86+
$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');
4887
foreach ($xml->xpath('//xliff:trans-unit') as $translation) {
4988
$attributes = $translation->attributes();
5089

@@ -64,17 +103,47 @@ public function load($resource, $locale, $domain = 'messages')
64103
$metadata['notes'] = $notes;
65104
}
66105
if (isset($translation->target) && $translation->target->attributes()) {
67-
$metadata['target-attributes'] = $translation->target->attributes();
106+
$metadata['target-attributes'] = array();
107+
foreach ($translation->target->attributes() as $key => $value) {
108+
$metadata['target-attributes'][$key] = (string) $value;
109+
}
68110
}
69111

70112
$catalogue->setMetadata((string) $source, $metadata, $domain);
71113
}
114+
}
72115

73-
if (class_exists('Symfony\Component\Config\Resource\FileResource')) {
74-
$catalogue->addResource(new FileResource($resource));
75-
}
116+
/**
117+
* @param \DOMDocument $dom
118+
* @param MessageCatalogue $catalogue
119+
* @param string $domain
120+
*/
121+
private function extractXliff2(\DOMDocument $dom, MessageCatalogue $catalogue, $domain)
122+
{
123+
$xml = simplexml_import_dom($dom);
124+
$encoding = strtoupper($dom->encoding);
76125

77-
return $catalogue;
126+
$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:2.0');
127+
128+
foreach ($xml->xpath('//xliff:unit/xliff:segment') as $segment) {
129+
$source = $segment->source;
130+
131+
// If the xlf file has another encoding specified, try to convert it because
132+
// simple_xml will always return utf-8 encoded values
133+
$target = $this->utf8ToCharset((string) (isset($segment->target) ? $segment->target : $source), $encoding);
134+
135+
$catalogue->set((string) $source, $target, $domain);
136+
137+
$metadata = array();
138+
if (isset($segment->target) && $segment->target->attributes()) {
139+
$metadata['target-attributes'] = array();
140+
foreach ($segment->target->attributes() as $key => $value) {
141+
$metadata['target-attributes'][$key] = (string) $value;
142+
}
143+
}
144+
145+
$catalogue->setMetadata((string) $source, $metadata, $domain);
146+
}
78147
}
79148

80149
/**
@@ -103,51 +172,64 @@ private function utf8ToCharset($content, $encoding = null)
103172
}
104173

105174
/**
106-
* Validates and parses the given file into a SimpleXMLElement.
107-
*
108-
* @param string $file
109-
*
110-
* @throws \RuntimeException
111-
*
112-
* @return \SimpleXMLElement
175+
* @param string $file
176+
* @param \DOMDocument $dom
177+
* @param string $schema source of the schema
113178
*
114179
* @throws InvalidResourceException
115180
*/
116-
private function parseFile($file)
181+
private function validateSchema($file, \DOMDocument $dom, $schema)
117182
{
118-
try {
119-
$dom = XmlUtils::loadFile($file);
120-
} catch (\InvalidArgumentException $e) {
121-
throw new InvalidResourceException(sprintf('Unable to load "%s": %s', $file, $e->getMessage()), $e->getCode(), $e);
122-
}
123-
124183
$internalErrors = libxml_use_internal_errors(true);
125184

126-
$location = str_replace('\\', '/', __DIR__).'/schema/dic/xliff-core/xml.xsd';
127-
$parts = explode('/', $location);
128-
if (0 === stripos($location, 'phar://')) {
129-
$tmpfile = tempnam(sys_get_temp_dir(), 'sf2');
130-
if ($tmpfile) {
131-
copy($location, $tmpfile);
132-
$parts = explode('/', str_replace('\\', '/', $tmpfile));
133-
}
134-
}
135-
$drive = '\\' === DIRECTORY_SEPARATOR ? array_shift($parts).'/' : '';
136-
$location = 'file:///'.$drive.implode('/', array_map('rawurlencode', $parts));
137-
138-
$source = file_get_contents(__DIR__.'/schema/dic/xliff-core/xliff-core-1.2-strict.xsd');
139-
$source = str_replace('http://www.w3.org/2001/xml.xsd', $location, $source);
140-
141-
if (!@$dom->schemaValidateSource($source)) {
185+
if (!@$dom->schemaValidateSource($schema)) {
142186
throw new InvalidResourceException(sprintf('Invalid resource provided: "%s"; Errors: %s', $file, implode("\n", $this->getXmlErrors($internalErrors))));
143187
}
144188

145189
$dom->normalizeDocument();
146190

147191
libxml_clear_errors();
148192
libxml_use_internal_errors($internalErrors);
193+
}
194+
195+
private function getSchema($xliffVersion)
196+
{
197+
if ('1.2' === $xliffVersion) {
198+
$schemaSource = file_get_contents(__DIR__.'/schema/dic/xliff-core/xliff-core-1.2-strict.xsd');
199+
$xmlUri = 'http://www.w3.org/2001/xml.xsd';
200+
} elseif ('2.0' === $xliffVersion) {
201+
$schemaSource = file_get_contents(__DIR__.'/schema/dic/xliff-core/xliff-core-2.0.xsd');
202+
$xmlUri = 'informativeCopiesOf3rdPartySchemas/w3c/xml.xsd';
203+
} else {
204+
throw new \InvalidArgumentException(sprintf('No support implemented for loading XLIFF version "%s".', $xliffVersion));
205+
}
149206

150-
return array(simplexml_import_dom($dom), strtoupper($dom->encoding));
207+
return $this->fixXmlLocation($schemaSource, $xmlUri);
208+
}
209+
210+
/**
211+
* Internally changes the URI of a dependent xsd to be loaded locally.
212+
*
213+
* @param string $schemaSource Current content of schema file
214+
* @param string $xmlUri External URI of XML to convert to local
215+
*
216+
* @return string
217+
*/
218+
private function fixXmlLocation($schemaSource, $xmlUri)
219+
{
220+
$newPath = str_replace('\\', '/', __DIR__).'/schema/dic/xliff-core/xml.xsd';
221+
$parts = explode('/', $newPath);
222+
if (0 === stripos($newPath, 'phar://')) {
223+
$tmpfile = tempnam(sys_get_temp_dir(), 'sf2');
224+
if ($tmpfile) {
225+
copy($newPath, $tmpfile);
226+
$parts = explode('/', str_replace('\\', '/', $tmpfile));
227+
}
228+
}
229+
$drive = '\\' === DIRECTORY_SEPARATOR ? array_shift($parts).'/' : '';
230+
$newPath = 'file:///'.$drive.implode('/', array_map('rawurlencode', $parts));
231+
232+
return str_replace($xmlUri, $newPath, $schemaSource);
151233
}
152234

153235
/**
@@ -178,6 +260,39 @@ private function getXmlErrors($internalErrors)
178260
}
179261

180262
/**
263+
* Gets xliff file version based on the root "version" attribute.
264+
* Defaults to 1.2 for backwards compatibility.
265+
*
266+
* @param \DOMDocument $dom
267+
*
268+
* @throws \InvalidArgumentException
269+
*
270+
* @return string
271+
*/
272+
private function getVersionNumber(\DOMDocument $dom)
273+
{
274+
/** @var \DOMNode $xliff */
275+
foreach ($dom->getElementsByTagName('xliff') as $xliff) {
276+
$version = $xliff->attributes->getNamedItem('version');
277+
if ($version) {
278+
return $version->nodeValue;
279+
}
280+
281+
$namespace = $xliff->attributes->getNamedItem('xmlns');
282+
if ($namespace) {
283+
if (substr_compare('urn:oasis:names:tc:xliff:document:', $namespace->nodeValue, 0, 34) !== 0) {
284+
throw new \InvalidArgumentException(sprintf('Not a valid XLIFF namespace "%s"', $namespace));
285+
}
286+
287+
return substr($namespace, 34);
288+
}
289+
}
290+
291+
// Falls back to v1.2
292+
return '1.2';
293+
}
294+
295+
/*
181296
* @param \SimpleXMLElement|null $noteElement
182297
* @param string|null $encoding
183298
*

0 commit comments

Comments
 (0)