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 use Symfony\Component\Translation\Exception\InvalidResourceException;
17 * @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/)
19 class MoFileLoader extends FileLoader
22 * Magic used for validating the format of a MO file as well as
23 * detecting if the machine used to create that file was little endian.
25 const MO_LITTLE_ENDIAN_MAGIC = 0x950412de;
28 * Magic used for validating the format of a MO file as well as
29 * detecting if the machine used to create that file was big endian.
31 const MO_BIG_ENDIAN_MAGIC = 0xde120495;
34 * The size of the header of a MO file in bytes.
36 const MO_HEADER_SIZE = 28;
39 * Parses machine object (MO) format, independent of the machine's endian it
40 * was created on. Both 32bit and 64bit systems are supported.
44 protected function loadResource($resource)
46 $stream = fopen($resource, 'r');
48 $stat = fstat($stream);
50 if ($stat['size'] < self::MO_HEADER_SIZE) {
51 throw new InvalidResourceException('MO stream content has an invalid format.');
53 $magic = unpack('V1', fread($stream, 4));
54 $magic = hexdec(substr(dechex(current($magic)), -8));
56 if (self::MO_LITTLE_ENDIAN_MAGIC == $magic) {
58 } elseif (self::MO_BIG_ENDIAN_MAGIC == $magic) {
61 throw new InvalidResourceException('MO stream content has an invalid format.');
65 $this->readLong($stream, $isBigEndian);
66 $count = $this->readLong($stream, $isBigEndian);
67 $offsetId = $this->readLong($stream, $isBigEndian);
68 $offsetTranslated = $this->readLong($stream, $isBigEndian);
70 $this->readLong($stream, $isBigEndian);
72 $this->readLong($stream, $isBigEndian);
76 for ($i = 0; $i < $count; ++$i) {
80 fseek($stream, $offsetId + $i * 8);
82 $length = $this->readLong($stream, $isBigEndian);
83 $offset = $this->readLong($stream, $isBigEndian);
89 fseek($stream, $offset);
90 $singularId = fread($stream, $length);
92 if (false !== strpos($singularId, "\000")) {
93 list($singularId, $pluralId) = explode("\000", $singularId);
96 fseek($stream, $offsetTranslated + $i * 8);
97 $length = $this->readLong($stream, $isBigEndian);
98 $offset = $this->readLong($stream, $isBigEndian);
104 fseek($stream, $offset);
105 $translated = fread($stream, $length);
107 if (false !== strpos($translated, "\000")) {
108 $translated = explode("\000", $translated);
111 $ids = array('singular' => $singularId, 'plural' => $pluralId);
112 $item = compact('ids', 'translated');
114 if (is_array($item['translated'])) {
115 $messages[$item['ids']['singular']] = stripcslashes($item['translated'][0]);
116 if (isset($item['ids']['plural'])) {
118 foreach ($item['translated'] as $plural => $translated) {
119 $plurals[] = sprintf('{%d} %s', $plural, $translated);
121 $messages[$item['ids']['plural']] = stripcslashes(implode('|', $plurals));
123 } elseif (!empty($item['ids']['singular'])) {
124 $messages[$item['ids']['singular']] = stripcslashes($item['translated']);
130 return array_filter($messages);
134 * Reads an unsigned long from stream respecting endianness.
136 * @param resource $stream
137 * @param bool $isBigEndian
141 private function readLong($stream, $isBigEndian)
143 $result = unpack($isBigEndian ? 'N1' : 'V1', fread($stream, 4));
144 $result = current($result);
146 return (int) substr($result, -8);