Neos\Flow\I18n\Xliff\XliffModel::getTargetBySource PHP Метод

getTargetBySource() публичный Метод

Returns translated label ("target" tag in XLIFF) from source-target pair where "source" tag equals to $source parameter.
public getTargetBySource ( string $source, integer $pluralFormIndex ) : mixed
$source string Label in original language ("source" tag in XLIFF)
$pluralFormIndex integer Index of plural form to use (starts with 0)
Результат mixed Translated label or FALSE on failure
    public function getTargetBySource($source, $pluralFormIndex = 0)
    {
        if (!isset($this->xmlParsedData['translationUnits'])) {
            $this->i18nLogger->log(sprintf('No trans-unit elements were found in "%s". This is allowed per specification, but no translation can be applied then.', $this->sourcePath), LOG_DEBUG);
            return false;
        }
        foreach ($this->xmlParsedData['translationUnits'] as $translationUnit) {
            // $source is always singular (or only) form, so compare with index 0
            if (!isset($translationUnit[0]) || $translationUnit[0]['source'] !== $source) {
                continue;
            }
            if (count($translationUnit) <= $pluralFormIndex) {
                $this->i18nLogger->log('The plural form index "' . $pluralFormIndex . '" for the source translation "' . $source . '"  in ' . $this->sourcePath . ' is not available.', LOG_DEBUG);
                return false;
            }
            return $translationUnit[$pluralFormIndex]['target'] ?: false;
        }
        return false;
    }

Usage Example

 /**
  * @test
  */
 public function getTargetBySourceLogsSilentlyIfNoTransUnitsArePresent()
 {
     $this->mockXliffParser = $this->createMock(I18n\Xliff\XliffParser::class);
     $this->mockXliffParser->expects($this->once())->method('getParsedData')->will($this->returnValue([]));
     $mockLogger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
     $mockLogger->expects($this->once())->method('log')->with($this->stringStartsWith('No trans-unit elements were found'), LOG_DEBUG);
     $this->model->injectParser($this->mockXliffParser);
     $this->inject($this->model, 'i18nLogger', $mockLogger);
     $this->model->initializeObject();
     $this->model->getTargetBySource('foo');
 }