Skip to content

Commit

Permalink
[TASK] ViewHelper: Translation ViewHelper
Browse files Browse the repository at this point in the history
Remove inheritance to the TYPO3 translation ViewHelper.

Resolves: #3376
  • Loading branch information
3l73 committed Oct 25, 2022
1 parent 50788bf commit eab2670
Showing 1 changed file with 54 additions and 13 deletions.
67 changes: 54 additions & 13 deletions Classes/ViewHelpers/TranslateViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,72 @@
namespace ApacheSolrForTypo3\Solr\ViewHelpers;

use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper as CoreTranslateViewHelper;
use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

/**
* Class TranslateViewHelper
*
* @author Frans Saris <[email protected]>
* @author Timo Hund <[email protected]>
*/
class TranslateViewHelper extends CoreTranslateViewHelper
class TranslateViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
/**
* @var bool
*/
protected $escapeChildren = true;

/**
* @return string|null
* Register required keys for translation
*
* @noinspection PhpMissingReturnTypeInspection
* @see \TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper::initializeArguments
* @return void
*/
public function render()
public function initializeArguments(): void
{
$result = parent::render();
$result = self::replaceTranslationPrefixesWithAtWithStringMarker($result);
if (is_array($this->arguments['arguments'])) {
$result = vsprintf($result, $this->arguments['arguments']);
$this->registerArgument('key', 'string', 'Translation Key');
$this->registerArgument('id', 'string', 'Translation ID. Same as key.');
$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');
$this->registerArgument('arguments', 'array', 'Arguments to be replaced in the resulting string', false, []);
$this->registerArgument('extensionName', 'string', 'UpperCamelCased extension key (for example BlogExample)');
$this->registerArgument('languageKey', 'string', 'Language key ("dk" for example) or "default" to use. If empty, use current language. Ignored in non-extbase context.');
$this->registerArgument('alternativeLanguageKeys', 'array', 'Alternative language keys if no translation does exist. Ignored in non-extbase context.');
}

/**
* Renders the label
*
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return string
*/
public static function renderStatic(
array $arguments,
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
): string {
$result = self::translateAndReplaceMarkers(
(string)($arguments['key'] ?? $arguments['id']),
(string)($arguments['extensionName'] ?? 'tx_solr'),
$arguments['arguments'],
$arguments['languageKey'],
$arguments['alternativeLanguageKeys']
);
if ($result === null && isset($arguments['default'])) {
$result = $arguments['default'];
$result = self::replaceTranslationPrefixesWithAtWithStringMarker($result);
if (is_array($arguments['arguments'])) {
$result = vsprintf($result, $arguments['arguments']);
}
}

return $result;
return (string)$result;
}

/**
Expand All @@ -69,7 +104,13 @@ public static function translateAndReplaceMarkers(
?string $languageKey = null,
?array $alternativeLanguageKeys = null
): ?string {
$result = LocalizationUtility::translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys);
$result = LocalizationUtility::translate(
$id,
$extensionName,
$arguments,
$languageKey,
$alternativeLanguageKeys
);
$result = self::replaceTranslationPrefixesWithAtWithStringMarker($result);
if (is_array($arguments)) {
$result = vsprintf($result, $arguments);
Expand Down Expand Up @@ -103,12 +144,12 @@ public function compile(
}

/**
* @param $result
* @param mixed $result
* @return mixed
*/
protected static function replaceTranslationPrefixesWithAtWithStringMarker($result)
{
if (strpos((string)$result, '@') !== false) {
if (str_contains((string)$result, '@')) {
$result = preg_replace('~\"?@[a-zA-Z]*\"?~', '%s', $result);
}
return $result;
Expand Down

0 comments on commit eab2670

Please sign in to comment.