You are here

private function PoFileLoader::parse in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/translation/Loader/PoFileLoader.php \Symfony\Component\Translation\Loader\PoFileLoader::parse()

Parses portable object (PO) format.

From http://www.gnu.org/software/gettext/manual/gettext.html#PO-Files we should be able to parse files having:

white-space # translator-comments #. extracted-comments #: reference... #, flag... #| msgid previous-untranslated-string msgid untranslated-string msgstr translated-string

extra or different lines are:

#| msgctxt previous-context #| msgid previous-untranslated-string msgctxt context

#| msgid previous-untranslated-string-singular #| msgid_plural previous-untranslated-string-plural msgid untranslated-string-singular msgid_plural untranslated-string-plural msgstr[0] translated-string-case-0 ... msgstr[N] translated-string-case-n

The definition states:

  • white-space and comments are optional.
  • msgid "" that an empty singleline defines a header.

This parser sacrifices some features of the reference implementation the differences to that implementation are as follows.

  • No support for comments spanning multiple lines.
  • Translator and extracted comments are treated as being the same type.
  • Message IDs are allowed to have other encodings as just US-ASCII.

Items with an empty id are ignored.

Parameters

resource $resource:

Return value

array

1 call to PoFileLoader::parse()
PoFileLoader::load in vendor/symfony/translation/Loader/PoFileLoader.php
Loads a locale.

File

vendor/symfony/translation/Loader/PoFileLoader.php, line 100

Class

PoFileLoader
@copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/) @copyright Copyright (c) 2012, Clemens Tolboom

Namespace

Symfony\Component\Translation\Loader

Code

private function parse($resource) {
  $stream = fopen($resource, 'r');
  $defaults = array(
    'ids' => array(),
    'translated' => null,
  );
  $messages = array();
  $item = $defaults;
  while ($line = fgets($stream)) {
    $line = trim($line);
    if ($line === '') {

      // Whitespace indicated current item is done
      $this
        ->addMessage($messages, $item);
      $item = $defaults;
    }
    elseif (substr($line, 0, 7) === 'msgid "') {

      // We start a new msg so save previous
      // TODO: this fails when comments or contexts are added
      $this
        ->addMessage($messages, $item);
      $item = $defaults;
      $item['ids']['singular'] = substr($line, 7, -1);
    }
    elseif (substr($line, 0, 8) === 'msgstr "') {
      $item['translated'] = substr($line, 8, -1);
    }
    elseif ($line[0] === '"') {
      $continues = isset($item['translated']) ? 'translated' : 'ids';
      if (is_array($item[$continues])) {
        end($item[$continues]);
        $item[$continues][key($item[$continues])] .= substr($line, 1, -1);
      }
      else {
        $item[$continues] .= substr($line, 1, -1);
      }
    }
    elseif (substr($line, 0, 14) === 'msgid_plural "') {
      $item['ids']['plural'] = substr($line, 14, -1);
    }
    elseif (substr($line, 0, 7) === 'msgstr[') {
      $size = strpos($line, ']');
      $item['translated'][(int) substr($line, 7, 1)] = substr($line, $size + 3, -1);
    }
  }

  // save last item
  $this
    ->addMessage($messages, $item);
  fclose($stream);
  return $messages;
}