1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the TYPO3 CMS project. |
7
|
|
|
* |
8
|
|
|
* It is free software; you can redistribute it and/or modify it under |
9
|
|
|
* the terms of the GNU General Public License, either version 2 |
10
|
|
|
* of the License, or any later version. |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please read the |
13
|
|
|
* LICENSE.txt file that was distributed with this source code. |
14
|
|
|
* |
15
|
|
|
* The TYPO3 project - inspiring people to share! |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace ApacheSolrForTypo3\Solr\ViewHelpers; |
19
|
|
|
|
20
|
|
|
use TYPO3\CMS\Extbase\Utility\LocalizationUtility; |
21
|
|
|
use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler; |
22
|
|
|
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode; |
23
|
|
|
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; |
24
|
|
|
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; |
25
|
|
|
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class TranslateViewHelper |
29
|
|
|
* |
30
|
|
|
* @author Frans Saris <[email protected]> |
31
|
|
|
* @author Timo Hund <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class TranslateViewHelper extends AbstractViewHelper |
34
|
|
|
{ |
35
|
|
|
use CompileWithRenderStatic; |
36
|
|
|
/** |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
protected $escapeChildren = true; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Register required keys for translation |
43
|
|
|
* |
44
|
|
|
* @see \TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper::initializeArguments |
45
|
|
|
*/ |
46
|
|
|
public function initializeArguments(): void |
47
|
|
|
{ |
48
|
|
|
$this->registerArgument('key', 'string', 'Translation Key'); |
49
|
|
|
$this->registerArgument('id', 'string', 'Translation ID. Same as key.'); |
50
|
|
|
$this->registerArgument('default', 'string', 'If the given locallang key could not be found, this value is used. If this argument is not set, child nodes will be used to render the default'); |
51
|
|
|
$this->registerArgument('arguments', 'array', 'Arguments to be replaced in the resulting string', false, []); |
52
|
|
|
$this->registerArgument('extensionName', 'string', 'UpperCamelCased extension key (for example BlogExample)'); |
53
|
|
|
$this->registerArgument('languageKey', 'string', 'Language key ("dk" for example) or "default" to use. If empty, use current language. Ignored in non-extbase context.'); |
54
|
|
|
$this->registerArgument('alternativeLanguageKeys', 'array', 'Alternative language keys if no translation does exist. Ignored in non-extbase context.'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Renders the label |
59
|
|
|
* |
60
|
|
|
* @param array $arguments |
61
|
|
|
* @param \Closure $renderChildrenClosure |
62
|
|
|
* @param RenderingContextInterface $renderingContext |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public static function renderStatic( |
66
|
|
|
array $arguments, |
67
|
|
|
\Closure $renderChildrenClosure, |
68
|
|
|
RenderingContextInterface $renderingContext |
69
|
|
|
): string { |
70
|
|
|
$result = self::translateAndReplaceMarkers( |
71
|
|
|
(string)($arguments['key'] ?? $arguments['id']), |
72
|
|
|
(string)($arguments['extensionName'] ?? 'tx_solr'), |
73
|
|
|
$arguments['arguments'], |
74
|
|
|
$arguments['languageKey'], |
75
|
|
|
$arguments['alternativeLanguageKeys'] |
76
|
|
|
); |
77
|
|
|
if ($result === null && isset($arguments['default'])) { |
78
|
|
|
$result = $arguments['default']; |
79
|
|
|
$result = self::replaceTranslationPrefixesWithAtWithStringMarker($result); |
80
|
|
|
if (is_array($arguments['arguments'])) { |
81
|
|
|
$result = vsprintf($result, $arguments['arguments']); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return (string)$result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Wrapper call to static LocalizationUtility |
90
|
|
|
* |
91
|
|
|
* @param string $id Translation Key compatible to TYPO3 Flow |
92
|
|
|
* @param string $extensionName UpperCamelCased extension key (for example BlogExample) |
93
|
|
|
* @param array|null $arguments Arguments to be replaced in the resulting string |
94
|
|
|
* @param string|null $languageKey Language key to use for this translation |
95
|
|
|
* @param string[]|null $alternativeLanguageKeys Alternative language keys if no translation does exist |
96
|
|
|
* |
97
|
|
|
* @return string|null |
98
|
|
|
*/ |
99
|
|
|
public static function translateAndReplaceMarkers( |
100
|
|
|
string $id, |
101
|
|
|
string $extensionName = 'solr', |
102
|
|
|
?array $arguments = null, |
103
|
|
|
?string $languageKey = null, |
104
|
|
|
?array $alternativeLanguageKeys = null |
105
|
|
|
): ?string { |
106
|
|
|
$result = LocalizationUtility::translate( |
107
|
|
|
$id, |
108
|
|
|
$extensionName, |
109
|
|
|
$arguments, |
110
|
|
|
$languageKey, |
111
|
|
|
$alternativeLanguageKeys |
112
|
|
|
); |
113
|
|
|
$result = self::replaceTranslationPrefixesWithAtWithStringMarker($result); |
114
|
|
|
if (is_array($arguments)) { |
115
|
|
|
$result = vsprintf($result, $arguments); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
return $result; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $argumentsName |
122
|
|
|
* @param string $closureName |
123
|
|
|
* @param string $initializationPhpCode |
124
|
|
|
* @param ViewHelperNode $node |
125
|
|
|
* @param TemplateCompiler $compiler |
126
|
|
|
* @return string |
127
|
|
|
* |
128
|
|
|
* @noinspection PhpMissingReturnTypeInspection |
129
|
|
|
*/ |
130
|
|
|
public function compile( |
131
|
|
|
$argumentsName, |
132
|
|
|
$closureName, |
133
|
|
|
&$initializationPhpCode, |
134
|
|
|
ViewHelperNode $node, |
135
|
|
|
TemplateCompiler $compiler |
136
|
|
|
) { |
137
|
|
|
return sprintf( |
138
|
|
|
'\\%1$s::translateAndReplaceMarkers(%2$s[\'key\'] ?? %2$s[\'id\'], %2$s[\'extensionName\'] ?? $renderingContext->getControllerContext()->getRequest()->getControllerExtensionName(), %2$s[\'arguments\'], %2$s[\'languageKey\'], %2$s[\'alternativeLanguageKeys\']) ?? %2$s[\'default\'] ?? %3$s()', |
139
|
|
|
static::class, |
140
|
|
|
$argumentsName, |
141
|
|
|
$closureName |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param mixed $result |
147
|
|
|
* @return mixed |
148
|
|
|
*/ |
149
|
|
|
protected static function replaceTranslationPrefixesWithAtWithStringMarker($result) |
150
|
|
|
{ |
151
|
|
|
if (str_contains((string)$result, '@')) { |
152
|
|
|
$result = preg_replace('~\"?@[a-zA-Z]*\"?~', '%s', $result); |
153
|
|
|
} |
154
|
|
|
return $result; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|