No Description

MoFileLoader.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 int 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. *
  68. * @throws InvalidResourceException If stream content has an invalid format.
  69. */
  70. private function parse($resource)
  71. {
  72. $stream = fopen($resource, 'r');
  73. $stat = fstat($stream);
  74. if ($stat['size'] < self::MO_HEADER_SIZE) {
  75. throw new InvalidResourceException("MO stream content has an invalid format.");
  76. }
  77. $magic = unpack('V1', fread($stream, 4));
  78. $magic = hexdec(substr(dechex(current($magic)), -8));
  79. if ($magic == self::MO_LITTLE_ENDIAN_MAGIC) {
  80. $isBigEndian = false;
  81. } elseif ($magic == self::MO_BIG_ENDIAN_MAGIC) {
  82. $isBigEndian = true;
  83. } else {
  84. throw new InvalidResourceException("MO stream content has an invalid format.");
  85. }
  86. // formatRevision
  87. $this->readLong($stream, $isBigEndian);
  88. $count = $this->readLong($stream, $isBigEndian);
  89. $offsetId = $this->readLong($stream, $isBigEndian);
  90. $offsetTranslated = $this->readLong($stream, $isBigEndian);
  91. // sizeHashes
  92. $this->readLong($stream, $isBigEndian);
  93. // offsetHashes
  94. $this->readLong($stream, $isBigEndian);
  95. $messages = array();
  96. for ($i = 0; $i < $count; $i++) {
  97. $singularId = $pluralId = null;
  98. $translated = null;
  99. fseek($stream, $offsetId + $i * 8);
  100. $length = $this->readLong($stream, $isBigEndian);
  101. $offset = $this->readLong($stream, $isBigEndian);
  102. if ($length < 1) {
  103. continue;
  104. }
  105. fseek($stream, $offset);
  106. $singularId = fread($stream, $length);
  107. if (strpos($singularId, "\000") !== false) {
  108. list($singularId, $pluralId) = explode("\000", $singularId);
  109. }
  110. fseek($stream, $offsetTranslated + $i * 8);
  111. $length = $this->readLong($stream, $isBigEndian);
  112. $offset = $this->readLong($stream, $isBigEndian);
  113. fseek($stream, $offset);
  114. $translated = fread($stream, $length);
  115. if (strpos($translated, "\000") !== false) {
  116. $translated = explode("\000", $translated);
  117. }
  118. $ids = array('singular' => $singularId, 'plural' => $pluralId);
  119. $item = compact('ids', 'translated');
  120. if (is_array($item['translated'])) {
  121. $messages[$item['ids']['singular']] = stripcslashes($item['translated'][0]);
  122. if (isset($item['ids']['plural'])) {
  123. $plurals = array();
  124. foreach ($item['translated'] as $plural => $translated) {
  125. $plurals[] = sprintf('{%d} %s', $plural, $translated);
  126. }
  127. $messages[$item['ids']['plural']] = stripcslashes(implode('|', $plurals));
  128. }
  129. } elseif (!empty($item['ids']['singular'])) {
  130. $messages[$item['ids']['singular']] = stripcslashes($item['translated']);
  131. }
  132. }
  133. fclose($stream);
  134. return array_filter($messages);
  135. }
  136. /**
  137. * Reads an unsigned long from stream respecting endianess.
  138. *
  139. * @param resource $stream
  140. * @param bool $isBigEndian
  141. *
  142. * @return int
  143. */
  144. private function readLong($stream, $isBigEndian)
  145. {
  146. $result = unpack($isBigEndian ? 'N1' : 'V1', fread($stream, 4));
  147. $result = current($result);
  148. return (int) substr($result, -8);
  149. }
  150. }