Commit bb2fca15 authored by Oliver Bartsch's avatar Oliver Bartsch
Browse files

[BUGFIX] Resolve default value in f:translate on time

Some components in the Core allow to either define
locallang references or static labels. To process them
in a template, usually the f:translate ViewHelper is used.

Since the value will be dynamically and a static label
as "key" will lead to an exception in non extbase context,
the dynamic value must also be defined as the default,
which will be used by the ViewHelper, in case the "key"
can not be resolved.

There are two possibilities to define the default. Either
by setting the "default" attribute or by adding the value
as tag content. Latter failed since #96473 because the
tag content was resolved too late.

The previous behaviour is now restored by evaluating
a possible default value before processing the "key".

Resolves: #96964
Related: #96473
Releases: main
Change-Id: If932d2de14b8b58005051a03ccf7be290b66ddd9
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/73599

Tested-by: core-ci's avatarcore-ci <typo3@b13.com>
Tested-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Tested-by: Stefan Bürk's avatarStefan Bürk <stefan@buerk.tech>
Tested-by: Oliver Bartsch's avatarOliver Bartsch <bo@cedev.de>
Reviewed-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Stefan Bürk's avatarStefan Bürk <stefan@buerk.tech>
Reviewed-by: Oliver Bartsch's avatarOliver Bartsch <bo@cedev.de>
parent 302cd4e3
......@@ -133,7 +133,7 @@ final class TranslateViewHelper extends AbstractViewHelper
{
$key = $arguments['key'];
$id = $arguments['id'];
$default = $arguments['default'];
$default = (string)($arguments['default'] ?? $renderChildrenClosure() ?? '');
$extensionName = $arguments['extensionName'];
$translateArguments = $arguments['arguments'];
......@@ -169,8 +169,10 @@ final class TranslateViewHelper extends AbstractViewHelper
$id = 'LLL:EXT:' . GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName) . '/Resources/Private/Language/locallang.xlf:' . $id;
}
$value = self::getLanguageService()->sL($id);
if (empty($value)) {
$value = $default ?? $renderChildrenClosure() ?? '';
if (empty($value) || (!str_starts_with($id, 'LLL:EXT:') && $value === $id)) {
// In case $value is empty (LLL: could not be resolved) or $value
// is the same as $id and is no "LLL:", fall back to the default.
$value = $default;
}
if (!empty($translateArguments)) {
$value = vsprintf($value, $translateArguments);
......@@ -192,12 +194,11 @@ final class TranslateViewHelper extends AbstractViewHelper
$value = null;
}
if ($value === null) {
$value = $default ?? $renderChildrenClosure();
$value = $default;
if (!empty($translateArguments)) {
$value = vsprintf($value, $translateArguments);
}
}
$value = $value ?? '';
return $value;
}
......
......@@ -61,10 +61,18 @@ class TranslateViewHelperTest extends FunctionalTestCase
'<f:translate key="LLL:EXT:backend/Resources/Private/Language/locallang.xlf:iDoNotExist" default="myDefault" />',
'myDefault',
],
'fallback to default attribute for static label' => [
'<f:translate key="static label" default="myDefault" />',
'myDefault',
],
'fallback to child for not existing label' => [
'<f:translate key="LLL:EXT:backend/Resources/Private/Language/locallang.xlf:iDoNotExist">myDefault</f:translate>',
'myDefault',
],
'fallback to child for static label' => [
'<f:translate key="static label">myDefault</f:translate>',
'myDefault',
],
'id and underscored extensionName given' => [
'<f:translate key="form.legend" extensionName="indexed_search" />',
'Search form',
......@@ -97,6 +105,7 @@ class TranslateViewHelperTest extends FunctionalTestCase
}
/**
* @test
* @dataProvider renderReturnsStringInNonExtbaseContextDataProvider
*/
public function renderReturnsStringInNonExtbaseContext(string $template, string $expected): void
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment