Mercurial > hg > isophonics-drupal-site
comparison core/modules/locale/src/PoDatabaseReader.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\locale; | |
4 | |
5 use Drupal\Component\Gettext\PoHeader; | |
6 use Drupal\Component\Gettext\PoItem; | |
7 use Drupal\Component\Gettext\PoReaderInterface; | |
8 | |
9 /** | |
10 * Gettext PO reader working with the locale module database. | |
11 */ | |
12 class PoDatabaseReader implements PoReaderInterface { | |
13 | |
14 /** | |
15 * An associative array indicating which type of strings should be read. | |
16 * | |
17 * Elements of the array: | |
18 * - not_customized: boolean indicating if not customized strings should be | |
19 * read. | |
20 * - customized: boolean indicating if customized strings should be read. | |
21 * - no_translated: boolean indicating if non-translated should be read. | |
22 * | |
23 * The three options define three distinct sets of strings, which combined | |
24 * cover all strings. | |
25 * | |
26 * @var array | |
27 */ | |
28 private $options; | |
29 | |
30 /** | |
31 * Language code of the language being read from the database. | |
32 * | |
33 * @var string | |
34 */ | |
35 private $langcode; | |
36 | |
37 /** | |
38 * Store the result of the query so it can be iterated later. | |
39 * | |
40 * @var resource | |
41 */ | |
42 private $result; | |
43 | |
44 /** | |
45 * Constructor, initializes with default options. | |
46 */ | |
47 public function __construct() { | |
48 $this->setOptions([]); | |
49 } | |
50 | |
51 /** | |
52 * {@inheritdoc} | |
53 */ | |
54 public function getLangcode() { | |
55 return $this->langcode; | |
56 } | |
57 | |
58 /** | |
59 * {@inheritdoc} | |
60 */ | |
61 public function setLangcode($langcode) { | |
62 $this->langcode = $langcode; | |
63 } | |
64 | |
65 /** | |
66 * Get the options used by the reader. | |
67 */ | |
68 public function getOptions() { | |
69 return $this->options; | |
70 } | |
71 | |
72 /** | |
73 * Set the options for the current reader. | |
74 */ | |
75 public function setOptions(array $options) { | |
76 $options += [ | |
77 'customized' => FALSE, | |
78 'not_customized' => FALSE, | |
79 'not_translated' => FALSE, | |
80 ]; | |
81 $this->options = $options; | |
82 } | |
83 | |
84 /** | |
85 * {@inheritdoc} | |
86 */ | |
87 public function getHeader() { | |
88 return new PoHeader($this->getLangcode()); | |
89 } | |
90 | |
91 /** | |
92 * Implements Drupal\Component\Gettext\PoMetadataInterface::setHeader(). | |
93 * | |
94 * @throws Exception | |
95 * Always, because you cannot set the PO header of a reader. | |
96 */ | |
97 public function setHeader(PoHeader $header) { | |
98 throw new \Exception('You cannot set the PO header in a reader.'); | |
99 } | |
100 | |
101 /** | |
102 * Builds and executes a database query based on options set earlier. | |
103 */ | |
104 private function loadStrings() { | |
105 $langcode = $this->langcode; | |
106 $options = $this->options; | |
107 $conditions = []; | |
108 | |
109 if (array_sum($options) == 0) { | |
110 // If user asked to not include anything in the translation files, | |
111 // that would not make sense, so just fall back on providing a template. | |
112 $langcode = NULL; | |
113 // Force option to get both translated and untranslated strings. | |
114 $options['not_translated'] = TRUE; | |
115 } | |
116 // Build and execute query to collect source strings and translations. | |
117 if (!empty($langcode)) { | |
118 $conditions['language'] = $langcode; | |
119 // Translate some options into field conditions. | |
120 if ($options['customized']) { | |
121 if (!$options['not_customized']) { | |
122 // Filter for customized strings only. | |
123 $conditions['customized'] = LOCALE_CUSTOMIZED; | |
124 } | |
125 // Else no filtering needed in this case. | |
126 } | |
127 else { | |
128 if ($options['not_customized']) { | |
129 // Filter for non-customized strings only. | |
130 $conditions['customized'] = LOCALE_NOT_CUSTOMIZED; | |
131 } | |
132 else { | |
133 // Filter for strings without translation. | |
134 $conditions['translated'] = FALSE; | |
135 } | |
136 } | |
137 if (!$options['not_translated']) { | |
138 // Filter for string with translation. | |
139 $conditions['translated'] = TRUE; | |
140 } | |
141 return \Drupal::service('locale.storage')->getTranslations($conditions); | |
142 } | |
143 else { | |
144 // If no language, we don't need any of the target fields. | |
145 return \Drupal::service('locale.storage')->getStrings($conditions); | |
146 } | |
147 } | |
148 | |
149 /** | |
150 * Get the database result resource for the given language and options. | |
151 */ | |
152 private function readString() { | |
153 if (!isset($this->result)) { | |
154 $this->result = $this->loadStrings(); | |
155 } | |
156 return array_shift($this->result); | |
157 } | |
158 | |
159 /** | |
160 * {@inheritdoc} | |
161 */ | |
162 public function readItem() { | |
163 if ($string = $this->readString()) { | |
164 $values = (array) $string; | |
165 $po_item = new PoItem(); | |
166 $po_item->setFromArray($values); | |
167 return $po_item; | |
168 } | |
169 } | |
170 | |
171 } |