1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHP Translation package. |
5
|
|
|
* |
6
|
|
|
* (c) PHP Translation team <[email protected]> |
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 Translation\SymfonyStorage\Dumper; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Translation\Dumper\FileDumper; |
15
|
|
|
use Symfony\Component\Translation\MessageCatalogue; |
16
|
|
|
use Symfony\Component\Translation\Exception\InvalidArgumentException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* XliffFileDumper generates xliff files from a message catalogue. |
20
|
|
|
* Mostly borrowed from Symfony. |
21
|
|
|
* |
22
|
|
|
* @author Tobias Nyholm <[email protected]> |
23
|
|
|
* @author Michel Salib <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
final class XliffDumper extends FileDumper |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
2 |
|
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = []) |
31
|
|
|
{ |
32
|
2 |
|
$xliffVersion = '1.2'; |
33
|
2 |
|
if (array_key_exists('xliff_version', $options)) { |
34
|
2 |
|
$xliffVersion = $options['xliff_version']; |
35
|
2 |
|
} |
36
|
|
|
|
37
|
2 |
|
if (array_key_exists('default_locale', $options)) { |
38
|
|
|
$defaultLocale = $options['default_locale']; |
39
|
|
|
} else { |
40
|
2 |
|
$defaultLocale = \Locale::getDefault(); |
41
|
|
|
} |
42
|
|
|
|
43
|
2 |
|
if ('1.2' === $xliffVersion) { |
44
|
|
|
return $this->dumpXliff1($defaultLocale, $messages, $domain, $options); |
45
|
|
|
} |
46
|
2 |
|
if ('2.0' === $xliffVersion) { |
47
|
2 |
|
return $this->dumpXliff2($defaultLocale, $messages, $domain, $options); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
throw new InvalidArgumentException( |
51
|
|
|
sprintf('No support implemented for dumping XLIFF version "%s".', $xliffVersion) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Symfony 2.7 support. |
57
|
|
|
* |
58
|
|
|
* @param MessageCatalogue $messages |
59
|
|
|
* @param string $domain |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
protected function format(MessageCatalogue $messages, $domain) |
64
|
|
|
{ |
65
|
|
|
return $this->dumpXliff2(\Locale::getDefault(), $messages, $domain); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
protected function getExtension() |
72
|
|
|
{ |
73
|
|
|
return 'xlf'; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function dumpXliff1($defaultLocale, MessageCatalogue $messages, $domain, array $options = []) |
77
|
|
|
{ |
78
|
|
|
$toolInfo = ['tool-id' => 'symfony', 'tool-name' => 'Symfony']; |
79
|
|
|
if (array_key_exists('tool_info', $options)) { |
80
|
|
|
$toolInfo = array_merge($toolInfo, $options['tool_info']); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$dom = new \DOMDocument('1.0', 'utf-8'); |
84
|
|
|
$dom->formatOutput = true; |
85
|
|
|
|
86
|
|
|
$xliff = $dom->appendChild($dom->createElement('xliff')); |
87
|
|
|
$xliff->setAttribute('version', '1.2'); |
88
|
|
|
$xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2'); |
89
|
|
|
|
90
|
|
|
$xliffFile = $xliff->appendChild($dom->createElement('file')); |
91
|
|
|
$xliffFile->setAttribute('source-language', str_replace('_', '-', $defaultLocale)); |
92
|
|
|
$xliffFile->setAttribute('target-language', str_replace('_', '-', $messages->getLocale())); |
93
|
|
|
$xliffFile->setAttribute('datatype', 'plaintext'); |
94
|
|
|
$xliffFile->setAttribute('original', 'file.ext'); |
95
|
|
|
|
96
|
|
|
$xliffHead = $xliffFile->appendChild($dom->createElement('header')); |
97
|
|
|
$xliffTool = $xliffHead->appendChild($dom->createElement('tool')); |
98
|
|
|
foreach ($toolInfo as $id => $value) { |
99
|
|
|
$xliffTool->setAttribute($id, $value); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$xliffBody = $xliffFile->appendChild($dom->createElement('body')); |
103
|
|
|
foreach ($messages->all($domain) as $source => $target) { |
104
|
|
|
$translation = $dom->createElement('trans-unit'); |
105
|
|
|
|
106
|
|
|
$translation->setAttribute( |
107
|
|
|
'id', |
108
|
|
|
strtr(substr(base64_encode(hash('sha256', $source, true)), 0, 7), '/+', '._') |
109
|
|
|
); |
110
|
|
|
$translation->setAttribute('resname', $source); |
111
|
|
|
|
112
|
|
|
$s = $translation->appendChild($dom->createElement('source')); |
113
|
|
|
$s->appendChild($dom->createTextNode($source)); |
114
|
|
|
|
115
|
|
|
// Does the target contain characters requiring a CDATA section? |
116
|
|
|
$text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode( |
117
|
|
|
$target |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$targetElement = $dom->createElement('target'); |
121
|
|
|
$metadata = $messages->getMetadata($source, $domain); |
122
|
|
|
if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) { |
123
|
|
|
foreach ($metadata['target-attributes'] as $name => $value) { |
124
|
|
|
$targetElement->setAttribute($name, $value); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
$t = $translation->appendChild($targetElement); |
128
|
|
|
$t->appendChild($text); |
129
|
|
|
|
130
|
|
|
if ($this->hasMetadataArrayInfo('notes', $metadata)) { |
131
|
|
|
foreach ($metadata['notes'] as $note) { |
132
|
|
|
if (!isset($note['content'])) { |
133
|
|
|
continue; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$n = $translation->appendChild($dom->createElement('note')); |
137
|
|
|
$n->appendChild($dom->createTextNode($note['content'])); |
138
|
|
|
|
139
|
|
|
if (isset($note['priority'])) { |
140
|
|
|
$n->setAttribute('priority', $note['priority']); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if (isset($note['from'])) { |
144
|
|
|
$n->setAttribute('from', $note['from']); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$xliffBody->appendChild($translation); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $dom->saveXML(); |
153
|
|
|
} |
154
|
|
|
|
155
|
2 |
|
private function dumpXliff2($defaultLocale, MessageCatalogue $messages, $domain, array $options = []) |
|
|
|
|
156
|
|
|
{ |
157
|
2 |
|
$dom = new \DOMDocument('1.0', 'utf-8'); |
158
|
2 |
|
$dom->formatOutput = true; |
159
|
|
|
|
160
|
2 |
|
$xliff = $dom->appendChild($dom->createElement('xliff')); |
161
|
2 |
|
$xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:2.0'); |
162
|
2 |
|
$xliff->setAttribute('version', '2.0'); |
163
|
2 |
|
$xliff->setAttribute('srcLang', str_replace('_', '-', $defaultLocale)); |
164
|
2 |
|
$xliff->setAttribute('trgLang', str_replace('_', '-', $messages->getLocale())); |
165
|
|
|
|
166
|
2 |
|
$xliffFile = $xliff->appendChild($dom->createElement('file')); |
167
|
2 |
|
$xliffFile->setAttribute('id', $domain.'.'.$messages->getLocale()); |
168
|
|
|
|
169
|
2 |
|
foreach ($messages->all($domain) as $source => $target) { |
170
|
2 |
|
$translation = $dom->createElement('unit'); |
171
|
2 |
|
$translation->setAttribute('id', strtr(substr(base64_encode(hash('sha256', $source, true)), 0, 7), '/+', '._')); |
172
|
2 |
|
$metadata = $messages->getMetadata($source, $domain); |
173
|
|
|
|
174
|
|
|
// Add notes section |
175
|
2 |
|
if ($this->hasMetadataArrayInfo('notes', $metadata)) { |
176
|
1 |
|
$notesElement = $dom->createElement('notes'); |
177
|
1 |
|
foreach ($metadata['notes'] as $note) { |
178
|
1 |
|
$n = $dom->createElement('note'); |
179
|
1 |
|
$n->appendChild($dom->createTextNode(isset($note['content']) ? $note['content'] : '')); |
180
|
1 |
|
unset($note['content']); |
181
|
|
|
|
182
|
1 |
|
foreach ($note as $name => $value) { |
183
|
1 |
|
$n->setAttribute($name, $value); |
184
|
1 |
|
} |
185
|
1 |
|
$notesElement->appendChild($n); |
186
|
1 |
|
} |
187
|
1 |
|
$translation->appendChild($notesElement); |
188
|
1 |
|
} |
189
|
|
|
|
190
|
2 |
|
$segment = $translation->appendChild($dom->createElement('segment')); |
191
|
|
|
|
192
|
2 |
|
$s = $segment->appendChild($dom->createElement('source')); |
193
|
2 |
|
$s->appendChild($dom->createTextNode($source)); |
194
|
|
|
|
195
|
|
|
// Does the target contain characters requiring a CDATA section? |
196
|
2 |
|
$text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode( |
197
|
|
|
$target |
198
|
2 |
|
); |
199
|
|
|
|
200
|
2 |
|
$targetElement = $dom->createElement('target'); |
201
|
2 |
|
if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) { |
202
|
|
|
foreach ($metadata['target-attributes'] as $name => $value) { |
203
|
|
|
$targetElement->setAttribute($name, $value); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
2 |
|
$t = $segment->appendChild($targetElement); |
208
|
2 |
|
$t->appendChild($text); |
209
|
|
|
|
210
|
2 |
|
$xliffFile->appendChild($translation); |
211
|
2 |
|
} |
212
|
|
|
|
213
|
2 |
|
return $dom->saveXML(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param string $key |
218
|
|
|
* @param array|null $metadata |
219
|
|
|
* |
220
|
|
|
* @return bool |
221
|
|
|
*/ |
222
|
2 |
|
private function hasMetadataArrayInfo($key, $metadata = null) |
223
|
|
|
{ |
224
|
2 |
|
return null !== $metadata && |
225
|
2 |
|
array_key_exists($key, $metadata) && |
226
|
2 |
|
($metadata[$key] instanceof \Traversable || is_array($metadata[$key])); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.