Searches for a translation in the source as defined by $sourceName
(interpretation depends on concrete translation provider used).
If any arguments are provided in the $arguments array, they will be inserted
to the translated string (in place of corresponding placeholders, with
format defined by these placeholders).
If $quantity is provided, correct plural form for provided $locale will
be chosen and used to choose correct translation variant.
If no $locale is provided, default system locale will be used.
public translateByOriginalLabel ( string $originalLabel, array $arguments = [], mixed $quantity = null, |
||
$originalLabel | string | Untranslated message |
$arguments | array | An array of values to replace placeholders with |
$quantity | mixed | A number to find plural form for (float or int), NULL to not use plural forms |
$locale | Locale to use (NULL for default one) | |
$sourceName | string | Name of file with translations, base path is $packageKey/Resources/Private/Locale/Translations/ |
$packageKey | string | Key of the package containing the source file |
return | string | Translated $originalLabel or $originalLabel itself on failure |
/** * 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 (use / as a directory separator) * @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) { $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; }
/** * @test * @dataProvider translateByOriginalLabelDataProvider * @param string $originalLabel * @param string $translatedLabel * @param string $expectedResult */ public function translateByOriginalLabelTests($originalLabel, $translatedLabel, $expectedResult) { $mockTranslationProvider = $this->createMock(XliffTranslationProvider::class); $mockTranslationProvider->expects($this->once())->method('getTranslationByOriginalLabel')->with($originalLabel)->will($this->returnValue($translatedLabel)); $this->translator->injectTranslationProvider($mockTranslationProvider); $actualResult = $this->translator->translateByOriginalLabel($originalLabel); $this->assertSame($expectedResult, $actualResult); }
/** * 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; }
/** * @test * @dataProvider labelAndLocaleForTranslation */ public function simpleTranslationByLabelWorks($label, $locale, $translation) { $result = $this->translator->translateByOriginalLabel($label, [], null, $locale, 'Main', 'Neos.Flow'); $this->assertEquals($translation, $result); }
/** * 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'; $request = $this->controllerContext->getRequest(); $packageKey = null; if (isset($translationConfiguration['package'])) { $packageKey = $translationConfiguration['package']; } elseif ($request instanceof ActionRequest) { $packageKey = $request->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); $translation = $this->translator->translateById($id, array(), null, $localeObject, $sourceName, $packageKey); return $translation !== null ? $translation : $label; default: throw new ViewHelper\Exception('You can only request to translate by "label" or by "id", but asked for "' . $translateBy . '" in your SelectViewHelper tag.', 1340050647); } }