3 namespace Drupal\locale;
5 use Drupal\Component\Gettext\PoHeader;
6 use Drupal\Component\Gettext\PoItem;
7 use Drupal\Component\Gettext\PoReaderInterface;
10 * Gettext PO reader working with the locale module database.
12 class PoDatabaseReader implements PoReaderInterface {
15 * An associative array indicating which type of strings should be read.
17 * Elements of the array:
18 * - not_customized: boolean indicating if not customized strings should be
20 * - customized: boolean indicating if customized strings should be read.
21 * - no_translated: boolean indicating if non-translated should be read.
23 * The three options define three distinct sets of strings, which combined
31 * Language code of the language being read from the database.
38 * Store the result of the query so it can be iterated later.
45 * Constructor, initializes with default options.
47 public function __construct() {
48 $this->setOptions([]);
54 public function getLangcode() {
55 return $this->langcode;
61 public function setLangcode($langcode) {
62 $this->langcode = $langcode;
66 * Get the options used by the reader.
68 public function getOptions() {
69 return $this->options;
73 * Set the options for the current reader.
75 public function setOptions(array $options) {
77 'customized' => FALSE,
78 'not_customized' => FALSE,
79 'not_translated' => FALSE,
81 $this->options = $options;
87 public function getHeader() {
88 return new PoHeader($this->getLangcode());
92 * Implements Drupal\Component\Gettext\PoMetadataInterface::setHeader().
95 * Always, because you cannot set the PO header of a reader.
97 public function setHeader(PoHeader $header) {
98 throw new \Exception('You cannot set the PO header in a reader.');
102 * Builds and executes a database query based on options set earlier.
104 private function loadStrings() {
105 $langcode = $this->langcode;
106 $options = $this->options;
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.
113 // Force option to get both translated and untranslated strings.
114 $options['not_translated'] = TRUE;
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;
125 // Else no filtering needed in this case.
128 if ($options['not_customized']) {
129 // Filter for non-customized strings only.
130 $conditions['customized'] = LOCALE_NOT_CUSTOMIZED;
133 // Filter for strings without translation.
134 $conditions['translated'] = FALSE;
137 if (!$options['not_translated']) {
138 // Filter for string with translation.
139 $conditions['translated'] = TRUE;
141 return \Drupal::service('locale.storage')->getTranslations($conditions);
144 // If no language, we don't need any of the target fields.
145 return \Drupal::service('locale.storage')->getStrings($conditions);
150 * Get the database result resource for the given language and options.
152 private function readString() {
153 if (!isset($this->result)) {
154 $this->result = $this->loadStrings();
156 return array_shift($this->result);
162 public function readItem() {
163 if ($string = $this->readString()) {
164 $values = (array) $string;
165 $po_item = new PoItem();
166 $po_item->setFromArray($values);