1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Translation\PlatformAdapter\PhraseApp\Bridge\Symfony; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Translation\Dumper\FileDumper; |
6
|
|
|
use Symfony\Component\Translation\MessageCatalogue; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Custom XliffFileDumper for use with phrase app |
10
|
|
|
* This class can be removed, as soon as https://github.com/symfony/symfony/pull/21442 is merged |
11
|
|
|
*/ |
12
|
|
|
class XliffFileDumper extends FileDumper |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* {@inheritdoc} |
16
|
|
|
*/ |
17
|
|
|
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array()) |
18
|
|
|
{ |
19
|
|
|
$xliffVersion = '1.2'; |
20
|
|
|
if (array_key_exists('xliff_version', $options)) { |
21
|
|
|
$xliffVersion = $options['xliff_version']; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if (array_key_exists('default_locale', $options)) { |
25
|
|
|
$defaultLocale = $options['default_locale']; |
26
|
|
|
} else { |
27
|
|
|
$defaultLocale = \Locale::getDefault(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if ('1.2' === $xliffVersion) { |
31
|
|
|
return $this->dumpXliff1($defaultLocale, $messages, $domain, $options); |
32
|
|
|
} |
33
|
|
|
if ('2.0' === $xliffVersion) { |
34
|
|
|
return $this->dumpXliff2($defaultLocale, $messages, $domain, $options); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
throw new \InvalidArgumentException(sprintf('No support implemented for dumping XLIFF version "%s".', $xliffVersion)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
protected function format(MessageCatalogue $messages, $domain) |
44
|
|
|
{ |
45
|
|
|
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0. Use the formatCatalogue() method instead.', E_USER_DEPRECATED); |
|
|
|
|
46
|
|
|
|
47
|
|
|
return $this->formatCatalogue($messages, $domain); |
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
|
|
|
// changed for phrase app |
89
|
|
|
$translation->setAttribute('id', sha1("$domain::$source")); |
90
|
|
|
$translation->setAttribute('resname', $source); |
91
|
|
|
|
92
|
|
|
$s = $translation->appendChild($dom->createElement('source')); |
93
|
|
|
$s->appendChild($dom->createTextNode($source)); |
94
|
|
|
|
95
|
|
|
// Does the target contain characters requiring a CDATA section? |
96
|
|
|
$text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target); |
97
|
|
|
|
98
|
|
|
$targetElement = $dom->createElement('target'); |
99
|
|
|
$metadata = $messages->getMetadata($source, $domain); |
100
|
|
|
if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) { |
101
|
|
|
foreach ($metadata['target-attributes'] as $name => $value) { |
102
|
|
|
$targetElement->setAttribute($name, $value); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
$t = $translation->appendChild($targetElement); |
106
|
|
|
$t->appendChild($text); |
107
|
|
|
|
108
|
|
|
if ($this->hasMetadataArrayInfo('notes', $metadata)) { |
109
|
|
|
foreach ($metadata['notes'] as $note) { |
110
|
|
|
if (!isset($note['content'])) { |
111
|
|
|
continue; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$n = $translation->appendChild($dom->createElement('note')); |
115
|
|
|
$n->appendChild($dom->createTextNode($note['content'])); |
116
|
|
|
|
117
|
|
|
if (isset($note['priority'])) { |
118
|
|
|
$n->setAttribute('priority', $note['priority']); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (isset($note['from'])) { |
122
|
|
|
$n->setAttribute('from', $note['from']); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$xliffBody->appendChild($translation); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $dom->saveXML(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
private function dumpXliff2($defaultLocale, MessageCatalogue $messages, $domain, array $options = array()) |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
$dom = new \DOMDocument('1.0', 'utf-8'); |
136
|
|
|
$dom->formatOutput = true; |
137
|
|
|
|
138
|
|
|
$xliff = $dom->appendChild($dom->createElement('xliff')); |
139
|
|
|
$xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:2.0'); |
140
|
|
|
$xliff->setAttribute('version', '2.0'); |
141
|
|
|
$xliff->setAttribute('srcLang', str_replace('_', '-', $defaultLocale)); |
142
|
|
|
$xliff->setAttribute('trgLang', str_replace('_', '-', $messages->getLocale())); |
143
|
|
|
|
144
|
|
|
$xliffFile = $xliff->appendChild($dom->createElement('file')); |
145
|
|
|
$xliffFile->setAttribute('id', $domain.'.'.$messages->getLocale()); |
146
|
|
|
|
147
|
|
|
foreach ($messages->all($domain) as $source => $target) { |
148
|
|
|
$translation = $dom->createElement('unit'); |
149
|
|
|
|
150
|
|
|
// changed for phrase app |
151
|
|
|
$translation->setAttribute('id', sha1("$domain::$source")); |
152
|
|
|
|
153
|
|
|
$segment = $translation->appendChild($dom->createElement('segment')); |
154
|
|
|
|
155
|
|
|
$s = $segment->appendChild($dom->createElement('source')); |
156
|
|
|
$s->appendChild($dom->createTextNode($source)); |
157
|
|
|
|
158
|
|
|
// Does the target contain characters requiring a CDATA section? |
159
|
|
|
$text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target); |
160
|
|
|
|
161
|
|
|
$targetElement = $dom->createElement('target'); |
162
|
|
|
$metadata = $messages->getMetadata($source, $domain); |
163
|
|
|
if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) { |
164
|
|
|
foreach ($metadata['target-attributes'] as $name => $value) { |
165
|
|
|
$targetElement->setAttribute($name, $value); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
$t = $segment->appendChild($targetElement); |
169
|
|
|
$t->appendChild($text); |
170
|
|
|
|
171
|
|
|
$xliffFile->appendChild($translation); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $dom->saveXML(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string $key |
179
|
|
|
* @param array|null $metadata |
180
|
|
|
* |
181
|
|
|
* @return bool |
182
|
|
|
*/ |
183
|
|
|
private function hasMetadataArrayInfo($key, $metadata = null) |
184
|
|
|
{ |
185
|
|
|
return null !== $metadata && array_key_exists($key, $metadata) && ($metadata[$key] instanceof \Traversable || is_array($metadata[$key])); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: