1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\ViewHelpers; |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of the TYPO3 CMS project. |
6
|
|
|
* |
7
|
|
|
* It is free software; you can redistribute it and/or modify it under |
8
|
|
|
* the terms of the GNU General Public License, either version 2 |
9
|
|
|
* of the License, or any later version. |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please read the |
12
|
|
|
* LICENSE.txt file that was distributed with this source code. |
13
|
|
|
* |
14
|
|
|
* The TYPO3 project - inspiring people to share! |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
use TYPO3\CMS\Extbase\Utility\LocalizationUtility; |
18
|
|
|
use TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper as CoreTranslateViewHelper; |
19
|
|
|
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode; |
20
|
|
|
use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class TranslateViewHelper |
24
|
|
|
* |
25
|
|
|
* @author Frans Saris <[email protected]> |
26
|
|
|
* @author Timo Hund <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class TranslateViewHelper extends CoreTranslateViewHelper |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
protected $escapeChildren = true; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return string|null |
37
|
|
|
*/ |
38
|
28 |
|
public function render() |
39
|
|
|
{ |
40
|
28 |
|
$result = parent::render(); |
41
|
28 |
|
$result = self::replaceTranslationPrefixesWithAtWithStringMarker($result); |
42
|
28 |
|
if (is_array($this->arguments['arguments'])) { |
43
|
25 |
|
$result = vsprintf($result, $this->arguments['arguments']); |
44
|
|
|
} |
45
|
|
|
|
46
|
28 |
|
return $result; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Wrapper call to static LocalizationUtility |
51
|
|
|
* |
52
|
|
|
* @param string $id Translation Key compatible to TYPO3 Flow |
53
|
|
|
* @param string $extensionName UpperCamelCased extension key (for example BlogExample) |
54
|
|
|
* @param array $arguments Arguments to be replaced in the resulting string |
55
|
|
|
* @param string $languageKey Language key to use for this translation |
56
|
|
|
* @param string[] $alternativeLanguageKeys Alternative language keys if no translation does exist |
57
|
|
|
* |
58
|
|
|
* @return string|null |
59
|
|
|
*/ |
60
|
3 |
|
public static function translateAndReplaceMarkers($id, $extensionName = 'solr', $arguments = null, $languageKey = null, $alternativeLanguageKeys = null) |
61
|
|
|
{ |
62
|
3 |
|
$result = LocalizationUtility::translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys); |
63
|
3 |
|
$result = self::replaceTranslationPrefixesWithAtWithStringMarker($result); |
64
|
3 |
|
if (is_array($arguments)) { |
65
|
3 |
|
$result = vsprintf($result, $arguments); |
|
|
|
|
66
|
|
|
} |
67
|
3 |
|
return $result; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $argumentsName |
72
|
|
|
* @param string $closureName |
73
|
|
|
* @param string $initializationPhpCode |
74
|
|
|
* @param ViewHelperNode $node |
75
|
|
|
* @param TemplateCompiler $compiler |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
29 |
|
public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler) |
79
|
|
|
{ |
80
|
29 |
|
return sprintf( |
81
|
29 |
|
'\\%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()', |
82
|
29 |
|
static::class, |
83
|
|
|
$argumentsName, |
84
|
|
|
$closureName |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $result |
90
|
|
|
* @return mixed |
91
|
|
|
*/ |
92
|
28 |
|
protected static function replaceTranslationPrefixesWithAtWithStringMarker($result) |
93
|
|
|
{ |
94
|
28 |
|
if (strpos($result, '@') !== false) { |
95
|
25 |
|
$result = preg_replace('~\"?@[a-zA-Z]*\"?~', '%s', $result); |
96
|
|
|
} |
97
|
28 |
|
return $result; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|