namespace Drupal\Component\Gettext;
-use Drupal\Component\Utility\SafeMarkup;
+use Drupal\Component\Render\FormattableMarkup;
/**
* Implements Gettext PO stream reader.
*
* @var int
*/
- private $_line_number = 0;
+ protected $lineNumber = 0;
/**
* Parser context for the stream reader state machine.
*
* @var string
*/
- private $_context = 'COMMENT';
+ protected $context = 'COMMENT';
/**
* Current entry being read. Incomplete.
*
* @var array
*/
- private $_current_item = [];
+ protected $currentItem = [];
/**
* Current plural index for plural translations.
*
* @var int
*/
- private $_current_plural_index = 0;
+ protected $currentPluralIndex = 0;
/**
* URI of the PO stream that is being read.
*
* @var string
*/
- private $_uri = '';
+ protected $uri = '';
/**
* Language code for the PO stream being read.
*
* @var string
*/
- private $_langcode = NULL;
+ protected $langcode = NULL;
/**
* File handle of the current PO stream.
*
* @var resource
*/
- private $_fd;
+ protected $fd;
/**
* The PO stream header.
*
* @var \Drupal\Component\Gettext\PoHeader
*/
- private $_header;
+ protected $header;
/**
* Object wrapper for the last read source/translation pair.
*
* @var \Drupal\Component\Gettext\PoItem
*/
- private $_last_item;
+ protected $lastItem;
/**
* Indicator of whether the stream reading is finished.
*
* @var bool
*/
- private $_finished;
+ protected $finished;
/**
* Array of translated error strings recorded on reading this stream so far.
*
* @var array
*/
- private $_errors;
+ protected $errors;
/**
* {@inheritdoc}
*/
public function getLangcode() {
- return $this->_langcode;
+ return $this->langcode;
}
/**
* {@inheritdoc}
*/
public function setLangcode($langcode) {
- $this->_langcode = $langcode;
+ $this->langcode = $langcode;
}
/**
* {@inheritdoc}
*/
public function getHeader() {
- return $this->_header;
+ return $this->header;
}
/**
* {@inheritdoc}
*/
public function getURI() {
- return $this->_uri;
+ return $this->uri;
}
/**
* {@inheritdoc}
*/
public function setURI($uri) {
- $this->_uri = $uri;
+ $this->uri = $uri;
}
/**
* If the URI is not yet set.
*/
public function open() {
- if (!empty($this->_uri)) {
- $this->_fd = fopen($this->_uri, 'rb');
+ if (!empty($this->uri)) {
+ $this->fd = fopen($this->uri, 'rb');
$this->readHeader();
}
else {
* If the stream is not open.
*/
public function close() {
- if ($this->_fd) {
- fclose($this->_fd);
+ if ($this->fd) {
+ fclose($this->fd);
}
else {
throw new \Exception('Cannot close stream that is not open.');
*/
public function readItem() {
// Clear out the last item.
- $this->_last_item = NULL;
+ $this->lastItem = NULL;
// Read until finished with the stream or a complete item was identified.
- while (!$this->_finished && is_null($this->_last_item)) {
+ while (!$this->finished && is_null($this->lastItem)) {
$this->readLine();
}
- return $this->_last_item;
+ return $this->lastItem;
}
/**
* The new seek position to set.
*/
public function setSeek($seek) {
- fseek($this->_fd, $seek);
+ fseek($this->fd, $seek);
}
/**
* Gets the pointer position of the current PO stream.
*/
public function getSeek() {
- return ftell($this->_fd);
+ return ftell($this->fd);
}
/**
}
$header = new PoHeader();
$header->setFromString(trim($item->getTranslation()));
- $this->_header = $header;
+ $this->header = $header;
}
/**
* Reads a line from the PO stream and stores data internally.
*
- * Expands $this->_current_item based on new data for the current item. If
+ * Expands $this->current_item based on new data for the current item. If
* this line ends the current item, it is saved with setItemFromArray() with
- * data from $this->_current_item.
+ * data from $this->current_item.
*
* An internal state machine is maintained in this reader using
- * $this->_context as the reading state. PO items are in between COMMENT
+ * $this->context as the reading state. PO items are in between COMMENT
* states (when items have at least one line or comment in between them) or
* indicated by MSGSTR or MSGSTR_ARR followed immediately by an MSGID or
* MSGCTXT (when items closely follow each other).
private function readLine() {
// Read a line and set the stream finished indicator if it was not
// possible anymore.
- $line = fgets($this->_fd);
- $this->_finished = ($line === FALSE);
+ $line = fgets($this->fd);
+ $this->finished = ($line === FALSE);
- if (!$this->_finished) {
+ if (!$this->finished) {
- if ($this->_line_number == 0) {
+ if ($this->lineNumber == 0) {
// The first line might come with a UTF-8 BOM, which should be removed.
$line = str_replace("\xEF\xBB\xBF", '', $line);
// Current plurality for 'msgstr[]'.
- $this->_current_plural_index = 0;
+ $this->currentPluralIndex = 0;
}
// Track the line number for error reporting.
- $this->_line_number++;
+ $this->lineNumber++;
// Initialize common values for error logging.
$log_vars = [
'%uri' => $this->getURI(),
- '%line' => $this->_line_number,
+ '%line' => $this->lineNumber,
];
// Trim away the linefeed. \\n might appear at the end of the string if
if (!strncmp('#', $line, 1)) {
// Lines starting with '#' are comments.
- if ($this->_context == 'COMMENT') {
+ if ($this->context == 'COMMENT') {
// Already in comment context, add to current comment.
- $this->_current_item['#'][] = substr($line, 1);
+ $this->currentItem['#'][] = substr($line, 1);
}
- elseif (($this->_context == 'MSGSTR') || ($this->_context == 'MSGSTR_ARR')) {
+ elseif (($this->context == 'MSGSTR') || ($this->context == 'MSGSTR_ARR')) {
// We are currently in string context, save current item.
- $this->setItemFromArray($this->_current_item);
+ $this->setItemFromArray($this->currentItem);
// Start a new entry for the comment.
- $this->_current_item = [];
- $this->_current_item['#'][] = substr($line, 1);
+ $this->currentItem = [];
+ $this->currentItem['#'][] = substr($line, 1);
- $this->_context = 'COMMENT';
+ $this->context = 'COMMENT';
return;
}
else {
// A comment following any other context is a syntax error.
- $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: "msgstr" was expected but not found on line %line.', $log_vars);
+ $this->errors[] = new FormattableMarkup('The translation stream %uri contains an error: "msgstr" was expected but not found on line %line.', $log_vars);
return FALSE;
}
return;
elseif (!strncmp('msgid_plural', $line, 12)) {
// A plural form for the current source string.
- if ($this->_context != 'MSGID') {
+ if ($this->context != 'MSGID') {
// A plural form can only be added to an msgid directly.
- $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: "msgid_plural" was expected but not found on line %line.', $log_vars);
+ $this->errors[] = new FormattableMarkup('The translation stream %uri contains an error: "msgid_plural" was expected but not found on line %line.', $log_vars);
return FALSE;
}
$quoted = $this->parseQuoted($line);
if ($quoted === FALSE) {
// The plural form must be wrapped in quotes.
- $this->_errors[] = SafeMarkup::format('The translation stream %uri contains a syntax error on line %line.', $log_vars);
+ $this->errors[] = new FormattableMarkup('The translation stream %uri contains a syntax error on line %line.', $log_vars);
return FALSE;
}
// Append the plural source to the current entry.
- if (is_string($this->_current_item['msgid'])) {
+ if (is_string($this->currentItem['msgid'])) {
// The first value was stored as string. Now we know the context is
// plural, it is converted to array.
- $this->_current_item['msgid'] = [$this->_current_item['msgid']];
+ $this->currentItem['msgid'] = [$this->currentItem['msgid']];
}
- $this->_current_item['msgid'][] = $quoted;
+ $this->currentItem['msgid'][] = $quoted;
- $this->_context = 'MSGID_PLURAL';
+ $this->context = 'MSGID_PLURAL';
return;
}
elseif (!strncmp('msgid', $line, 5)) {
// Starting a new message.
- if (($this->_context == 'MSGSTR') || ($this->_context == 'MSGSTR_ARR')) {
+ if (($this->context == 'MSGSTR') || ($this->context == 'MSGSTR_ARR')) {
// We are currently in string context, save current item.
- $this->setItemFromArray($this->_current_item);
+ $this->setItemFromArray($this->currentItem);
// Start a new context for the msgid.
- $this->_current_item = [];
+ $this->currentItem = [];
}
- elseif ($this->_context == 'MSGID') {
+ elseif ($this->context == 'MSGID') {
// We are currently already in the context, meaning we passed an id with no data.
- $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: "msgid" is unexpected on line %line.', $log_vars);
+ $this->errors[] = new FormattableMarkup('The translation stream %uri contains an error: "msgid" is unexpected on line %line.', $log_vars);
return FALSE;
}
$quoted = $this->parseQuoted($line);
if ($quoted === FALSE) {
// The message id must be wrapped in quotes.
- $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: invalid format for "msgid" on line %line.', $log_vars, $log_vars);
+ $this->errors[] = new FormattableMarkup('The translation stream %uri contains an error: invalid format for "msgid" on line %line.', $log_vars, $log_vars);
return FALSE;
}
- $this->_current_item['msgid'] = $quoted;
- $this->_context = 'MSGID';
+ $this->currentItem['msgid'] = $quoted;
+ $this->context = 'MSGID';
return;
}
elseif (!strncmp('msgctxt', $line, 7)) {
// Starting a new context.
- if (($this->_context == 'MSGSTR') || ($this->_context == 'MSGSTR_ARR')) {
+ if (($this->context == 'MSGSTR') || ($this->context == 'MSGSTR_ARR')) {
// We are currently in string context, save current item.
- $this->setItemFromArray($this->_current_item);
- $this->_current_item = [];
+ $this->setItemFromArray($this->currentItem);
+ $this->currentItem = [];
}
- elseif (!empty($this->_current_item['msgctxt'])) {
+ elseif (!empty($this->currentItem['msgctxt'])) {
// A context cannot apply to another context.
- $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: "msgctxt" is unexpected on line %line.', $log_vars);
+ $this->errors[] = new FormattableMarkup('The translation stream %uri contains an error: "msgctxt" is unexpected on line %line.', $log_vars);
return FALSE;
}
$quoted = $this->parseQuoted($line);
if ($quoted === FALSE) {
// The context string must be quoted.
- $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: invalid format for "msgctxt" on line %line.', $log_vars);
+ $this->errors[] = new FormattableMarkup('The translation stream %uri contains an error: invalid format for "msgctxt" on line %line.', $log_vars);
return FALSE;
}
- $this->_current_item['msgctxt'] = $quoted;
+ $this->currentItem['msgctxt'] = $quoted;
- $this->_context = 'MSGCTXT';
+ $this->context = 'MSGCTXT';
return;
}
elseif (!strncmp('msgstr[', $line, 7)) {
// A message string for a specific plurality.
- if (($this->_context != 'MSGID') &&
- ($this->_context != 'MSGCTXT') &&
- ($this->_context != 'MSGID_PLURAL') &&
- ($this->_context != 'MSGSTR_ARR')) {
- // Plural message strings must come after msgid, msgxtxt,
+ if (($this->context != 'MSGID') &&
+ ($this->context != 'MSGCTXT') &&
+ ($this->context != 'MSGID_PLURAL') &&
+ ($this->context != 'MSGSTR_ARR')) {
+ // Plural message strings must come after msgid, msgctxt,
// msgid_plural, or other msgstr[] entries.
- $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: "msgstr[]" is unexpected on line %line.', $log_vars);
+ $this->errors[] = new FormattableMarkup('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->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: invalid format for "msgstr[]" on line %line.', $log_vars);
+ $this->errors[] = new FormattableMarkup('The translation stream %uri contains an error: invalid format for "msgstr[]" on line %line.', $log_vars);
return FALSE;
}
// Extract the plurality.
$frombracket = strstr($line, '[');
- $this->_current_plural_index = substr($frombracket, 1, strpos($frombracket, ']') - 1);
+ $this->currentPluralIndex = substr($frombracket, 1, strpos($frombracket, ']') - 1);
// Skip to the next whitespace and trim away any further whitespace,
// bringing $line to the message text only.
$quoted = $this->parseQuoted($line);
if ($quoted === FALSE) {
// The string must be quoted.
- $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: invalid format for "msgstr[]" on line %line.', $log_vars);
+ $this->errors[] = new FormattableMarkup('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'])) {
- $this->_current_item['msgstr'] = [];
+ if (!isset($this->currentItem['msgstr']) || !is_array($this->currentItem['msgstr'])) {
+ $this->currentItem['msgstr'] = [];
}
- $this->_current_item['msgstr'][$this->_current_plural_index] = $quoted;
+ $this->currentItem['msgstr'][$this->currentPluralIndex] = $quoted;
- $this->_context = 'MSGSTR_ARR';
+ $this->context = 'MSGSTR_ARR';
return;
}
elseif (!strncmp("msgstr", $line, 6)) {
// A string pair for an msgid (with optional context).
- if (($this->_context != 'MSGID') && ($this->_context != 'MSGCTXT')) {
+ if (($this->context != 'MSGID') && ($this->context != 'MSGCTXT')) {
// Strings are only valid within an id or context scope.
- $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: "msgstr" is unexpected on line %line.', $log_vars);
+ $this->errors[] = new FormattableMarkup('The translation stream %uri contains an error: "msgstr" is unexpected on line %line.', $log_vars);
return FALSE;
}
$quoted = $this->parseQuoted($line);
if ($quoted === FALSE) {
// The string must be quoted.
- $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: invalid format for "msgstr" on line %line.', $log_vars);
+ $this->errors[] = new FormattableMarkup('The translation stream %uri contains an error: invalid format for "msgstr" on line %line.', $log_vars);
return FALSE;
}
- $this->_current_item['msgstr'] = $quoted;
+ $this->currentItem['msgstr'] = $quoted;
- $this->_context = 'MSGSTR';
+ $this->context = 'MSGSTR';
return;
}
elseif ($line != '') {
$quoted = $this->parseQuoted($line);
if ($quoted === FALSE) {
// This string must be quoted.
- $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: string continuation expected on line %line.', $log_vars);
+ $this->errors[] = new FormattableMarkup('The translation stream %uri contains an error: string continuation expected on line %line.', $log_vars);
return FALSE;
}
// Append the string to the current item.
- if (($this->_context == 'MSGID') || ($this->_context == 'MSGID_PLURAL')) {
- if (is_array($this->_current_item['msgid'])) {
+ if (($this->context == 'MSGID') || ($this->context == 'MSGID_PLURAL')) {
+ if (is_array($this->currentItem['msgid'])) {
// Add string to last array element for plural sources.
- $last_index = count($this->_current_item['msgid']) - 1;
- $this->_current_item['msgid'][$last_index] .= $quoted;
+ $last_index = count($this->currentItem['msgid']) - 1;
+ $this->currentItem['msgid'][$last_index] .= $quoted;
}
else {
// Singular source, just append the string.
- $this->_current_item['msgid'] .= $quoted;
+ $this->currentItem['msgid'] .= $quoted;
}
}
- elseif ($this->_context == 'MSGCTXT') {
+ elseif ($this->context == 'MSGCTXT') {
// Multiline context name.
- $this->_current_item['msgctxt'] .= $quoted;
+ $this->currentItem['msgctxt'] .= $quoted;
}
- elseif ($this->_context == 'MSGSTR') {
+ elseif ($this->context == 'MSGSTR') {
// Multiline translation string.
- $this->_current_item['msgstr'] .= $quoted;
+ $this->currentItem['msgstr'] .= $quoted;
}
- elseif ($this->_context == 'MSGSTR_ARR') {
+ elseif ($this->context == 'MSGSTR_ARR') {
// Multiline plural translation string.
- $this->_current_item['msgstr'][$this->_current_plural_index] .= $quoted;
+ $this->currentItem['msgstr'][$this->currentPluralIndex] .= $quoted;
}
else {
// No valid context to append to.
- $this->_errors[] = SafeMarkup::format('The translation stream %uri contains an error: unexpected string on line %line.', $log_vars);
+ $this->errors[] = new FormattableMarkup('The translation stream %uri contains an error: unexpected string on line %line.', $log_vars);
return FALSE;
}
return;
}
// Empty line read or EOF of PO stream, close out the last entry.
- if (($this->_context == 'MSGSTR') || ($this->_context == 'MSGSTR_ARR')) {
- $this->setItemFromArray($this->_current_item);
- $this->_current_item = [];
+ if (($this->context == 'MSGSTR') || ($this->context == 'MSGSTR_ARR')) {
+ $this->setItemFromArray($this->currentItem);
+ $this->currentItem = [];
}
- elseif ($this->_context != 'COMMENT') {
- $this->_errors[] = SafeMarkup::format('The translation stream %uri ended unexpectedly at line %line.', $log_vars);
+ elseif ($this->context != 'COMMENT') {
+ $this->errors[] = new FormattableMarkup('The translation stream %uri ended unexpectedly at line %line.', $log_vars);
return FALSE;
}
$item->setTranslation($value['msgstr']);
$item->setPlural($plural);
$item->setComment($comments);
- $item->setLangcode($this->_langcode);
+ $item->setLangcode($this->langcode);
- $this->_last_item = $item;
+ $this->lastItem = $item;
- $this->_context = 'COMMENT';
+ $this->context = 'COMMENT';
}
/**