comparison vendor/symfony/translation/Dumper/XliffFileDumper.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Translation\Dumper;
13
14 use Symfony\Component\Translation\MessageCatalogue;
15 use Symfony\Component\Translation\Exception\InvalidArgumentException;
16
17 /**
18 * XliffFileDumper generates xliff files from a message catalogue.
19 *
20 * @author Michel Salib <michelsalib@hotmail.com>
21 */
22 class XliffFileDumper extends FileDumper
23 {
24 /**
25 * {@inheritdoc}
26 */
27 public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
28 {
29 $xliffVersion = '1.2';
30 if (array_key_exists('xliff_version', $options)) {
31 $xliffVersion = $options['xliff_version'];
32 }
33
34 if (array_key_exists('default_locale', $options)) {
35 $defaultLocale = $options['default_locale'];
36 } else {
37 $defaultLocale = \Locale::getDefault();
38 }
39
40 if ('1.2' === $xliffVersion) {
41 return $this->dumpXliff1($defaultLocale, $messages, $domain, $options);
42 }
43 if ('2.0' === $xliffVersion) {
44 return $this->dumpXliff2($defaultLocale, $messages, $domain, $options);
45 }
46
47 throw new InvalidArgumentException(sprintf('No support implemented for dumping XLIFF version "%s".', $xliffVersion));
48 }
49
50 /**
51 * {@inheritdoc}
52 */
53 protected function getExtension()
54 {
55 return 'xlf';
56 }
57
58 private function dumpXliff1($defaultLocale, MessageCatalogue $messages, $domain, array $options = array())
59 {
60 $toolInfo = array('tool-id' => 'symfony', 'tool-name' => 'Symfony');
61 if (array_key_exists('tool_info', $options)) {
62 $toolInfo = array_merge($toolInfo, $options['tool_info']);
63 }
64
65 $dom = new \DOMDocument('1.0', 'utf-8');
66 $dom->formatOutput = true;
67
68 $xliff = $dom->appendChild($dom->createElement('xliff'));
69 $xliff->setAttribute('version', '1.2');
70 $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');
71
72 $xliffFile = $xliff->appendChild($dom->createElement('file'));
73 $xliffFile->setAttribute('source-language', str_replace('_', '-', $defaultLocale));
74 $xliffFile->setAttribute('target-language', str_replace('_', '-', $messages->getLocale()));
75 $xliffFile->setAttribute('datatype', 'plaintext');
76 $xliffFile->setAttribute('original', 'file.ext');
77
78 $xliffHead = $xliffFile->appendChild($dom->createElement('header'));
79 $xliffTool = $xliffHead->appendChild($dom->createElement('tool'));
80 foreach ($toolInfo as $id => $value) {
81 $xliffTool->setAttribute($id, $value);
82 }
83
84 $xliffBody = $xliffFile->appendChild($dom->createElement('body'));
85 foreach ($messages->all($domain) as $source => $target) {
86 $translation = $dom->createElement('trans-unit');
87
88 $translation->setAttribute('id', md5($source));
89 $translation->setAttribute('resname', $source);
90
91 $s = $translation->appendChild($dom->createElement('source'));
92 $s->appendChild($dom->createTextNode($source));
93
94 // Does the target contain characters requiring a CDATA section?
95 $text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target);
96
97 $targetElement = $dom->createElement('target');
98 $metadata = $messages->getMetadata($source, $domain);
99 if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) {
100 foreach ($metadata['target-attributes'] as $name => $value) {
101 $targetElement->setAttribute($name, $value);
102 }
103 }
104 $t = $translation->appendChild($targetElement);
105 $t->appendChild($text);
106
107 if ($this->hasMetadataArrayInfo('notes', $metadata)) {
108 foreach ($metadata['notes'] as $note) {
109 if (!isset($note['content'])) {
110 continue;
111 }
112
113 $n = $translation->appendChild($dom->createElement('note'));
114 $n->appendChild($dom->createTextNode($note['content']));
115
116 if (isset($note['priority'])) {
117 $n->setAttribute('priority', $note['priority']);
118 }
119
120 if (isset($note['from'])) {
121 $n->setAttribute('from', $note['from']);
122 }
123 }
124 }
125
126 $xliffBody->appendChild($translation);
127 }
128
129 return $dom->saveXML();
130 }
131
132 private function dumpXliff2($defaultLocale, MessageCatalogue $messages, $domain, array $options = array())
133 {
134 $dom = new \DOMDocument('1.0', 'utf-8');
135 $dom->formatOutput = true;
136
137 $xliff = $dom->appendChild($dom->createElement('xliff'));
138 $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:2.0');
139 $xliff->setAttribute('version', '2.0');
140 $xliff->setAttribute('srcLang', str_replace('_', '-', $defaultLocale));
141 $xliff->setAttribute('trgLang', str_replace('_', '-', $messages->getLocale()));
142
143 $xliffFile = $xliff->appendChild($dom->createElement('file'));
144 $xliffFile->setAttribute('id', $domain.'.'.$messages->getLocale());
145
146 foreach ($messages->all($domain) as $source => $target) {
147 $translation = $dom->createElement('unit');
148 $translation->setAttribute('id', md5($source));
149
150 $segment = $translation->appendChild($dom->createElement('segment'));
151
152 $s = $segment->appendChild($dom->createElement('source'));
153 $s->appendChild($dom->createTextNode($source));
154
155 // Does the target contain characters requiring a CDATA section?
156 $text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target);
157
158 $targetElement = $dom->createElement('target');
159 $metadata = $messages->getMetadata($source, $domain);
160 if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) {
161 foreach ($metadata['target-attributes'] as $name => $value) {
162 $targetElement->setAttribute($name, $value);
163 }
164 }
165 $t = $segment->appendChild($targetElement);
166 $t->appendChild($text);
167
168 $xliffFile->appendChild($translation);
169 }
170
171 return $dom->saveXML();
172 }
173
174 /**
175 * @param string $key
176 * @param array|null $metadata
177 *
178 * @return bool
179 */
180 private function hasMetadataArrayInfo($key, $metadata = null)
181 {
182 return null !== $metadata && array_key_exists($key, $metadata) && ($metadata[$key] instanceof \Traversable || is_array($metadata[$key]));
183 }
184 }