Commit 06aa2fac authored by Dries's avatar Dries

- Patch #1189184 by attiks, clemens.tolboom, penyaskito, Gábor Hojtsy,...

- Patch #1189184 by attiks, clemens.tolboom, penyaskito, Gábor Hojtsy, Sutharsan: OOP & PSR-0-ify gettext .po file parsing and generation.
parent b4f471e4
This diff is collapsed.
<?php
use Drupal\Core\Database\Database;
use Drupal\locale\Gettext;
/**
* Indicates that a module has not been installed yet.
......@@ -700,13 +701,7 @@ function st($string, array $args = array(), array $options = array()) {
// that multiple files end with the same extension, even if unlikely.
$files = install_find_translation_files($install_state['parameters']['langcode']);
if (!empty($files)) {
// Include cross-dependent code from locale module and gettext.inc.
require_once DRUPAL_ROOT . '/core/modules/locale/locale.module';
require_once DRUPAL_ROOT . '/core/includes/gettext.inc';
foreach ($files as $file) {
_locale_import_read_po('mem-store', $file);
}
$strings = _locale_import_one_string('mem-report');
$strings = Gettext::filesToArray($install_state['parameters']['langcode'], $files);
}
}
}
......
This diff is collapsed.
<?php
/**
* @file
* Definition of Drupal\Component\Gettext\PoItem.
*/
namespace Drupal\Component\Gettext;
/**
* PoItem handles one translation.
*
* @todo: This class contains some really old legacy code.
* @see https://drupal.org/node/1637662
*/
class PoItem {
/**
* The language code this translation is in.
*
* @car string
*/
private $_langcode;
/**
* The context this translation belongs to.
*
* @var string
*/
private $_context = '';
/**
* The source string or array of strings if it has plurals.
*
* @var string or array
* @see $_plural
*/
private $_source;
/**
* Flag indicating if this translation has plurals.
*
* @var boolean
*/
private $_plural;
/**
* The comment of this translation.
*
* @var string
*/
private $_comment;
/**
* The translation string or array of strings if it has plurals.
*
* @var string or array
* @see $_plural
*/
private $_translation;
/**
* Get the language code of the currently used language.
*
* @return string with langcode
*/
function getLangcode() {
return $this->_langcode;
}
/**
* Set the language code of the current language.
*
* @param string $langcode
*/
function setLangcode($langcode) {
$this->_langcode = $langcode;
}
/**
* Get the context this translation belongs to.
*
* @return string $context
*/
function getContext() {
return $this->_context;
}
/**
* Set the context this translation belongs to.
*
* @param string $context
*/
function setContext($context) {
$this->_context = $context;
}
/**
* Get the source string or the array of strings if the translation has
* plurals.
*
* @return string or array $translation
*/
function getSource() {
return $this->_source;
}
/**
* Set the source string or the array of strings if the translation has
* plurals.
*
* @param string or array $source
*/
function setSource($source) {
$this->_source = $source;
}
/**
* Get the translation string or the array of strings if the translation has
* plurals.
*
* @return string or array $translation
*/
function getTranslation() {
return $this->_translation;
}
/**
* Set the translation string or the array of strings if the translation has
* plurals.
*
* @param string or array $translation
*/
function setTranslation($translation) {
$this->_translation = $translation;
}
/**
* Set if the translation has plural values.
*
* @param boolean $plural
*/
function setPlural($plural) {
$this->_plural = $plural;
}
/**
* Get if the translation has plural values.
*
* @return boolean $plural
*/
function isPlural() {
return $this->_plural;
}
/**
* Get the comment of this translation.
*
* @return String $comment
*/
function getComment() {
return $this->_comment;
}
/**
* Set the comment of this translation.
*
* @param String $comment
*/
function setComment($comment) {
$this->_comment = $comment;
}
/**
* Create the PoItem from a structured array.
*
* @param array values
*/
public function setFromArray(array $values = array()) {
if (isset($values['context'])) {
$this->setContext($values['context']);
}
if (isset($values['source'])) {
$this->setSource($values['source']);
}
if (isset($values['translation'])) {
$this->setTranslation($values['translation']);
}
if (isset($values['comment'])){
$this->setComment($values['comment']);
}
if (isset($this->_source) &&
strpos($this->_source, LOCALE_PLURAL_DELIMITER) !== FALSE) {
$this->setSource(explode(LOCALE_PLURAL_DELIMITER, $this->_source));
$this->setTranslation(explode(LOCALE_PLURAL_DELIMITER, $this->_translation));
$this->setPlural(count($this->_translation) > 1);
}
}
/**
* Output the PoItem as a string.
*/
public function __toString() {
return $this->formatItem();
}
/**
* Format the POItem as a string.
*/
private function formatItem() {
$output = '';
// Format string context.
if (!empty($this->_context)) {
$output .= 'msgctxt ' . $this->formatString($this->_context);
}
// Format translation.
if ($this->_plural) {
$output .= $this->formatPlural();
}
else {
$output .= $this->formatSingular();
}
// Add one empty line to separate the translations.
$output .= "\n";
return $output;
}
/**
* Formats a plural translation.
*/
private function formatPlural() {
$output = '';
// Format source strings.
$output .= 'msgid ' . $this->formatString($this->_source[0]);
$output .= 'msgid_plural ' . $this->formatString($this->_source[1]);
foreach ($this->_translation as $i => $trans) {
if (isset($this->_translation[$i])) {
$output .= 'msgstr[' . $i . '] ' . $this->formatString($trans);
}
else {
$output .= 'msgstr[' . $i . '] ""' . "\n";
}
}
return $output;
}
/**
* Formats a singular translation.
*/
private function formatSingular() {
$output = '';
$output .= 'msgid '. $this->formatString($this->_source);
$output .= 'msgstr '. (isset($this->_translation) ? $this->formatString($this->_translation) : '');
return $output;
}
/**
* Formats a string for output on multiple lines.
*/
private function formatString($string) {
// Escape characters for processing.
$string = addcslashes($string, "\0..\37\\\"");
// Always include a line break after the explicit \n line breaks from
// the source string. Otherwise wrap at 70 chars to accommodate the extra
// format overhead too.
$parts = explode("\n", wordwrap(str_replace('\n', "\\n\n", $string), 70, " \n"));
// Multiline string should be exported starting with a "" and newline to
// have all lines aligned on the same column.
if (count($parts) > 1) {
return "\"\"\n\"" . implode("\"\n\"", $parts) . "\"\n";
}
// Single line strings are output on the same line.
else {
return "\"$parts[0]\"\n";
}
}
}
<?php
/**
* @file
* Definition of Drupal\Component\Gettext\PoMemoryWriter.
*/
namespace Drupal\Component\Gettext;
use Drupal\Component\Gettext\PoWriterInterface;
use Drupal\Component\Gettext\PoHeader;
use Drupal\Component\Gettext\PoItem;
/**
* Defines a Gettext PO memory writer, to be used by the installer.
*/
class PoMemoryWriter implements PoWriterInterface {
/**
* Array to hold all PoItem elements.
*
* @var array
*/
private $_items;
/**
* Constructor, initialize empty items.
*/
function __construct() {
$this->_items = array();
}
/**
* Implements Drupal\Component\Gettext\PoWriterInterface::writeItem().
*/
public function writeItem(PoItem $item) {
if (is_array($item->source)) {
$item->source = implode(LOCALE_PLURAL_DELIMITER, $item->source);
$item->translation = implode(LOCALE_PLURAL_DELIMITER, $item->translation);
}
$this->_items[isset($item->context) ? $item->context : ''][$item->source] = $item->translation;
}
/**
* Implements Drupal\Component\Gettext\PoWriterInterface::writeItems().
*/
public function writeItems(PoReaderInterface $reader, $count = -1) {
$forever = $count == -1;
while (($count-- > 0 || $forever) && ($item = $reader->readItem())) {
$this->writeItem($item);
}
}
/**
* Get all stored PoItem's.
*
* @return array PoItem
*/
public function getData() {
return $this->_items;
}
/**
* Implements Drupal\Component\Gettext\PoMetadataInterface:setLangcode().
*
* Not implemented. Not relevant for the MemoryWriter.
*/
function setLangcode($langcode) {
}
/**
* Implements Drupal\Component\Gettext\PoMetadataInterface:getLangcode().
*
* Not implemented. Not relevant for the MemoryWriter.
*/
function getLangcode() {
}
/**
* Implements Drupal\Component\Gettext\PoMetadataInterface:getHeader().
*
* Not implemented. Not relevant for the MemoryWriter.
*/
function getHeader() {
}
/**
* Implements Drupal\Component\Gettext\PoMetadataInterface:setHeader().
*
* Not implemented. Not relevant for the MemoryWriter.
*/
function setHeader(PoHeader $header) {
}
}
<?php
/**
* @file
* Definition of Drupal\Component\Gettext\PoMetadataInterface.
*/
namespace Drupal\Component\Gettext;
use Drupal\Component\Gettext\PoHeader;
/**
* Methods required for both reader and writer implementations.
*
* @see Drupal\Component\Gettext\PoReaderInterface
* @see Drupal\Component\Gettext\PoWriterInterface
*/
interface PoMetadataInterface {
/**
* Set language code.
*
* @param string $langcode
* Language code string.
*/
function setLangcode($langcode);
/**
* Get language code.
*
* @return string
* Language code string.
*/
function getLangcode();
/**
* Set header metadata.
*
* @param Drupal\Component\Gettext\PoHeader $header
* Header object representing metadata in a PO header.
*/
function setHeader(PoHeader $header);
/**
* Get header metadata.
*
* @return Drupal\Component\Gettext\PoHeader $header
* Header instance representing metadata in a PO header.
*/
function getHeader();
}
<?php
/**
* @file
* Definition of Drupal\Component\Gettext\PoReaderInterface.
*/
namespace Drupal\Component\Gettext;
use Drupal\Component\Gettext\PoMetadataInterface;
/**
* Shared interface definition for all Gettext PO Readers.
*/
interface PoReaderInterface extends PoMetadataInterface {
/**
* Reads and returns a PoItem (source/translation pair).
*
* @return Drupal\Component\Gettext\PoItem
* Wrapper for item data instance.
*/
function readItem();
}
<?php
/**
* @file
* Definition of Drupal\Component\Gettext\PoStreamInterface.
*/
namespace Drupal\Component\Gettext;
/**
* Common functions for file/stream based PO readers/writers.
*
* @see PoReaderInterface
* @see PoWriterInterface
*/
interface PoStreamInterface {
/**
* Open the stream. Set the URI for the stream earlier with setURI().
*/
function open();
/**
* Close the stream.
*/
function close();
/**
* Get the URI of the PO stream that is being read or written.
*
* @return
* URI string for this stream.
*/
function getURI();
/**
* Set the URI of the PO stream that is going to be read or written.
*
* @param $uri
* URI string to set for this stream.
*/
function setURI($uri);
}
This diff is collapsed.
<?php
/**
* @file
* Definition of Drupal\Component\Gettext\PoStreamWriter.
*/
namespace Drupal\Component\Gettext;
use Drupal\Component\Gettext\PoHeader;
use Drupal\Component\Gettext\PoItem;
use Drupal\Component\Gettext\PoReaderInterface;
use Drupal\Component\Gettext\PoWriterInterface;
use Drupal\Component\Gettext\PoStreamInterface;
/**
* Defines a Gettext PO stream writer.
*/
class PoStreamWriter implements PoWriterInterface, PoStreamInterface {
/**
* URI of the PO stream that is being written.
*
* @var string
*/
private $_uri;
/**
* The Gettext PO header.
*
* @var Drupal\Component\Gettext\PoHeader
*/
private $_header;
/**
* File handle of the current PO stream.
*
* @var resource
*/
private $_fd;
/**
* Get the PO header of the current stream.
*
* @return Drupal\Component\Gettext\PoHeader
* The Gettext PO header.
*/
public function getHeader() {
return $this->_header;
}
/**
* Set the PO header for the current stream.
*
* @param Drupal\Component\Gettext\PoHeader $header
* The Gettext PO header to set.
*/
public function setHeader(PoHeader $header) {
$this->_header = $header;
}
/**
* Get the current language code used.
*
* @return string
* The language code.
*/
public function getLangcode() {
return $this->_langcode;
}
/**
* Set the language code.
*
* @param string $langcode
* The language code.
*/
public function setLangcode($langcode) {
$this->_langcode = $langcode;
}
/**
* Implements Drupal\Component\Gettext\PoStreamInterface::open().
*/
public function open() {
// Open in write mode. Will overwrite the stream if it already exists.
$this->_fd = fopen($this->getURI(), 'w');
// Write the header at the start.
$this->writeHeader();
}
/**
* Implements Drupal\Component\Gettext\PoStreamInterface::close().
*
* @throws Exception
* If the stream is not open.
*/
public function close() {
if ($this->_fd) {
fclose($this->_fd);
}
else {
throw new Exception('Cannot close stream that is not open.');
}
}
/**
* Write data to the stream.
*
* @param string $data
* Piece of string to write to the stream. If the value is not directly a
* string, casting will happen in writing.
*
* @throws Exception
* If writing the data is not possible.
*/
private function write($data) {
$result = fputs($this->_fd, $data);
if ($result === FALSE) {
throw new Exception('Unable to write data: ' . substr($data, 0, 20));
}
}
/**
* Write the PO header to the stream.
*/
private function writeHeader() {
$this->write($this->_header);
}
/**
* Implements Drupal\Component\Gettext\PoWriterInterface::writeItem().
*/
public function writeItem(PoItem $item) {
$this->write($item);
}
/**
* Implements Drupal\Component\Gettext\PoWriterInterface::writeItems().
*/
public function writeItems(PoReaderInterface $reader, $count = -1) {
$forever = $count == -1;
while (($count-- > 0 || $forever) && ($item = $reader->readItem())) {
$this->writeItem($item);
}
}
/**
* Implements Drupal\Component\Gettext\PoStreamInterface::getURI().
*
* @throws Exception
* If the URI is not set.
*/
public function getURI() {
if (empty($this->_uri)) {
throw new Exception('No URI set.');
}
return $this->_uri;
}
/**
* Implements Drupal\Component\Gettext\PoStreamInterface::setURI().
*/
public function setURI($uri) {
$this->_uri = $uri;
}
}
<?php
/**
* @file
* Definition of Drupal\Component\Gettext\PoWriterInterface.
*/
namespace Drupal\Component\Gettext;
use Drupal\Component\Gettext\PoMetadataInterface;
use Drupal\Component\Gettext\PoItem;
/**
* Shared interface definition for all Gettext PO Writers.
*/
interface PoWriterInterface extends PoMetadataInterface {
/**
* Writes the given item.
*
* @param PoItem $item
* One specific item to write.
*/
function writeItem(PoItem $item);
/**
* Writes all or the given amount of items.
*
* @param PoReaderInterface $reader
* Reader to read PoItems from.