Chris@0: setOptions([]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getLangcode() { Chris@0: return $this->langcode; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setLangcode($langcode) { Chris@0: $this->langcode = $langcode; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the options used by the reader. Chris@0: */ Chris@0: public function getOptions() { Chris@0: return $this->options; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set the options for the current reader. Chris@0: */ Chris@0: public function setOptions(array $options) { Chris@0: $options += [ Chris@0: 'customized' => FALSE, Chris@0: 'not_customized' => FALSE, Chris@0: 'not_translated' => FALSE, Chris@0: ]; Chris@0: $this->options = $options; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getHeader() { Chris@0: return new PoHeader($this->getLangcode()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements Drupal\Component\Gettext\PoMetadataInterface::setHeader(). Chris@0: * Chris@0: * @throws Exception Chris@0: * Always, because you cannot set the PO header of a reader. Chris@0: */ Chris@0: public function setHeader(PoHeader $header) { Chris@0: throw new \Exception('You cannot set the PO header in a reader.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Builds and executes a database query based on options set earlier. Chris@0: */ Chris@0: private function loadStrings() { Chris@0: $langcode = $this->langcode; Chris@0: $options = $this->options; Chris@0: $conditions = []; Chris@0: Chris@0: if (array_sum($options) == 0) { Chris@0: // If user asked to not include anything in the translation files, Chris@0: // that would not make sense, so just fall back on providing a template. Chris@0: $langcode = NULL; Chris@0: // Force option to get both translated and untranslated strings. Chris@0: $options['not_translated'] = TRUE; Chris@0: } Chris@0: // Build and execute query to collect source strings and translations. Chris@0: if (!empty($langcode)) { Chris@0: $conditions['language'] = $langcode; Chris@0: // Translate some options into field conditions. Chris@0: if ($options['customized']) { Chris@0: if (!$options['not_customized']) { Chris@0: // Filter for customized strings only. Chris@0: $conditions['customized'] = LOCALE_CUSTOMIZED; Chris@0: } Chris@0: // Else no filtering needed in this case. Chris@0: } Chris@0: else { Chris@0: if ($options['not_customized']) { Chris@0: // Filter for non-customized strings only. Chris@0: $conditions['customized'] = LOCALE_NOT_CUSTOMIZED; Chris@0: } Chris@0: else { Chris@0: // Filter for strings without translation. Chris@0: $conditions['translated'] = FALSE; Chris@0: } Chris@0: } Chris@0: if (!$options['not_translated']) { Chris@0: // Filter for string with translation. Chris@0: $conditions['translated'] = TRUE; Chris@0: } Chris@0: return \Drupal::service('locale.storage')->getTranslations($conditions); Chris@0: } Chris@0: else { Chris@0: // If no language, we don't need any of the target fields. Chris@0: return \Drupal::service('locale.storage')->getStrings($conditions); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the database result resource for the given language and options. Chris@0: */ Chris@0: private function readString() { Chris@0: if (!isset($this->result)) { Chris@0: $this->result = $this->loadStrings(); Chris@0: } Chris@0: return array_shift($this->result); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function readItem() { Chris@0: if ($string = $this->readString()) { Chris@0: $values = (array) $string; Chris@0: $po_item = new PoItem(); Chris@0: $po_item->setFromArray($values); Chris@0: return $po_item; Chris@0: } Chris@0: } Chris@0: Chris@0: }