/**
  * Renders the translated label.
  *
  * Replaces all placeholders with corresponding values if they exist in the
  * translated label.
  *
  * @param string $id Id to use for finding translation (trans-unit id in XLIFF)
  * @param string $value 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
  * @param array $arguments Numerically indexed array of values to be inserted into placeholders
  * @param string $source Name of file with translations
  * @param string $package Target package key. If not set, the current package key will be used
  * @param mixed $quantity A number to find plural form for (float or int), NULL to not use plural forms
  * @param string $locale An identifier of locale to use (NULL for use the default locale)
  * @return string Translated label or source label / ID key
  * @throws ViewHelper\Exception
  */
 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 ViewHelper\Exception('"' . $locale . '" is not a valid locale identifier.', 1279815885);
         }
     }
     if ($package === NULL) {
         $package = $this->controllerContext->getRequest()->getControllerPackageKey();
     }
     $originalLabel = $value === NULL ? $this->renderChildren() : $value;
     if ($id === NULL) {
         return $this->translator->translateByOriginalLabel($originalLabel, $arguments, $quantity, $localeObject, $source, $package);
     } else {
         $translation = $this->translator->translateById($id, $arguments, $quantity, $localeObject, $source, $package);
         if ($translation === $id) {
             if ($originalLabel) {
                 return $this->translator->translateByOriginalLabel($originalLabel, $arguments, $quantity, $localeObject, $source, $package);
             } else {
                 return $id;
             }
         } else {
             return $translation;
         }
     }
 }
 /**
  * Renders the translated label.
  * Replaces all placeholders with corresponding values if they exist in the
  * translated label.
  *
  * @param string $id Id to use for finding translation (trans-unit id in XLIFF)
  * @param string $value 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
  * @param array $arguments Numerically indexed array of values to be inserted into placeholders
  * @param string $source Name of file with translations
  * @param string $package Target package key. If not set, the current package key will be used
  * @param mixed $quantity A number to find plural form for (float or int), NULL to not use plural forms
  * @param string $locale An identifier of locale to use (NULL for use the default locale)
  * @return string Translated label or source label / ID key
  * @throws ViewHelperException
  */
 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) {
         $package = $this->controllerContext->getRequest()->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 $this->translator->translateByOriginalLabel($originalLabel, $arguments, $quantity, $localeObject, $source, $package);
     } else {
         $translation = $this->translator->translateById($id, $arguments, $quantity, $localeObject, $source, $package);
         if ($translation === $id) {
             if ($originalLabel) {
                 return $this->translator->translateByOriginalLabel($originalLabel, $arguments, $quantity, $localeObject, $source, $package);
             } else {
                 return $id;
             }
         } else {
             return $translation;
         }
     }
 }
 /**
  * @test
  */
 public function translateByOriginalLabelReturnsTranslationIfOneNumericArgumentIsGiven()
 {
     $mockTranslationProvider = $this->getAccessibleMock(\TYPO3\Flow\I18n\TranslationProvider\XliffTranslationProvider::class);
     $mockTranslationProvider->expects($this->once())->method('getTranslationByOriginalLabel')->with('Untranslated label', $this->defaultLocale, NULL, 'source', 'packageKey')->will($this->returnValue('Translated label'));
     $mockFormatResolver = $this->getMock(\TYPO3\Flow\I18n\FormatResolver::class);
     $mockFormatResolver->expects($this->once())->method('resolvePlaceholders')->with('Translated label', array(1.0), $this->defaultLocale)->will($this->returnValue('Formatted and translated label'));
     $mockPluralsReader = $this->getMock(\TYPO3\Flow\I18n\Cldr\Reader\PluralsReader::class);
     $mockPluralsReader->expects($this->never())->method('getPluralForm');
     $this->translator->injectTranslationProvider($mockTranslationProvider);
     $this->translator->injectFormatResolver($mockFormatResolver);
     $this->translator->injectPluralsReader($mockPluralsReader);
     $result = $this->translator->translateByOriginalLabel('Untranslated label', array(1.0), NULL, NULL, 'source', 'packageKey');
     $this->assertEquals('Formatted and translated label', $result);
 }
 /**
  * Translate according to currently collected parameters
  *
  * @param array $overrides An associative array to override the collected parameters
  * @return string
  */
 public function translate(array $overrides = [])
 {
     array_replace_recursive($this->parameters, $overrides);
     $id = isset($this->parameters['id']) ? $this->parameters['id'] : null;
     $value = isset($this->parameters['value']) ? $this->parameters['value'] : null;
     $arguments = isset($this->parameters['arguments']) ? $this->parameters['arguments'] : [];
     $source = isset($this->parameters['source']) ? $this->parameters['source'] : 'Main';
     $package = isset($this->parameters['package']) ? $this->parameters['package'] : null;
     $quantity = isset($this->parameters['quantity']) ? $this->parameters['quantity'] : null;
     $locale = isset($this->parameters['locale']) ? $this->parameters['locale'] : null;
     if ($id === null) {
         return $this->translator->translateByOriginalLabel($value, $arguments, $quantity, $locale, $source, $package);
     }
     $translation = $this->translator->translateById($id, $arguments, $quantity, $locale, $source, $package);
     if ($translation === null && $value !== null) {
         return $this->translator->translateByOriginalLabel($value, $arguments, $quantity, $locale, $source, $package);
     }
     return $translation;
 }
 /**
  * Returns a translated version of the given label
  *
  * @param string $value option tag value
  * @param string $label option tag label
  * @return string
  * @throws ViewHelper\Exception
  * @throws Fluid\Exception
  */
 protected function getTranslatedLabel($value, $label)
 {
     $translationConfiguration = $this->arguments['translate'];
     $translateBy = isset($translationConfiguration['by']) ? $translationConfiguration['by'] : 'id';
     $sourceName = isset($translationConfiguration['source']) ? $translationConfiguration['source'] : 'Main';
     $packageKey = isset($translationConfiguration['package']) ? $translationConfiguration['package'] : $this->controllerContext->getRequest()->getControllerPackageKey();
     $prefix = isset($translationConfiguration['prefix']) ? $translationConfiguration['prefix'] : '';
     if (isset($translationConfiguration['locale'])) {
         try {
             $localeObject = new Locale($translationConfiguration['locale']);
         } catch (InvalidLocaleIdentifierException $e) {
             throw new ViewHelper\Exception('"' . $translationConfiguration['locale'] . '" is not a valid locale identifier.', 1330013193);
         }
     } else {
         $localeObject = NULL;
     }
     switch ($translateBy) {
         case 'label':
             $label = isset($translationConfiguration['using']) && $translationConfiguration['using'] === 'value' ? $value : $label;
             return $this->translator->translateByOriginalLabel($label, array(), NULL, $localeObject, $sourceName, $packageKey);
         case 'id':
             $id = $prefix . (isset($translationConfiguration['using']) && $translationConfiguration['using'] === 'label' ? $label : $value);
             return $this->translator->translateById($id, array(), NULL, $localeObject, $sourceName, $packageKey);
         default:
             throw new Fluid\Exception('You can only request to translate by "label" or by "id", but asked for "' . $translateBy . '" in your SelectViewHelper tag.', 1340050647);
     }
 }
 /**
  * @test
  * @dataProvider labelAndLocaleForTranslation
  */
 public function simpleTranslationByLabelWorks($label, $locale, $translation)
 {
     $result = $this->translator->translateByOriginalLabel($label, array(), NULL, $locale, 'Main', 'TYPO3.Flow');
     $this->assertEquals($translation, $result);
 }