forked from phptal/PHPTAL
/
Translate.php
193 lines (179 loc) · 6.36 KB
/
Translate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
declare(strict_types=1);
/**
* PHPTAL templating engine
*
* @category HTML
* @package PHPTAL
* @author Laurent Bedubourg <lbedubourg@motion-twin.com>
* @author Kornel Lesiński <kornel@aardvarkmedia.co.uk>
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
* @link http://phptal.org/
*/
namespace PhpTal\Php\Attribute\I18N;
use PhpTal\Dom\Attr;
use PhpTal\Dom\Element;
use PhpTal\Dom\Node;
use PhpTal\Dom\Text;
use PhpTal\Exception\TemplateException;
use PhpTal\Php\Attribute;
use PhpTal\Php\Attribute\TAL\Content;
use PhpTal\Php\CodeWriter;
use PhpTal\Php\TalesChainExecutor;
use PhpTal\TalNamespace\Builtin;
/**
* ZPTInternationalizationSupport
*
* i18n:translate
*
* This attribute is used to mark units of text for translation. If this
* attribute is specified with an empty string as the value, the message ID
* is computed from the content of the element bearing this attribute.
* Otherwise, the value of the element gives the message ID.
*
*
* @package PHPTAL
*/
class Translate extends Content
{
/**
* @param CodeWriter $codewriter
*
* @return void
*
* @throws TemplateException
* @throws \PhpTal\Exception\ConfigurationException
* @throws \PhpTal\Exception\ParserException
* @throws \PhpTal\Exception\PhpNotAllowedException
* @throws \PhpTal\Exception\PhpTalException
* @throws \PhpTal\Exception\UnknownModifierException
* @throws \ReflectionException
*/
public function before(CodeWriter $codewriter): void
{
$escape = true;
$this->echoType = Attribute::ECHO_TEXT;
if (preg_match('/^(text|structure)(?:\s+(.*)|\s*$)/', $this->expression, $m)) {
if ($m[1] === 'structure') {
$escape = false;
$this->echoType = Attribute::ECHO_STRUCTURE;
}
$this->expression = $m[2] ?? '';
}
$this->prepareNames($codewriter, $this->phpelement);
// if no expression is given, the content of the node is used as
// a translation key
if (trim($this->expression) === '') {
$key = $this->getTranslationKey($this->phpelement, !$escape, $codewriter->getEncoding());
$key = trim(preg_replace('/\s+/sm' . ($codewriter->getEncoding() === 'UTF-8' ? 'u' : ''), ' ', $key));
if (trim($key) === '') {
throw new TemplateException(
'Empty translation key',
$this->phpelement->getSourceFile(),
$this->phpelement->getSourceLine()
);
}
$code = $codewriter->str($key);
} else {
$code = $codewriter->evaluateExpression($this->expression);
if (is_array($code)) {
$this->generateChainedContent($codewriter, $code);
return;
}
$code = $codewriter->evaluateExpression($this->expression);
}
$codewriter->pushCode(
'echo ' . $codewriter->getTranslatorReference() . '->translate(' . $code . ',' . ($escape ? 'true' : 'false') . ');'
);
}
/**
* @param CodeWriter $codewriter
* @return void
*/
public function after(CodeWriter $codewriter): void
{
}
/**
* @param TalesChainExecutor $executor
* @param mixed $expression
* @param bool $islast
*
* @return void
* @throws \PhpTal\Exception\ConfigurationException
* @throws \PhpTal\Exception\PhpTalException
*/
public function talesChainPart(TalesChainExecutor $executor, string $expression, bool $islast): void
{
$codewriter = $executor->getCodeWriter();
$escape = Attribute::ECHO_STRUCTURE !== $this->echoType;
$expression = $codewriter->getTranslatorReference() . "->translate($expression, " . ($escape ? 'true' : 'false') . ')';
if (!$islast) {
$var = $codewriter->createTempVariable();
$executor->doIf('!\PhpTal\Helper::phptal_isempty(' . $var . ' = ' . $expression . ')');
$codewriter->pushCode("echo $var");
$codewriter->recycleTempVariable($var);
} else {
$executor->doElse();
$codewriter->pushCode("echo $expression");
}
}
/**
* @param Node|Element $tag
* @param bool $preserve_tags
* @param string $encoding
*
* @return string
*/
private function getTranslationKey(Node $tag, bool $preserve_tags, string $encoding): string
{
$result = '';
foreach ($tag->childNodes as $child) {
if ($child instanceof Text) {
if ($preserve_tags) {
$result .= $child->getValueEscaped();
} else {
$result .= $child->getValue();
}
} elseif ($child instanceof Element) {
$attr = $child->getAttributeNodeNS(Builtin::NS_I18N, 'name');
if ($attr) {
$result .= '${' . $attr->getValue() . '}';
} elseif ($preserve_tags) {
$result .= '<' . $child->getQualifiedName();
foreach ($child->getAttributeNodes() as $attr) {
if ($attr->getReplacedState() === Attr::HIDDEN) {
continue;
}
$result .= ' ' . $attr->getQualifiedName() . '="' . $attr->getValueEscaped() . '"';
}
$result .= '>' . $this->getTranslationKey($child, $preserve_tags, $encoding) .
'</' . $child->getQualifiedName() . '>';
} else {
$result .= $this->getTranslationKey($child, $preserve_tags, $encoding);
}
}
}
return $result;
}
/**
* @param CodeWriter $codewriter
* @param Node|Element $tag
*
* @return void
*
* @throws \PhpTal\Exception\PhpTalException
* @throws TemplateException
*/
private function prepareNames(CodeWriter $codewriter, Node $tag): void
{
foreach ($tag->childNodes as $child) {
if ($child instanceof Element) {
if ($child->hasAttributeNS(Builtin::NS_I18N, 'name')) {
$child->generateCode($codewriter);
} else {
$this->prepareNames($codewriter, $child);
}
}
}
}
}