diff --git a/core/lib/Drupal/Component/Gettext/PoStreamReader.php b/core/lib/Drupal/Component/Gettext/PoStreamReader.php index d138791..c30311f 100644 --- a/core/lib/Drupal/Component/Gettext/PoStreamReader.php +++ b/core/lib/Drupal/Component/Gettext/PoStreamReader.php @@ -105,6 +105,13 @@ class PoStreamReader implements PoStreamInterface, PoReaderInterface { private $_finished; /** + * Array of translated error strings recorded on reading this stream so far. + * + * @var array + */ + private $_errors; + + /** * Implements Drupal\Component\Gettext\PoMetadataInterface::getLangcode(). */ public function getLangcode() { @@ -247,6 +254,13 @@ class PoStreamReader implements PoStreamInterface, PoReaderInterface { // Track the line number for error reporting. $this->_line_number++; + // Initialize common values for error logging. + $log_vars = array( + '%uri' => $this->getURI(), + '%line' => $this->_line_number, + ); + $t = get_t(); + // Trim away the linefeed. \\n might appear at the end of the string if // another line continuing the same string follows. We can remove that. $line = trim(strtr($line, array("\\\n" => ""))); @@ -271,7 +285,7 @@ class PoStreamReader implements PoStreamInterface, PoReaderInterface { } else { // A comment following any other context is a syntax error. - $this->log('The translation stream %uri contains an error: "msgstr" was expected but not found on line %line.'); + $this->_errors[] = $t('The translation stream %uri contains an error: "msgstr" was expected but not found on line %line.', $log_vars); return FALSE; } return; @@ -281,7 +295,7 @@ class PoStreamReader implements PoStreamInterface, PoReaderInterface { if ($this->_context != 'MSGID') { // A plural form can only be added to an msgid directly. - $this->log('The translation stream %uri contains an error: "msgid_plural" was expected but not found on line %line.'); + $this->_errors[] = $t('The translation stream %uri contains an error: "msgid_plural" was expected but not found on line %line.', $log_vars); return FALSE; } @@ -292,7 +306,7 @@ class PoStreamReader implements PoStreamInterface, PoReaderInterface { $quoted = $this->parseQuoted($line); if ($quoted === FALSE) { // The plural form must be wrapped in quotes. - $this->log('The translation stream %uri contains a syntax error on line %line.'); + $this->_errors[] = $t('The translation stream %uri contains a syntax error on line %line.', $log_vars); return FALSE; } @@ -319,7 +333,7 @@ class PoStreamReader implements PoStreamInterface, PoReaderInterface { } elseif ($this->_context == 'MSGID') { // We are currently already in the context, meaning we passed an id with no data. - $this->log('The translation stream %uri contains an error: "msgid" is unexpected on line %line.'); + $this->_errors[] = $t('The translation stream %uri contains an error: "msgid" is unexpected on line %line.', $log_vars); return FALSE; } @@ -330,7 +344,7 @@ class PoStreamReader implements PoStreamInterface, PoReaderInterface { $quoted = $this->parseQuoted($line); if ($quoted === FALSE) { // The message id must be wrapped in quotes. - $this->log('The translation stream %uri contains an error: invalid format for "msgid" on line %line.'); + $this->_errors[] = $t('The translation stream %uri contains an error: invalid format for "msgid" on line %line.', $log_vars, $log_vars); return FALSE; } @@ -348,7 +362,7 @@ class PoStreamReader implements PoStreamInterface, PoReaderInterface { } elseif (!empty($this->_current_item['msgctxt'])) { // A context cannot apply to another context. - $this->log('The translation stream %uri contains an error: "msgctxt" is unexpected on line %line.'); + $this->_errors[] = $t('The translation stream %uri contains an error: "msgctxt" is unexpected on line %line.', $log_vars); return FALSE; } @@ -359,7 +373,7 @@ class PoStreamReader implements PoStreamInterface, PoReaderInterface { $quoted = $this->parseQuoted($line); if ($quoted === FALSE) { // The context string must be quoted. - $this->log('The translation stream %uri contains an error: invalid format for "msgctxt" on line %line.'); + $this->_errors[] = $t('The translation stream %uri contains an error: invalid format for "msgctxt" on line %line.', $log_vars); return FALSE; } @@ -374,13 +388,13 @@ class PoStreamReader implements PoStreamInterface, PoReaderInterface { if (($this->_context != 'MSGID') && ($this->_context != 'MSGCTXT') && ($this->_context != 'MSGID_PLURAL') && ($this->_context != 'MSGSTR_ARR')) { // Plural message strings must come after msgid, msgxtxt, // msgid_plural, or other msgstr[] entries. - $this->log('The translation stream %uri contains an error: "msgstr[]" is unexpected on line %line.'); + $this->_errors[] = $t('The translation stream %uri contains an error: "msgstr[]" is unexpected on line %line.', $log_vars); return FALSE; } // Ensure the plurality is terminated. if (strpos($line, ']') === FALSE) { - $this->log('The translation stream %uri contains an error: invalid format for "msgstr[]" on line %line.'); + $this->_errors[] = $t('The translation stream %uri contains an error: invalid format for "msgstr[]" on line %line.', $log_vars); return FALSE; } @@ -395,7 +409,7 @@ class PoStreamReader implements PoStreamInterface, PoReaderInterface { $quoted = $this->parseQuoted($line); if ($quoted === FALSE) { // The string must be quoted. - $this->log('The translation stream %uri contains an error: invalid format for "msgstr[]" on line %line.'); + $this->_errors[] = $t('The translation stream %uri contains an error: invalid format for "msgstr[]" on line %line.', $log_vars); return FALSE; } if (!isset($this->_current_item['msgstr']) || !is_array($this->_current_item['msgstr'])) { @@ -412,7 +426,7 @@ class PoStreamReader implements PoStreamInterface, PoReaderInterface { if (($this->_context != 'MSGID') && ($this->_context != 'MSGCTXT')) { // Strings are only valid within an id or context scope. - $this->log('The translation stream %uri contains an error: "msgstr" is unexpected on line %line.'); + $this->_errors[] = $t('The translation stream %uri contains an error: "msgstr" is unexpected on line %line.', $log_vars); return FALSE; } @@ -423,7 +437,7 @@ class PoStreamReader implements PoStreamInterface, PoReaderInterface { $quoted = $this->parseQuoted($line); if ($quoted === FALSE) { // The string must be quoted. - $this->log('The translation stream %uri contains an error: invalid format for "msgstr" on line %line.'); + $this->_errors[] = $t('The translation stream %uri contains an error: invalid format for "msgstr" on line %line.', $log_vars); return FALSE; } @@ -438,7 +452,7 @@ class PoStreamReader implements PoStreamInterface, PoReaderInterface { $quoted = $this->parseQuoted($line); if ($quoted === FALSE) { // This string must be quoted. - $this->log('The translation stream %uri contains an error: string continuation expected on line %line.'); + $this->_errors[] = $t('The translation stream %uri contains an error: string continuation expected on line %line.', $log_vars); return FALSE; } @@ -468,7 +482,7 @@ class PoStreamReader implements PoStreamInterface, PoReaderInterface { } else { // No valid context to append to. - $this->log('The translation stream %uri contains an error: unexpected string on line %line.'); + $this->_errors[] = $t('The translation stream %uri contains an error: unexpected string on line %line.', $log_vars); return FALSE; } return; @@ -481,27 +495,12 @@ class PoStreamReader implements PoStreamInterface, PoReaderInterface { $this->_current_item = array(); } elseif ($this->_context != 'COMMENT') { - $this->log('The translation stream %uri ended unexpectedly at line %line.'); + $this->_errors[] = $t('The translation stream %uri ended unexpectedly at line %line.', $log_vars); return FALSE; } } /** - * Sets an error message if an error occurred during PO stream parsing. - * - * @param string $message - * The message to be translated. - */ - protected function log($message) { - $vars = array( - '%uri' => $this->getURI(), - '%line' => $this->_line_number, - ); - $t = get_t(); - $this->errorLog[] = $t($message, $vars); - } - - /** * Store the parsed values as a PoItem object. */ public function setItemFromArray($value) { diff --git a/core/lib/Drupal/Component/Gettext/PoStreamWriter.php b/core/lib/Drupal/Component/Gettext/PoStreamWriter.php index 65df423..d066307 100644 --- a/core/lib/Drupal/Component/Gettext/PoStreamWriter.php +++ b/core/lib/Drupal/Component/Gettext/PoStreamWriter.php @@ -10,11 +10,13 @@ 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 { +class PoStreamWriter implements PoWriterInterface, PoStreamInterface { /** * URI of the PO stream that is being written. @@ -144,10 +146,7 @@ class PoStreamWriter implements PoWriterInterface { } /** - * Get the URI of the current stream. - * - * @return string - * The URI of the current stream. + * Implements Drupal\Component\Gettext\PoStreamInterface::getURI(). * * @throws Exception * If the URI is not set. @@ -160,10 +159,7 @@ class PoStreamWriter implements PoWriterInterface { } /** - * Set the URI to be used. - * - * @param string $uri - * Set the strem URI for writing. + * Implements Drupal\Component\Gettext\PoStreamInterface::setURI(). */ public function setURI($uri) { $this->_uri = $uri;