class PluralFormula
Same name in other branches
- 8.9.x core/modules/locale/src/PluralFormula.php \Drupal\locale\PluralFormula
- 10 core/modules/locale/src/PluralFormula.php \Drupal\locale\PluralFormula
- 11.x core/modules/locale/src/PluralFormula.php \Drupal\locale\PluralFormula
Manages the storage of plural formula per language in state.
Hierarchy
- class \Drupal\locale\PluralFormula implements \Drupal\locale\PluralFormulaInterface
Expanded class hierarchy of PluralFormula
See also
\Drupal\locale\PoDatabaseWriter::setHeader()
1 string reference to 'PluralFormula'
- locale.services.yml in core/
modules/ locale/ locale.services.yml - core/modules/locale/locale.services.yml
1 service uses PluralFormula
- locale.plural.formula in core/
modules/ locale/ locale.services.yml - Drupal\locale\PluralFormula
File
-
core/
modules/ locale/ src/ PluralFormula.php, line 13
Namespace
Drupal\localeView source
class PluralFormula implements PluralFormulaInterface {
/**
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* The plural formula and count keyed by langcode.
*
* For example the structure looks like this:
* @code
* [
* 'de' => [
* 'plurals' => 2,
* 'formula' => [
* // @todo
* ]
* ],
* ]
* @endcode
* @var array
*/
protected $formulae;
/**
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\State\StateInterface $state
* The 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 $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;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
PluralFormula::$formulae | protected | property | The plural formula and count keyed by langcode. | |
PluralFormula::$languageManager | protected | property | ||
PluralFormula::$state | protected | property | ||
PluralFormula::getFormula | public | function | Gets the plural formula for a langcode. | Overrides PluralFormulaInterface::getFormula |
PluralFormula::getNumberOfPlurals | public | function | Returns the number of plurals supported by a given language. | Overrides PluralFormulaInterface::getNumberOfPlurals |
PluralFormula::loadFormulae | protected | function | Loads the formulae and stores them on the PluralFormula object if not set. | |
PluralFormula::reset | public | function | Resets the static formulae cache. | Overrides PluralFormulaInterface::reset |
PluralFormula::setPluralFormula | public | function | Overrides PluralFormulaInterface::setPluralFormula | |
PluralFormula::__construct | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.