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\Port; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Translation\MessageCatalogue; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* This code is moved from the Symfony 3.4 repo. It will be removed when |
18
|
|
|
* we drop support for Symfony 3.3 and below. |
19
|
|
|
* |
20
|
|
|
* @author Tobias Nyholm <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class SymfonyPort |
23
|
|
|
{ |
24
|
2 |
|
public function dumpXliff2($defaultLocale, MessageCatalogue $messages, $domain, array $options = []) |
|
|
|
|
25
|
|
|
{ |
26
|
2 |
|
$dom = new \DOMDocument('1.0', 'utf-8'); |
27
|
2 |
|
$dom->formatOutput = true; |
28
|
|
|
|
29
|
2 |
|
$xliff = $dom->appendChild($dom->createElement('xliff')); |
30
|
2 |
|
$xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:2.0'); |
31
|
2 |
|
$xliff->setAttribute('version', '2.0'); |
32
|
2 |
|
$xliff->setAttribute('srcLang', str_replace('_', '-', $defaultLocale)); |
33
|
2 |
|
$xliff->setAttribute('trgLang', str_replace('_', '-', $messages->getLocale())); |
34
|
|
|
|
35
|
2 |
|
$xliffFile = $xliff->appendChild($dom->createElement('file')); |
36
|
2 |
|
$xliffFile->setAttribute('id', $domain.'.'.$messages->getLocale()); |
37
|
|
|
|
38
|
2 |
|
foreach ($messages->all($domain) as $source => $target) { |
39
|
2 |
|
$translation = $dom->createElement('unit'); |
40
|
2 |
|
$translation->setAttribute('id', strtr(substr(base64_encode(hash('sha256', $source, true)), 0, 7), '/+', '._')); |
41
|
2 |
|
$metadata = $messages->getMetadata($source, $domain); |
42
|
|
|
|
43
|
|
|
// Add notes section |
44
|
2 |
|
if ($this->hasMetadataArrayInfo('notes', $metadata)) { |
45
|
1 |
|
$notesElement = $dom->createElement('notes'); |
46
|
1 |
|
foreach ($metadata['notes'] as $note) { |
47
|
1 |
|
$n = $dom->createElement('note'); |
48
|
1 |
|
$n->appendChild($dom->createTextNode(isset($note['content']) ? $note['content'] : '')); |
49
|
1 |
|
unset($note['content']); |
50
|
|
|
|
51
|
1 |
|
foreach ($note as $name => $value) { |
52
|
1 |
|
$n->setAttribute($name, $value); |
53
|
1 |
|
} |
54
|
1 |
|
$notesElement->appendChild($n); |
55
|
1 |
|
} |
56
|
1 |
|
$translation->appendChild($notesElement); |
57
|
1 |
|
} |
58
|
|
|
|
59
|
2 |
|
$segment = $translation->appendChild($dom->createElement('segment')); |
60
|
|
|
|
61
|
2 |
|
$s = $segment->appendChild($dom->createElement('source')); |
62
|
2 |
|
$s->appendChild($dom->createTextNode($source)); |
63
|
|
|
|
64
|
|
|
// Does the target contain characters requiring a CDATA section? |
65
|
2 |
|
$text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode( |
66
|
|
|
$target |
67
|
2 |
|
); |
68
|
|
|
|
69
|
2 |
|
$targetElement = $dom->createElement('target'); |
70
|
2 |
|
if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) { |
71
|
|
|
foreach ($metadata['target-attributes'] as $name => $value) { |
72
|
|
|
$targetElement->setAttribute($name, $value); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
$t = $segment->appendChild($targetElement); |
77
|
2 |
|
$t->appendChild($text); |
78
|
|
|
|
79
|
2 |
|
$xliffFile->appendChild($translation); |
80
|
2 |
|
} |
81
|
|
|
|
82
|
2 |
|
return $dom->saveXML(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $key |
87
|
|
|
* @param array|null $metadata |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
2 |
|
private function hasMetadataArrayInfo($key, $metadata = null) |
92
|
|
|
{ |
93
|
2 |
|
return null !== $metadata && |
94
|
2 |
|
array_key_exists($key, $metadata) && |
95
|
2 |
|
($metadata[$key] instanceof \Traversable || is_array($metadata[$key])); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.