40f5464bf2f70a36c8f263e269f61f8d705b7374
[yaffs-website] / vendor / symfony / translation / Loader / PoFileLoader.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Translation\Loader;
13
14 /**
15  * @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/)
16  * @copyright Copyright (c) 2012, Clemens Tolboom
17  */
18 class PoFileLoader extends FileLoader
19 {
20     /**
21      * Parses portable object (PO) format.
22      *
23      * From http://www.gnu.org/software/gettext/manual/gettext.html#PO-Files
24      * we should be able to parse files having:
25      *
26      * white-space
27      * #  translator-comments
28      * #. extracted-comments
29      * #: reference...
30      * #, flag...
31      * #| msgid previous-untranslated-string
32      * msgid untranslated-string
33      * msgstr translated-string
34      *
35      * extra or different lines are:
36      *
37      * #| msgctxt previous-context
38      * #| msgid previous-untranslated-string
39      * msgctxt context
40      *
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
46      * ...
47      * msgstr[N] translated-string-case-n
48      *
49      * The definition states:
50      * - white-space and comments are optional.
51      * - msgid "" that an empty singleline defines a header.
52      *
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.
58      *
59      * Items with an empty id are ignored.
60      *
61      * {@inheritdoc}
62      */
63     protected function loadResource($resource)
64     {
65         $stream = fopen($resource, 'r');
66
67         $defaults = array(
68             'ids' => array(),
69             'translated' => null,
70         );
71
72         $messages = array();
73         $item = $defaults;
74         $flags = array();
75
76         while ($line = fgets($stream)) {
77             $line = trim($line);
78
79             if ($line === '') {
80                 // Whitespace indicated current item is done
81                 if (!in_array('fuzzy', $flags)) {
82                     $this->addMessage($messages, $item);
83                 }
84                 $item = $defaults;
85                 $flags = array();
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);
92                 $item = $defaults;
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';
98
99                 if (is_array($item[$continues])) {
100                     end($item[$continues]);
101                     $item[$continues][key($item[$continues])] .= substr($line, 1, -1);
102                 } else {
103                     $item[$continues] .= substr($line, 1, -1);
104                 }
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);
110             }
111         }
112         // save last item
113         if (!in_array('fuzzy', $flags)) {
114             $this->addMessage($messages, $item);
115         }
116         fclose($stream);
117
118         return $messages;
119     }
120
121     /**
122      * Save a translation item to the messages.
123      *
124      * A .po file could contain by error missing plural indexes. We need to
125      * fix these before saving them.
126      *
127      * @param array $messages
128      * @param array $item
129      */
130     private function addMessage(array &$messages, array $item)
131     {
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.
137                 ksort($plurals);
138                 // Make sure every index is filled.
139                 end($plurals);
140                 $count = key($plurals);
141                 // Fill missing spots with '-'.
142                 $empties = array_fill(0, $count + 1, '-');
143                 $plurals += $empties;
144                 ksort($plurals);
145                 $messages[stripcslashes($item['ids']['plural'])] = stripcslashes(implode('|', $plurals));
146             }
147         } elseif (!empty($item['ids']['singular'])) {
148             $messages[stripcslashes($item['ids']['singular'])] = stripcslashes($item['translated']);
149         }
150     }
151 }