Messages (labels) can be translated in two modes: - by original label: untranslated label is used as a key - by ID: string identifier is used as a key (eg. user.noaccess) Correct plural form of translated message is returned when $quantity parameter is provided to a method. Otherwise, or on failure just translated version is returned (eg. when string is translated only to one form). When all fails, untranslated (original) string or ID is returned (depends on translation method). Placeholders' resolving is done when needed (see FormatResolver class). Actual translating is done by injected TranslationProvider instance, so storage format depends on concrete implementation.
See also: FormatResolver
See also: TranslationProvider\TranslationProviderInterface
See also: Cldr\Reader\PluralsReader
Example #1
0
 /**
  * Executes this finisher
  * @see AbstractFinisher::execute()
  *
  * @return void
  * @throws FinisherException
  */
 protected function executeInternal()
 {
     $formRuntime = $this->finisherContext->getFormRuntime();
     $labelId = $this->parseOption('translation.id');
     if ($labelId !== null) {
         $locale = null;
         $localeIdentifier = $this->parseOption('translation.locale');
         if ($localeIdentifier !== null) {
             try {
                 $locale = new Locale($localeIdentifier);
             } catch (InvalidLocaleIdentifierException $exception) {
                 throw new FinisherException(sprintf('"%s" is not a valid locale identifier.', $locale), 1421325113, $exception);
             }
         }
         $messagePackageKey = $this->parseOption('translation.package');
         if ($messagePackageKey === null) {
             $renderingOptions = $formRuntime->getRenderingOptions();
             $messagePackageKey = $renderingOptions['translationPackage'];
         }
         $message = $this->translator->translateById($labelId, array(), null, $locale, $this->parseOption('translation.source'), $messagePackageKey);
     } else {
         $message = $this->parseOption('message');
     }
     $formRuntime->getResponse()->setContent($message);
 }
 /**
  * @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);
 }
 /**
  * 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;
 }
Example #4
0
 /**
  * Discards content of the whole workspace
  *
  * @param Workspace $workspace
  * @return void
  */
 public function discardWorkspaceAction(Workspace $workspace)
 {
     $unpublishedNodes = $this->publishingService->getUnpublishedNodes($workspace);
     $this->publishingService->discardNodes($unpublishedNodes);
     $this->addFlashMessage($this->translator->translateById('workspaces.allChangesInWorkspaceHaveBeenDiscarded', [htmlspecialchars($workspace->getTitle())], null, null, 'Modules', 'Neos.Neos'));
     $this->redirect('index');
 }
 /**
  * @test
  * @dataProvider translateByIdDataProvider
  * @param string $id
  * @param string $translatedId
  * @param string $expectedResult
  */
 public function translateByIdTests($id, $translatedId, $expectedResult)
 {
     $mockTranslationProvider = $this->createMock(XliffTranslationProvider::class);
     $mockTranslationProvider->expects($this->once())->method('getTranslationById')->with($id)->will($this->returnValue($translatedId));
     $this->translator->injectTranslationProvider($mockTranslationProvider);
     $actualResult = $this->translator->translateById($id);
     $this->assertSame($expectedResult, $actualResult);
 }
 /**
  * @param string $property
  * @param FormElementInterface $element
  * @return string the rendered form head
  */
 public function render($property, FormElementInterface $element = null)
 {
     if ($element === null) {
         $element = $this->renderChildren();
     }
     if ($property === 'label') {
         $defaultValue = $element->getLabel();
     } else {
         $defaultValue = isset($element->getProperties()[$property]) ? (string) $element->getProperties()[$property] : '';
     }
     $renderingOptions = $element->getRenderingOptions();
     if (!isset($renderingOptions['translationPackage'])) {
         return $defaultValue;
     }
     $translationId = sprintf('forms.elements.%s.%s', $element->getIdentifier(), $property);
     try {
         $translation = $this->translator->translateById($translationId, [], null, null, 'Main', $renderingOptions['translationPackage']);
     } catch (ResourceException $exception) {
         return $defaultValue;
     }
     return $translation === null ? $defaultValue : $translation;
 }
 /**
  * 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
  */
 public function translationByIdReturnsNullOnFailure()
 {
     $result = $this->translator->translateById('non-existing-id');
     $this->assertNull($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);
     }
 }
 /**
  * Add a translated flashMessage.
  *
  * @param string $messageBody The translation id for the message body.
  * @param string $messageTitle The translation id for the message title.
  * @param string $severity
  * @param array $messageArguments
  * @param integer $messageCode
  * @return void
  */
 public function addFlashMessage($messageBody, $messageTitle = '', $severity = Message::SEVERITY_OK, array $messageArguments = array(), $messageCode = null)
 {
     if (is_string($messageBody)) {
         $messageBody = $this->translator->translateById($messageBody, $messageArguments, null, null, 'Main', 'Neos.Media') ?: $messageBody;
     }
     $messageTitle = $this->translator->translateById($messageTitle, $messageArguments, null, null, 'Main', 'Neos.Media');
     parent::addFlashMessage($messageBody, $messageTitle, $severity, $messageArguments, $messageCode);
 }