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\Converter\Reader; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Config\Resource\FileResource; |
15
|
|
|
use Symfony\Component\Translation\Loader\LoaderInterface; |
16
|
|
|
use Symfony\Component\Translation\MessageCatalogue; |
17
|
|
|
|
18
|
|
|
class JmsReader implements LoaderInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param mixed $resource |
22
|
|
|
* @param string $locale |
23
|
|
|
* @param string $domain |
24
|
|
|
* |
25
|
|
|
* @return MessageCatalogue |
26
|
|
|
*/ |
27
|
1 |
|
public function load($resource, $locale, $domain = 'messages') |
28
|
|
|
{ |
29
|
1 |
|
$previous = libxml_use_internal_errors(true); |
30
|
1 |
|
if (false === $doc = simplexml_load_file($resource)) { |
31
|
|
|
libxml_use_internal_errors($previous); |
32
|
|
|
$libxmlError = libxml_get_last_error(); |
33
|
|
|
|
34
|
|
|
throw new \RuntimeException(sprintf('Could not load XML-file "%s": %s', $resource, $libxmlError->message)); |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
libxml_use_internal_errors($previous); |
38
|
|
|
|
39
|
1 |
|
$doc->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2'); |
40
|
1 |
|
$doc->registerXPathNamespace('jms', 'urn:jms:translation'); |
41
|
|
|
|
42
|
1 |
|
$hasReferenceFiles = in_array('urn:jms:translation', $doc->getNamespaces(true)); |
43
|
|
|
|
44
|
1 |
|
$catalogue = new MessageCatalogue($locale); |
45
|
1 |
|
$catalogue->addResource(new FileResource($resource)); |
46
|
|
|
|
47
|
|
|
/** @var \SimpleXMLElement $trans */ |
48
|
1 |
|
foreach ($doc->xpath('//xliff:trans-unit') as $trans) { |
49
|
1 |
|
$id = ($resName = (string) $trans->attributes()->resname) |
50
|
1 |
|
? $resName : (string) $trans->source; |
51
|
|
|
|
52
|
1 |
|
if (empty($id)) { |
53
|
|
|
continue; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
$meta = []; |
57
|
1 |
|
if ($hasReferenceFiles) { |
58
|
1 |
|
foreach ($trans->xpath('./jms:reference-file') as $file) { |
59
|
1 |
|
$line = (string) $file->attributes()->line; |
60
|
|
|
|
61
|
1 |
|
$meta['notes'][] = ['category' => 'file-source', 'content' => sprintf('%s:%s', (string) $file, $line ? (int) $line : 0)]; |
62
|
1 |
|
} |
63
|
1 |
|
} |
64
|
|
|
|
65
|
1 |
|
if ($meaning = (string) $trans->attributes()->extradata) { |
66
|
1 |
|
if (0 === strpos($meaning, 'Meaning: ')) { |
67
|
1 |
|
$meaning = substr($meaning, 9); |
68
|
1 |
|
} |
69
|
|
|
|
70
|
1 |
|
$meta['notes'][] = ['category' => 'meaning', 'content' => $meaning]; |
71
|
1 |
|
} |
72
|
1 |
|
if ($approved = (string) $trans->attributes()->approved) { |
73
|
1 |
|
$text = (string) $approved; |
74
|
1 |
|
$meta['notes'][] = ['category' => 'approved', 'content' => 'yes' == $text || 'true' == $text ? 'true' : 'false']; |
75
|
1 |
|
} |
76
|
|
|
|
77
|
1 |
|
foreach ($trans->target->attributes() as $name => $value) { |
78
|
1 |
|
if ('state' === $name) { |
79
|
1 |
|
$meta['notes'][] = ['category' => 'state', 'content' => (string) $value]; |
80
|
1 |
|
} |
81
|
1 |
|
} |
82
|
|
|
|
83
|
1 |
|
$catalogue->set($id, $trans->target, $domain); |
84
|
1 |
|
$catalogue->setMetadata($id, $meta, $domain); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
1 |
|
return $catalogue; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|