X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Flocale%2Fsrc%2FPluralFormula.php;fp=web%2Fcore%2Fmodules%2Flocale%2Fsrc%2FPluralFormula.php;h=638d60fac863a023212887ab94b7c3f9c69dfa01;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/locale/src/PluralFormula.php b/web/core/modules/locale/src/PluralFormula.php new file mode 100644 index 000000000..638d60fac --- /dev/null +++ b/web/core/modules/locale/src/PluralFormula.php @@ -0,0 +1,111 @@ + [ + * 'plurals' => 2, + * 'formula' => [ + * // @todo + * ] + * ], + * ] + * @endcode + * @var [] + */ + protected $formulae; + + /** + * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager + * @param \Drupal\Core\State\StateInterface $state + */ + public function __construct(LanguageManagerInterface $language_manager, StateInterface $state) { + $this->languageManager = $language_manager; + $this->state = $state; + } + + /** + * {@inheritdoc} + */ + public function setPluralFormula($langcode, $plural_count, array $formula) { + // Ensure that the formulae are loaded. + $this->loadFormulae(); + + $this->formulae[$langcode] = [ + 'plurals' => $plural_count, + 'formula' => $formula, + ]; + $this->state->set('locale.translation.formulae', $this->formulae); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getNumberOfPlurals($langcode = NULL) { + // Ensure that the formulae are loaded. + $this->loadFormulae(); + + // Set the langcode to use. + $langcode = $langcode ?: $this->languageManager->getCurrentLanguage()->getId(); + + // We assume 2 plurals if there is no explicit information yet. + if (!isset($this->formulae[$langcode]['plurals'])) { + return 2; + } + return $this->formulae[$langcode]['plurals']; + } + + /** + * {@inheritdoc} + */ + public function getFormula($langcode) { + $this->loadFormulae(); + return isset($this->formulae[$langcode]['formula']) ? $this->formulae[$langcode]['formula'] : FALSE; + } + + /** + * Loads the formulae and stores them on the PluralFormula object if not set. + * + * @return array + */ + protected function loadFormulae() { + if (!isset($this->formulae)) { + $this->formulae = $this->state->get('locale.translation.formulae', []); + } + } + + /** + * {@inheritdoc} + */ + public function reset() { + $this->formulae = NULL; + return $this; + } + +}