composer package updates
[openemr.git] / vendor / symfony / translation / Loader / PoFileLoader.php
blob40f5464bf2f70a36c8f263e269f61f8d705b7374
1 <?php
3 /*
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;
14 /**
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
20 /**
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:
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
35 * extra or different lines are:
37 * #| msgctxt previous-context
38 * #| msgid previous-untranslated-string
39 * msgctxt context
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
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.
61 * {@inheritdoc}
63 protected function loadResource($resource)
65 $stream = fopen($resource, 'r');
67 $defaults = array(
68 'ids' => array(),
69 'translated' => null,
72 $messages = array();
73 $item = $defaults;
74 $flags = array();
76 while ($line = fgets($stream)) {
77 $line = trim($line);
79 if ($line === '') {
80 // Whitespace indicated current item is done
81 if (!in_array('fuzzy', $flags)) {
82 $this->addMessage($messages, $item);
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';
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);
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);
112 // save last item
113 if (!in_array('fuzzy', $flags)) {
114 $this->addMessage($messages, $item);
116 fclose($stream);
118 return $messages;
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
128 * @param array $item
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.
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));
147 } elseif (!empty($item['ids']['singular'])) {
148 $messages[stripcslashes($item['ids']['singular'])] = stripcslashes($item['translated']);