PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/translation/Loader/PoFileLoader.php

https://gitlab.com/Pasantias/pasantiasASLG
PHP | 188 lines | 94 code | 19 blank | 75 comment | 13 complexity | 9389cca6a891d0e2f2a63373aa8bfecb MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Loader;
  11. use Symfony\Component\Translation\Exception\InvalidResourceException;
  12. use Symfony\Component\Translation\Exception\NotFoundResourceException;
  13. use Symfony\Component\Config\Resource\FileResource;
  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 ArrayLoader
  19. {
  20. public function load($resource, $locale, $domain = 'messages')
  21. {
  22. if (!stream_is_local($resource)) {
  23. throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
  24. }
  25. if (!file_exists($resource)) {
  26. throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
  27. }
  28. $messages = $this->parse($resource);
  29. // empty file
  30. if (null === $messages) {
  31. $messages = array();
  32. }
  33. // not an array
  34. if (!is_array($messages)) {
  35. throw new InvalidResourceException(sprintf('The file "%s" must contain a valid po file.', $resource));
  36. }
  37. $catalogue = parent::load($messages, $locale, $domain);
  38. if (class_exists('Symfony\Component\Config\Resource\FileResource')) {
  39. $catalogue->addResource(new FileResource($resource));
  40. }
  41. return $catalogue;
  42. }
  43. /**
  44. * Parses portable object (PO) format.
  45. *
  46. * From http://www.gnu.org/software/gettext/manual/gettext.html#PO-Files
  47. * we should be able to parse files having:
  48. *
  49. * white-space
  50. * # translator-comments
  51. * #. extracted-comments
  52. * #: reference...
  53. * #, flag...
  54. * #| msgid previous-untranslated-string
  55. * msgid untranslated-string
  56. * msgstr translated-string
  57. *
  58. * extra or different lines are:
  59. *
  60. * #| msgctxt previous-context
  61. * #| msgid previous-untranslated-string
  62. * msgctxt context
  63. *
  64. * #| msgid previous-untranslated-string-singular
  65. * #| msgid_plural previous-untranslated-string-plural
  66. * msgid untranslated-string-singular
  67. * msgid_plural untranslated-string-plural
  68. * msgstr[0] translated-string-case-0
  69. * ...
  70. * msgstr[N] translated-string-case-n
  71. *
  72. * The definition states:
  73. * - white-space and comments are optional.
  74. * - msgid "" that an empty singleline defines a header.
  75. *
  76. * This parser sacrifices some features of the reference implementation the
  77. * differences to that implementation are as follows.
  78. * - No support for comments spanning multiple lines.
  79. * - Translator and extracted comments are treated as being the same type.
  80. * - Message IDs are allowed to have other encodings as just US-ASCII.
  81. *
  82. * Items with an empty id are ignored.
  83. *
  84. * @param resource $resource
  85. *
  86. * @return array
  87. */
  88. private function parse($resource)
  89. {
  90. $stream = fopen($resource, 'r');
  91. $defaults = array(
  92. 'ids' => array(),
  93. 'translated' => null,
  94. );
  95. $messages = array();
  96. $item = $defaults;
  97. $flags = array();
  98. while ($line = fgets($stream)) {
  99. $line = trim($line);
  100. if ($line === '') {
  101. // Whitespace indicated current item is done
  102. if (!in_array('fuzzy', $flags)) {
  103. $this->addMessage($messages, $item);
  104. }
  105. $item = $defaults;
  106. $flags = array();
  107. } elseif (substr($line, 0, 2) === '#,') {
  108. $flags = array_map('trim', explode(',', substr($line, 2)));
  109. } elseif (substr($line, 0, 7) === 'msgid "') {
  110. // We start a new msg so save previous
  111. // TODO: this fails when comments or contexts are added
  112. $this->addMessage($messages, $item);
  113. $item = $defaults;
  114. $item['ids']['singular'] = substr($line, 7, -1);
  115. } elseif (substr($line, 0, 8) === 'msgstr "') {
  116. $item['translated'] = substr($line, 8, -1);
  117. } elseif ($line[0] === '"') {
  118. $continues = isset($item['translated']) ? 'translated' : 'ids';
  119. if (is_array($item[$continues])) {
  120. end($item[$continues]);
  121. $item[$continues][key($item[$continues])] .= substr($line, 1, -1);
  122. } else {
  123. $item[$continues] .= substr($line, 1, -1);
  124. }
  125. } elseif (substr($line, 0, 14) === 'msgid_plural "') {
  126. $item['ids']['plural'] = substr($line, 14, -1);
  127. } elseif (substr($line, 0, 7) === 'msgstr[') {
  128. $size = strpos($line, ']');
  129. $item['translated'][(int) substr($line, 7, 1)] = substr($line, $size + 3, -1);
  130. }
  131. }
  132. // save last item
  133. if (!in_array('fuzzy', $flags)) {
  134. $this->addMessage($messages, $item);
  135. }
  136. fclose($stream);
  137. return $messages;
  138. }
  139. /**
  140. * Save a translation item to the messages.
  141. *
  142. * A .po file could contain by error missing plural indexes. We need to
  143. * fix these before saving them.
  144. *
  145. * @param array $messages
  146. * @param array $item
  147. */
  148. private function addMessage(array &$messages, array $item)
  149. {
  150. if (is_array($item['translated'])) {
  151. $messages[stripcslashes($item['ids']['singular'])] = stripcslashes($item['translated'][0]);
  152. if (isset($item['ids']['plural'])) {
  153. $plurals = $item['translated'];
  154. // PO are by definition indexed so sort by index.
  155. ksort($plurals);
  156. // Make sure every index is filled.
  157. end($plurals);
  158. $count = key($plurals);
  159. // Fill missing spots with '-'.
  160. $empties = array_fill(0, $count + 1, '-');
  161. $plurals += $empties;
  162. ksort($plurals);
  163. $messages[stripcslashes($item['ids']['plural'])] = stripcslashes(implode('|', $plurals));
  164. }
  165. } elseif (!empty($item['ids']['singular'])) {
  166. $messages[stripcslashes($item['ids']['singular'])] = stripcslashes($item['translated']);
  167. }
  168. }
  169. }