Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\locale;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Language\LanguageManagerInterface;
|
Chris@0
|
6 use Drupal\Core\State\StateInterface;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Manages the storage of plural formula per language in state.
|
Chris@0
|
10 *
|
Chris@0
|
11 * @see \Drupal\locale\PoDatabaseWriter::setHeader()
|
Chris@0
|
12 */
|
Chris@0
|
13 class PluralFormula implements PluralFormulaInterface {
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * @var \Drupal\Core\Language\LanguageManagerInterface
|
Chris@0
|
17 */
|
Chris@0
|
18 protected $languageManager;
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * @var \Drupal\Core\State\StateInterface
|
Chris@0
|
22 */
|
Chris@0
|
23 protected $state;
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * The plural formula and count keyed by langcode.
|
Chris@0
|
27 *
|
Chris@0
|
28 * For example the structure looks like this:
|
Chris@0
|
29 * @code
|
Chris@0
|
30 * [
|
Chris@0
|
31 * 'de' => [
|
Chris@0
|
32 * 'plurals' => 2,
|
Chris@0
|
33 * 'formula' => [
|
Chris@0
|
34 * // @todo
|
Chris@0
|
35 * ]
|
Chris@0
|
36 * ],
|
Chris@0
|
37 * ]
|
Chris@0
|
38 * @endcode
|
Chris@17
|
39 * @var array
|
Chris@0
|
40 */
|
Chris@0
|
41 protected $formulae;
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
|
Chris@0
|
45 * @param \Drupal\Core\State\StateInterface $state
|
Chris@0
|
46 */
|
Chris@0
|
47 public function __construct(LanguageManagerInterface $language_manager, StateInterface $state) {
|
Chris@0
|
48 $this->languageManager = $language_manager;
|
Chris@0
|
49 $this->state = $state;
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * {@inheritdoc}
|
Chris@0
|
54 */
|
Chris@0
|
55 public function setPluralFormula($langcode, $plural_count, array $formula) {
|
Chris@0
|
56 // Ensure that the formulae are loaded.
|
Chris@0
|
57 $this->loadFormulae();
|
Chris@0
|
58
|
Chris@0
|
59 $this->formulae[$langcode] = [
|
Chris@0
|
60 'plurals' => $plural_count,
|
Chris@0
|
61 'formula' => $formula,
|
Chris@0
|
62 ];
|
Chris@0
|
63 $this->state->set('locale.translation.formulae', $this->formulae);
|
Chris@0
|
64 return $this;
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * {@inheritdoc}
|
Chris@0
|
69 */
|
Chris@0
|
70 public function getNumberOfPlurals($langcode = NULL) {
|
Chris@0
|
71 // Ensure that the formulae are loaded.
|
Chris@0
|
72 $this->loadFormulae();
|
Chris@0
|
73
|
Chris@0
|
74 // Set the langcode to use.
|
Chris@0
|
75 $langcode = $langcode ?: $this->languageManager->getCurrentLanguage()->getId();
|
Chris@0
|
76
|
Chris@0
|
77 // We assume 2 plurals if there is no explicit information yet.
|
Chris@0
|
78 if (!isset($this->formulae[$langcode]['plurals'])) {
|
Chris@0
|
79 return 2;
|
Chris@0
|
80 }
|
Chris@0
|
81 return $this->formulae[$langcode]['plurals'];
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@0
|
85 * {@inheritdoc}
|
Chris@0
|
86 */
|
Chris@0
|
87 public function getFormula($langcode) {
|
Chris@0
|
88 $this->loadFormulae();
|
Chris@0
|
89 return isset($this->formulae[$langcode]['formula']) ? $this->formulae[$langcode]['formula'] : FALSE;
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 /**
|
Chris@0
|
93 * Loads the formulae and stores them on the PluralFormula object if not set.
|
Chris@0
|
94 *
|
Chris@0
|
95 * @return array
|
Chris@0
|
96 */
|
Chris@0
|
97 protected function loadFormulae() {
|
Chris@0
|
98 if (!isset($this->formulae)) {
|
Chris@0
|
99 $this->formulae = $this->state->get('locale.translation.formulae', []);
|
Chris@0
|
100 }
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 /**
|
Chris@0
|
104 * {@inheritdoc}
|
Chris@0
|
105 */
|
Chris@0
|
106 public function reset() {
|
Chris@0
|
107 $this->formulae = NULL;
|
Chris@0
|
108 return $this;
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 }
|