MoFileLoader.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. */
  17. class MoFileLoader extends ArrayLoader implements LoaderInterface
  18. {
  19. /**
  20. * Magic used for validating the format of a MO file as well as
  21. * detecting if the machine used to create that file was little endian.
  22. *
  23. * @var float
  24. */
  25. const MO_LITTLE_ENDIAN_MAGIC = 0x950412de;
  26. /**
  27. * Magic used for validating the format of a MO file as well as
  28. * detecting if the machine used to create that file was big endian.
  29. *
  30. * @var float
  31. */
  32. const MO_BIG_ENDIAN_MAGIC = 0xde120495;
  33. /**
  34. * The size of the header of a MO file in bytes.
  35. *
  36. * @var integer Number of bytes.
  37. */
  38. const MO_HEADER_SIZE = 28;
  39. public function load($resource, $locale, $domain = 'messages')
  40. {
  41. if (!stream_is_local($resource)) {
  42. throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
  43. }
  44. if (!file_exists($resource)) {
  45. throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
  46. }
  47. $messages = $this->parse($resource);
  48. // empty file
  49. if (null === $messages) {
  50. $messages = array();
  51. }
  52. // not an array
  53. if (!is_array($messages)) {
  54. throw new InvalidResourceException(sprintf('The file "%s" must contain a valid mo file.', $resource));
  55. }
  56. $catalogue = parent::load($messages, $locale, $domain);
  57. $catalogue->addResource(new FileResource($resource));
  58. return $catalogue;
  59. }
  60. /**
  61. * Parses machine object (MO) format, independent of the machine's endian it
  62. * was created on. Both 32bit and 64bit systems are supported.
  63. *
  64. * @param resource $resource
  65. *
  66. * @return array
  67. * @throws InvalidResourceException If stream content has an invalid format.
  68. */
  69. private function parse($resource)
  70. {
  71. $stream = fopen($resource, 'r');
  72. $stat = fstat($stream);
  73. if ($stat['size'] < self::MO_HEADER_SIZE) {
  74. throw new InvalidResourceException("MO stream content has an invalid format.");
  75. }
  76. $magic = unpack('V1', fread($stream, 4));
  77. $magic = hexdec(substr(dechex(current($magic)), -8));
  78. if ($magic == self::MO_LITTLE_ENDIAN_MAGIC) {
  79. $isBigEndian = false;
  80. } elseif ($magic == self::MO_BIG_ENDIAN_MAGIC) {
  81. $isBigEndian = true;
  82. } else {
  83. throw new InvalidResourceException("MO stream content has an invalid format.");
  84. }
  85. // formatRevision
  86. $this->readLong($stream, $isBigEndian);
  87. $count = $this->readLong($stream, $isBigEndian);
  88. $offsetId = $this->readLong($stream, $isBigEndian);
  89. $offsetTranslated = $this->readLong($stream, $isBigEndian);
  90. // sizeHashes
  91. $this->readLong($stream, $isBigEndian);
  92. // offsetHashes
  93. $this->readLong($stream, $isBigEndian);
  94. $messages = array();
  95. for ($i = 0; $i < $count; $i++) {
  96. $singularId = $pluralId = null;
  97. $translated = null;
  98. fseek($stream, $offsetId + $i * 8);
  99. $length = $this->readLong($stream, $isBigEndian);
  100. $offset = $this->readLong($stream, $isBigEndian);
  101. if ($length < 1) {
  102. continue;
  103. }
  104. fseek($stream, $offset);
  105. $singularId = fread($stream, $length);
  106. if (strpos($singularId, "\000") !== false) {
  107. list($singularId, $pluralId) = explode("\000", $singularId);
  108. }
  109. fseek($stream, $offsetTranslated + $i * 8);
  110. $length = $this->readLong($stream, $isBigEndian);
  111. $offset = $this->readLong($stream, $isBigEndian);
  112. fseek($stream, $offset);
  113. $translated = fread($stream, $length);
  114. if (strpos($translated, "\000") !== false) {
  115. $translated = explode("\000", $translated);
  116. }
  117. $ids = array('singular' => $singularId, 'plural' => $pluralId);
  118. $item = compact('ids', 'translated');
  119. if (is_array($item['translated'])) {
  120. $messages[$item['ids']['singular']] = stripcslashes($item['translated'][0]);
  121. if (isset($item['ids']['plural'])) {
  122. $plurals = array();
  123. foreach ($item['translated'] as $plural => $translated) {
  124. $plurals[] = sprintf('{%d} %s', $plural, $translated);
  125. }
  126. $messages[$item['ids']['plural']] = stripcslashes(implode('|', $plurals));
  127. }
  128. } elseif (!empty($item['ids']['singular'])) {
  129. $messages[$item['ids']['singular']] = stripcslashes($item['translated']);
  130. }
  131. }
  132. fclose($stream);
  133. return array_filter($messages);
  134. }
  135. /**
  136. * Reads an unsigned long from stream respecting endianess.
  137. *
  138. * @param resource $stream
  139. * @param boolean $isBigEndian
  140. * @return integer
  141. */
  142. private function readLong($stream, $isBigEndian)
  143. {
  144. $result = unpack($isBigEndian ? 'N1' : 'V1', fread($stream, 4));
  145. $result = current($result);
  146. return (integer) substr($result, -8);
  147. }
  148. }