Neos\FluidAdaptor\ViewHelpers\TranslateViewHelper::render PHP Method

render() public method

Replaces all placeholders with corresponding values if they exist in the translated label.
public render ( string $id = null, string $value = null, array $arguments = [], string $source = 'Main', string $package = null, mixed $quantity = null, string $locale = null ) : string
$id string Id to use for finding translation (trans-unit id in XLIFF)
$value string If $key is not specified or could not be resolved, this value is used. If this argument is not set, child nodes will be used to render the default
$arguments array Numerically indexed array of values to be inserted into placeholders
$source string Name of file with translations (use / as a directory separator)
$package string Target package key. If not set, the current package key will be used
$quantity mixed A number to find plural form for (float or int), NULL to not use plural forms
$locale string An identifier of locale to use (NULL for use the default locale)
return string Translated label or source label / ID key
    public function render($id = null, $value = null, array $arguments = array(), $source = 'Main', $package = null, $quantity = null, $locale = null)
    {
        $localeObject = null;
        if ($locale !== null) {
            try {
                $localeObject = new Locale($locale);
            } catch (InvalidLocaleIdentifierException $e) {
                throw new ViewHelperException(sprintf('"%s" is not a valid locale identifier.', $locale), 1279815885);
            }
        }
        if ($package === null) {
            $request = $this->renderingContext->getControllerContext()->getRequest();
            if ($request instanceof ActionRequest) {
                $package = $request->getControllerPackageKey();
            }
            if ($package === null) {
                throw new ViewHelperException('The current package key can\'t be resolved. Make sure to initialize the Fluid view with a proper ActionRequest and/or specify the "package" argument when using the f:translate ViewHelper', 1416832309);
            }
        }
        $originalLabel = $value === null ? $this->renderChildren() : $value;
        if ($id === null) {
            return (string) $this->translator->translateByOriginalLabel($originalLabel, $arguments, $quantity, $localeObject, $source, $package);
        }
        $translation = $this->translator->translateById($id, $arguments, $quantity, $localeObject, $source, $package);
        if ($translation !== null) {
            return (string) $translation;
        }
        if ($originalLabel !== null) {
            return $originalLabel;
        }
        return (string) $id;
    }

Usage Example

 /**
  * @test
  * @dataProvider translationFallbackDataProvider
  * @param string $id
  * @param string $value
  * @param string $translatedId
  * @param string $translatedValue
  * @param string $expectedResult
  */
 public function translationFallbackTests($id, $value, $translatedId, $translatedValue, $expectedResult)
 {
     $this->mockTranslator->expects($this->any())->method('translateById', $id)->will($this->returnValue($translatedId));
     $this->mockTranslator->expects($this->any())->method('translateByOriginalLabel', $value)->will($this->returnValue($translatedValue));
     $actualResult = $this->translateViewHelper->render($id, $value);
     $this->assertSame($expectedResult, $actualResult);
 }
All Usage Examples Of Neos\FluidAdaptor\ViewHelpers\TranslateViewHelper::render
TranslateViewHelper