Chris@0: [ Chris@0: * 'plurals' => 2, Chris@0: * 'formula' => [ Chris@0: * // @todo Chris@0: * ] Chris@0: * ], Chris@0: * ] Chris@0: * @endcode Chris@17: * @var array Chris@0: */ Chris@0: protected $formulae; Chris@0: Chris@0: /** Chris@0: * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager Chris@0: * @param \Drupal\Core\State\StateInterface $state Chris@0: */ Chris@0: public function __construct(LanguageManagerInterface $language_manager, StateInterface $state) { Chris@0: $this->languageManager = $language_manager; Chris@0: $this->state = $state; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setPluralFormula($langcode, $plural_count, array $formula) { Chris@0: // Ensure that the formulae are loaded. Chris@0: $this->loadFormulae(); Chris@0: Chris@0: $this->formulae[$langcode] = [ Chris@0: 'plurals' => $plural_count, Chris@0: 'formula' => $formula, Chris@0: ]; Chris@0: $this->state->set('locale.translation.formulae', $this->formulae); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getNumberOfPlurals($langcode = NULL) { Chris@0: // Ensure that the formulae are loaded. Chris@0: $this->loadFormulae(); Chris@0: Chris@0: // Set the langcode to use. Chris@0: $langcode = $langcode ?: $this->languageManager->getCurrentLanguage()->getId(); Chris@0: Chris@0: // We assume 2 plurals if there is no explicit information yet. Chris@0: if (!isset($this->formulae[$langcode]['plurals'])) { Chris@0: return 2; Chris@0: } Chris@0: return $this->formulae[$langcode]['plurals']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getFormula($langcode) { Chris@0: $this->loadFormulae(); Chris@0: return isset($this->formulae[$langcode]['formula']) ? $this->formulae[$langcode]['formula'] : FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Loads the formulae and stores them on the PluralFormula object if not set. Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: protected function loadFormulae() { Chris@0: if (!isset($this->formulae)) { Chris@0: $this->formulae = $this->state->get('locale.translation.formulae', []); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function reset() { Chris@0: $this->formulae = NULL; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: }