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