diff --git a/core/lib/Drupal/Core/Gettext/TranslationDatabaseIterator.php b/core/lib/Drupal/Core/Gettext/TranslationDatabaseIterator.php new file mode 100644 index 0000000..0449d2b --- /dev/null +++ b/core/lib/Drupal/Core/Gettext/TranslationDatabaseIterator.php @@ -0,0 +1,103 @@ +langcode = $langcode; + } + else { + throw new Exception('Wrong langcode type for LocaleTranslationIterator::__construct().'); + } + + if ($this->langcode) { + $this->result = db_query("SELECT s.lid, s.source, s.context, s.location, t.translation FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = :language", array(':language' => $this->langcode)); + } + else { + $this->result = db_query("SELECT s.lid, s.source, s.context, s.location FROM {locales_source} s"); + } + } + + public function __destruct() { + // This may be a lot of data. Its worth not to wait for garbage collection. + unset($this->data); + } + + /** + * Implements Iterator::current(). + */ + public function current() { + return $this->data[$this->index]; + } + + /** + * Implements Iterator::key(). + */ + public function key() { + return $this->data[$this->index]->lid; + } + + /** + * Implements Iterator::next(). + */ + public function next() { + $this->index++; + $this->load(); + } + + /** + * Implements Iterator::rewind(). + */ + public function rewind() { + $this->index = 0; + $this->load(); + } + + /** + * Implements Iterator::valid(). + */ + public function valid() { + return $this->valid; + } + + /** + * Load translations from the database into memory. + * + * Keeping the data in memory is not required for forward only itteration. + * As long as this class is used in a foreach loop only, it can do without + * storing data in memory. But without memory storage, any other use of the + * class may beak the thing. + */ + private function load() { + if (!isset($this->data[$this->index])) { + $data = $this->result->fetchObject(); + $this->valid = !empty($data); + if ($this->valid) { + $this->data[$this->index] = $data; + } + } + else { + // Data comes from memory, so it is present. + $this->valid = TRUE; + } + } +} diff --git a/core/lib/Drupal/Core/Gettext/Writer.php b/core/lib/Drupal/Core/Gettext/Writer.php new file mode 100644 index 0000000..4a1f5d5 --- /dev/null +++ b/core/lib/Drupal/Core/Gettext/Writer.php @@ -0,0 +1,77 @@ + 1) { + return "\"\"\n\"" . implode("\"\n\"", $parts) . "\"\n"; + } + // Single line strings are output on the same line. + else { + return "\"$parts[0]\"\n"; + } + } + +} \ No newline at end of file