Placeholders have following syntax:
{id[,name[,attribute1[,attribute2...]]]}
Where 'id' is an index of argument to insert in place of placeholder, an
optional 'name' is a name of formatter to use for formatting the argument
(if no name given, provided argument will be just string-casted), and
optional attributes are strings directly passed to the formatter (what they
do depends on concrete formatter which is being used).
Examples:
{0}
{0,number,decimal}
{1,datetime,time,full}
/** * @test */ public function formatResolverWorksCorrectlyForFullyQualifiedFormatterClassNames() { $actualFormatter = new Fixtures\SampleFormatter(); $locale = new I18n\Locale('de'); $testResult = $this->formatResolver->resolvePlaceholders(sprintf('{0,%s}', Fixtures\SampleFormatter::class), ['foo'], $locale); $this->assertEquals($actualFormatter->format('foo', $locale), $testResult); }
/** * Returns translated string found under the $labelId. * * 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. * * @param string $labelId Key to use for finding translation * @param array $arguments An array of values to replace placeholders with * @param mixed $quantity A number to find plural form for (float or int), NULL to not use plural forms * @param Locale $locale Locale to use (NULL for default one) * @param string $sourceName Name of file with translations, base path is $packageKey/Resources/Private/Locale/Translations/ * @param string $packageKey Key of the package containing the source file * @return string Translated message or NULL on failure * @api * @see Translator::translateByOriginalLabel() */ public function translateById($labelId, array $arguments = [], $quantity = null, Locale $locale = null, $sourceName = 'Main', $packageKey = 'Neos.Flow') { if ($locale === null) { $locale = $this->localizationService->getConfiguration()->getCurrentLocale(); } $pluralForm = $this->getPluralForm($quantity, $locale); $translatedMessage = $this->translationProvider->getTranslationById($labelId, $locale, $pluralForm, $sourceName, $packageKey); if ($translatedMessage === false) { return null; } if (!empty($arguments)) { return $this->formatResolver->resolvePlaceholders($translatedMessage, $arguments, $locale); } return $translatedMessage; }
/** * @test */ public function fullyQualifiedFormatterWithLowercaseVendorNameIsCorrectlyBeingUsed() { $mockFormatter = $this->createMock(I18n\Formatter\FormatterInterface::class); $mockFormatter->expects($this->once())->method('format')->with(123, $this->sampleLocale, [])->will($this->returnValue('FormatterOutput42')); $mockObjectManager = $this->createMock(ObjectManagerInterface::class); $mockObjectManager->expects($this->once())->method('isRegistered')->with('acme\\Foo\\SampleFormatter')->will($this->returnValue(true)); $mockObjectManager->expects($this->once())->method('get')->with('acme\\Foo\\SampleFormatter')->will($this->returnValue($mockFormatter)); $mockReflectionService = $this->createMock(ReflectionService::class); $mockReflectionService->expects($this->once())->method('isClassImplementationOf')->with('acme\\Foo\\SampleFormatter', I18n\Formatter\FormatterInterface::class)->will($this->returnValue(true)); $formatResolver = new I18n\FormatResolver(); $formatResolver->injectObjectManager($mockObjectManager); $this->inject($formatResolver, 'reflectionService', $mockReflectionService); $actual = $formatResolver->resolvePlaceholders('{0,acme\\Foo\\SampleFormatter}', [123], $this->sampleLocale); $this->assertEquals('FormatterOutput42', $actual); }