PoFileLoader.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 implements LoaderInterface
  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. $catalogue->addResource(new FileResource($resource));
  39. return $catalogue;
  40. }
  41. /**
  42. * Parses portable object (PO) format.
  43. *
  44. * From http://www.gnu.org/software/gettext/manual/gettext.html#PO-Files
  45. * we should be able to parse files having:
  46. *
  47. * white-space
  48. * # translator-comments
  49. * #. extracted-comments
  50. * #: reference...
  51. * #, flag...
  52. * #| msgid previous-untranslated-string
  53. * msgid untranslated-string
  54. * msgstr translated-string
  55. *
  56. * extra or different lines are:
  57. *
  58. * #| msgctxt previous-context
  59. * #| msgid previous-untranslated-string
  60. * msgctxt context
  61. *
  62. * #| msgid previous-untranslated-string-singular
  63. * #| msgid_plural previous-untranslated-string-plural
  64. * msgid untranslated-string-singular
  65. * msgid_plural untranslated-string-plural
  66. * msgstr[0] translated-string-case-0
  67. * ...
  68. * msgstr[N] translated-string-case-n
  69. *
  70. * The definition states:
  71. * - white-space and comments are optional.
  72. * - msgid "" that an empty singleline defines a header.
  73. *
  74. * This parser sacrifices some features of the reference implementation the
  75. * differences to that implementation are as follows.
  76. * - No support for comments spanning multiple lines.
  77. * - Translator and extracted comments are treated as being the same type.
  78. * - Message IDs are allowed to have other encodings as just US-ASCII.
  79. *
  80. * Items with an empty id are ignored.
  81. *
  82. * @param resource $resource
  83. *
  84. * @return array
  85. */
  86. private function parse($resource)
  87. {
  88. $stream = fopen($resource, 'r');
  89. $defaults = array(
  90. 'ids' => array(),
  91. 'translated' => null,
  92. );
  93. $messages = array();
  94. $item = $defaults;
  95. while ($line = fgets($stream)) {
  96. $line = trim($line);
  97. if ($line === '') {
  98. // Whitespace indicated current item is done
  99. $this->addMessage($messages, $item);
  100. $item = $defaults;
  101. } elseif (substr($line, 0, 7) === 'msgid "') {
  102. // We start a new msg so save previous
  103. // TODO: this fails when comments or contexts are added
  104. $this->addMessage($messages, $item);
  105. $item = $defaults;
  106. $item['ids']['singular'] = substr($line, 7, -1);
  107. } elseif (substr($line, 0, 8) === 'msgstr "') {
  108. $item['translated'] = substr($line, 8, -1);
  109. } elseif ($line[0] === '"') {
  110. $continues = isset($item['translated']) ? 'translated' : 'ids';
  111. if (is_array($item[$continues])) {
  112. end($item[$continues]);
  113. $item[$continues][key($item[$continues])] .= substr($line, 1, -1);
  114. } else {
  115. $item[$continues] .= substr($line, 1, -1);
  116. }
  117. } elseif (substr($line, 0, 14) === 'msgid_plural "') {
  118. $item['ids']['plural'] = substr($line, 14, -1);
  119. } elseif (substr($line, 0, 7) === 'msgstr[') {
  120. $size = strpos($line, ']');
  121. $item['translated'][(integer) substr($line, 7, 1)] = substr($line, $size + 3, -1);
  122. }
  123. }
  124. // save last item
  125. $this->addMessage($messages, $item);
  126. fclose($stream);
  127. return $messages;
  128. }
  129. /**
  130. * Save a translation item to the messeages.
  131. *
  132. * A .po file could contain by error missing plural indexes. We need to
  133. * fix these before saving them.
  134. *
  135. * @param array $messages
  136. * @param array $item
  137. */
  138. private function addMessage(array &$messages, array $item)
  139. {
  140. if (is_array($item['translated'])) {
  141. $messages[$item['ids']['singular']] = stripslashes($item['translated'][0]);
  142. if (isset($item['ids']['plural'])) {
  143. $plurals = $item['translated'];
  144. // PO are by definition indexed so sort by index.
  145. ksort($plurals);
  146. // Make sure every index is filled.
  147. end($plurals);
  148. $count = key($plurals);
  149. // Fill missing spots with '-'.
  150. $empties = array_fill(0, $count+1, '-');
  151. $plurals += $empties;
  152. ksort($plurals);
  153. $messages[$item['ids']['plural']] = stripcslashes(implode('|', $plurals));
  154. }
  155. } elseif (!empty($item['ids']['singular'])) {
  156. $messages[$item['ids']['singular']] = stripslashes($item['translated']);
  157. }
  158. }
  159. }