PluralFormula.php in Drupal 8
File
core/modules/locale/src/PluralFormula.php
View source
<?php
namespace Drupal\locale;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\State\StateInterface;
class PluralFormula implements PluralFormulaInterface {
protected $languageManager;
protected $state;
protected $formulae;
public function __construct(LanguageManagerInterface $language_manager, StateInterface $state) {
$this->languageManager = $language_manager;
$this->state = $state;
}
public function setPluralFormula($langcode, $plural_count, array $formula) {
$this
->loadFormulae();
$this->formulae[$langcode] = [
'plurals' => $plural_count,
'formula' => $formula,
];
$this->state
->set('locale.translation.formulae', $this->formulae);
return $this;
}
public function getNumberOfPlurals($langcode = NULL) {
$this
->loadFormulae();
$langcode = $langcode ?: $this->languageManager
->getCurrentLanguage()
->getId();
if (!isset($this->formulae[$langcode]['plurals'])) {
return 2;
}
return $this->formulae[$langcode]['plurals'];
}
public function getFormula($langcode) {
$this
->loadFormulae();
return isset($this->formulae[$langcode]['formula']) ? $this->formulae[$langcode]['formula'] : FALSE;
}
protected function loadFormulae() {
if (!isset($this->formulae)) {
$this->formulae = $this->state
->get('locale.translation.formulae', []);
}
}
public function reset() {
$this->formulae = NULL;
return $this;
}
}
Classes
Name |
Description |
PluralFormula |
Manages the storage of plural formula per language in state. |