/** * @test * @expectedException \TYPO3\FLOW3\I18n\TranslationProvider\Exception\InvalidPluralFormException */ public function getTranslationByIdThrowsExceptionWhenInvalidPluralFormProvided() { $this->mockPluralsReader->expects($this->any())->method('getPluralForms')->with($this->sampleLocale)->will($this->returnValue(array(\TYPO3\FLOW3\I18n\Cldr\Reader\PluralsReader::RULE_ONE, \TYPO3\FLOW3\I18n\Cldr\Reader\PluralsReader::RULE_OTHER))); $translationProvider = $this->getMock('TYPO3\\FLOW3\\I18n\\TranslationProvider\\XliffTranslationProvider', array('getModel')); $translationProvider->injectPluralsReader($this->mockPluralsReader); $translationProvider->getTranslationById('bar', $this->sampleLocale, \TYPO3\FLOW3\I18n\Cldr\Reader\PluralsReader::RULE_FEW, $this->sampleSourceName, $this->samplePackageKey); }
/** * Get the plural form to be used. * * If $quantity is non-NULL, the plural form for provided $locale will be * chosen according to it. * * Otherwise, if $arguments contains exactly one numeric element, it is * automatically used as the $quantity. * * In all other cases, NULL is returned. * * @param mixed $quantity * @param array $arguments * @param \TYPO3\FLOW3\I18n\Locale $locale * @return string */ protected function getPluralForm($quantity, array $arguments, Locale $locale) { if (!is_numeric($quantity)) { if (count($arguments) === 1) { return is_numeric(current($arguments)) ? $this->pluralsReader->getPluralForm(current($arguments), $locale) : NULL; } else { return NULL; } } else { return $this->pluralsReader->getPluralForm($quantity, $locale); } }
/** * Returns label for a key ($labelId) from a file defined by $sourceName. * * Chooses particular form of label if available and defined in $pluralForm. * * @param string $labelId Key used to find translated label * @param \TYPO3\FLOW3\I18n\Locale $locale Locale to use * @param string $pluralForm One of RULE constants of PluralsReader * @param string $sourceName A relative path to the filename with translations (labels' catalog) * @param string $packageKey Key of the package containing the source file * @return mixed Translated label or FALSE on failure * @throws \TYPO3\FLOW3\I18n\TranslationProvider\Exception\InvalidPluralFormException */ public function getTranslationById($labelId, \TYPO3\FLOW3\I18n\Locale $locale, $pluralForm = NULL, $sourceName = 'Main', $packageKey = 'TYPO3.FLOW3') { $model = $this->getModel($packageKey, $sourceName, $locale); if ($pluralForm !== NULL) { $pluralFormsForProvidedLocale = $this->pluralsReader->getPluralForms($locale); if (!in_array($pluralForm, $pluralFormsForProvidedLocale)) { throw new \TYPO3\FLOW3\I18n\TranslationProvider\Exception\InvalidPluralFormException('There is no plural form "' . $pluralForm . '" in "' . (string) $locale . '" locale.', 1281033387); } // We need to convert plural form's string to index, as they are accessed using integers in XLIFF files $pluralFormIndex = (int) array_search($pluralForm, $pluralFormsForProvidedLocale); } else { $pluralFormIndex = 0; } return $model->getTargetByTransUnitId($labelId, $pluralFormIndex); }
/** * @test * @dataProvider quantities */ public function returnsCorrectPluralForm($quantity, $pluralForm) { $locale = new \TYPO3\FLOW3\I18n\Locale('mo'); $result = $this->reader->getPluralForm($quantity, $locale); $this->assertEquals($pluralForm, $result); }