4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation\Loader
;
15 * @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/)
16 * @copyright Copyright (c) 2012, Clemens Tolboom
18 class PoFileLoader
extends FileLoader
21 * Parses portable object (PO) format.
23 * From http://www.gnu.org/software/gettext/manual/gettext.html#PO-Files
24 * we should be able to parse files having:
27 * # translator-comments
28 * #. extracted-comments
31 * #| msgid previous-untranslated-string
32 * msgid untranslated-string
33 * msgstr translated-string
35 * extra or different lines are:
37 * #| msgctxt previous-context
38 * #| msgid previous-untranslated-string
41 * #| msgid previous-untranslated-string-singular
42 * #| msgid_plural previous-untranslated-string-plural
43 * msgid untranslated-string-singular
44 * msgid_plural untranslated-string-plural
45 * msgstr[0] translated-string-case-0
47 * msgstr[N] translated-string-case-n
49 * The definition states:
50 * - white-space and comments are optional.
51 * - msgid "" that an empty singleline defines a header.
53 * This parser sacrifices some features of the reference implementation the
54 * differences to that implementation are as follows.
55 * - No support for comments spanning multiple lines.
56 * - Translator and extracted comments are treated as being the same type.
57 * - Message IDs are allowed to have other encodings as just US-ASCII.
59 * Items with an empty id are ignored.
63 protected function loadResource($resource)
65 $stream = fopen($resource, 'r');
76 while ($line = fgets($stream)) {
80 // Whitespace indicated current item is done
81 if (!in_array('fuzzy', $flags)) {
82 $this->addMessage($messages, $item);
86 } elseif (substr($line, 0, 2) === '#,') {
87 $flags = array_map('trim', explode(',', substr($line, 2)));
88 } elseif (substr($line, 0, 7) === 'msgid "') {
89 // We start a new msg so save previous
90 // TODO: this fails when comments or contexts are added
91 $this->addMessage($messages, $item);
93 $item['ids']['singular'] = substr($line, 7, -1);
94 } elseif (substr($line, 0, 8) === 'msgstr "') {
95 $item['translated'] = substr($line, 8, -1);
96 } elseif ($line[0] === '"') {
97 $continues = isset($item['translated']) ?
'translated' : 'ids';
99 if (is_array($item[$continues])) {
100 end($item[$continues]);
101 $item[$continues][key($item[$continues])] .= substr($line, 1, -1);
103 $item[$continues] .= substr($line, 1, -1);
105 } elseif (substr($line, 0, 14) === 'msgid_plural "') {
106 $item['ids']['plural'] = substr($line, 14, -1);
107 } elseif (substr($line, 0, 7) === 'msgstr[') {
108 $size = strpos($line, ']');
109 $item['translated'][(int) substr($line, 7, 1)] = substr($line, $size +
3, -1);
113 if (!in_array('fuzzy', $flags)) {
114 $this->addMessage($messages, $item);
122 * Save a translation item to the messages.
124 * A .po file could contain by error missing plural indexes. We need to
125 * fix these before saving them.
127 * @param array $messages
130 private function addMessage(array &$messages, array $item)
132 if (is_array($item['translated'])) {
133 $messages[stripcslashes($item['ids']['singular'])] = stripcslashes($item['translated'][0]);
134 if (isset($item['ids']['plural'])) {
135 $plurals = $item['translated'];
136 // PO are by definition indexed so sort by index.
138 // Make sure every index is filled.
140 $count = key($plurals);
141 // Fill missing spots with '-'.
142 $empties = array_fill(0, $count +
1, '-');
143 $plurals +
= $empties;
145 $messages[stripcslashes($item['ids']['plural'])] = stripcslashes(implode('|', $plurals));
147 } elseif (!empty($item['ids']['singular'])) {
148 $messages[stripcslashes($item['ids']['singular'])] = stripcslashes($item['translated']);