1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Webino (http://webino.sk) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/webino/WebinoDraw for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2012-2017 Webino, s. r. o. (http://webino.sk) |
7
|
|
|
* @author Peter Bačinský <[email protected]> |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace WebinoDraw\Draw\Helper; |
12
|
|
|
|
13
|
|
|
use WebinoDraw\Dom\Attr; |
14
|
|
|
use WebinoDraw\Dom\NodeList; |
15
|
|
|
use WebinoDraw\VarTranslator\Translation; |
16
|
|
|
use Zend\I18n\Translator\TranslatorInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Translate |
20
|
|
|
*/ |
21
|
|
|
class Translate extends Element |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Draw helper service name |
25
|
|
|
*/ |
26
|
|
|
const SERVICE = 'webinodrawtranslate'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var TranslatorInterface |
30
|
|
|
*/ |
31
|
|
|
protected $translator; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param TranslatorInterface|object $translator |
35
|
|
|
*/ |
36
|
|
|
public function __construct(TranslatorInterface $translator) |
37
|
|
|
{ |
38
|
|
|
$this->translator = $translator; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param TranslatorInterface $translator |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
|
|
public function setTranslator(TranslatorInterface $translator) |
46
|
|
|
{ |
47
|
|
|
$this->translator = $translator; |
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param NodeList $nodes |
53
|
|
|
* @param array $spec |
54
|
|
|
* @return self |
55
|
|
|
*/ |
56
|
|
|
public function drawNodes(NodeList $nodes, array $spec) |
57
|
|
|
{ |
58
|
|
|
$this->resetVarTranslation($spec); |
59
|
|
|
|
60
|
|
|
$remainNodes = $this->translateAttribNodes( |
61
|
|
|
$nodes, |
62
|
|
|
$this->resolveTextDomain($spec), |
63
|
|
|
$this->resolveLocale($spec) |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
if (empty($remainNodes)) { |
67
|
|
|
// return early when all nodes were attribs |
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return parent::drawNodes($nodes->create($remainNodes), $spec); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $value |
76
|
|
|
* @param Translation $varTranslation |
77
|
|
|
* @param array $spec |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function translateValue($value, Translation $varTranslation, array $spec) |
81
|
|
|
{ |
82
|
|
|
$varValue = trim(parent::translateValue($value, $varTranslation, $spec)); |
83
|
|
|
if (empty($varValue)) { |
84
|
|
|
return ''; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->translator->translate( |
88
|
|
|
$varValue, |
89
|
|
|
$this->resolveTextDomain($spec), |
90
|
|
|
$this->resolveLocale($spec) |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param NodeList $nodes |
96
|
|
|
* @param string $textDomain |
97
|
|
|
* @param string $locale |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
protected function translateAttribNodes(NodeList $nodes, $textDomain, $locale) |
101
|
|
|
{ |
102
|
|
|
$remainNodes = []; |
103
|
|
|
foreach ($nodes as $node) { |
104
|
|
|
if ($node instanceof Attr && !$node->isEmpty()) { |
105
|
|
|
$node->nodeValue = $this->translator->translate($node->nodeValue, $textDomain, $locale); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$remainNodes[] = $node; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $remainNodes; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param array $spec |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
protected function resolveTextDomain(array $spec) |
119
|
|
|
{ |
120
|
|
|
return !empty($spec['text_domain']) |
121
|
|
|
? $this->getVarTranslation()->translateString($spec['text_domain']) |
122
|
|
|
: 'default'; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param array $spec |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
protected function resolveLocale(array $spec) |
130
|
|
|
{ |
131
|
|
|
return !empty($spec['locale']) |
132
|
|
|
? $this->getVarTranslation()->translateString($spec['locale']) |
133
|
|
|
: null; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param array $spec |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
|
|
private function resetVarTranslation(array $spec) |
141
|
|
|
{ |
142
|
|
|
$varTranslation = $this->getVarTranslation(); |
143
|
|
|
|
144
|
|
|
if (!empty($spec['text_domain']) && $varTranslation->containsVar($spec['text_domain'])) { |
145
|
|
|
$this->setVarTranslation(null); |
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if (!empty($spec['locale']) && $varTranslation->containsVar($spec['locale'])) { |
150
|
|
|
$this->setVarTranslation(null); |
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|