GettextMoFile.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\i18n;
  8. use yii\base\Exception;
  9. /**
  10. * GettextMoFile represents an MO Gettext message file.
  11. *
  12. * This class is written by adapting Michael's Gettext_MO class in PEAR.
  13. * Please refer to the following license terms.
  14. *
  15. * Copyright (c) 2004-2005, Michael Wallner <mike@iworks.at>.
  16. * All rights reserved.
  17. *
  18. * Redistribution and use in source and binary forms, with or without
  19. * modification, are permitted provided that the following conditions are met:
  20. *
  21. * * Redistributions of source code must retain the above copyright notice,
  22. * this list of conditions and the following disclaimer.
  23. * * Redistributions in binary form must reproduce the above copyright
  24. * notice, this list of conditions and the following disclaimer in the
  25. * documentation and/or other materials provided with the distribution.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  28. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  31. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  33. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  35. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * @author Qiang Xue <qiang.xue@gmail.com>
  39. * @since 2.0
  40. */
  41. class GettextMoFile extends GettextFile
  42. {
  43. /**
  44. * @var bool whether to use big-endian when reading and writing an integer.
  45. */
  46. public $useBigEndian = false;
  47. /**
  48. * Loads messages from an MO file.
  49. * @param string $filePath file path
  50. * @param string $context message context
  51. * @return array message translations. Array keys are source messages and array values are translated messages:
  52. * source message => translated message.
  53. * @throws Exception if unable to read the MO file
  54. */
  55. public function load($filePath, $context)
  56. {
  57. if (false === ($fileHandle = @fopen($filePath, 'rb'))) {
  58. throw new Exception('Unable to read file "' . $filePath . '".');
  59. }
  60. if (false === @flock($fileHandle, LOCK_SH)) {
  61. throw new Exception('Unable to lock file "' . $filePath . '" for reading.');
  62. }
  63. // magic
  64. $array = unpack('c', $this->readBytes($fileHandle, 4));
  65. $magic = current($array);
  66. if ($magic == -34) {
  67. $this->useBigEndian = false;
  68. } elseif ($magic == -107) {
  69. $this->useBigEndian = true;
  70. } else {
  71. throw new Exception('Invalid MO file: ' . $filePath . ' (magic: ' . $magic . ').');
  72. }
  73. // revision
  74. $revision = $this->readInteger($fileHandle);
  75. if ($revision !== 0) {
  76. throw new Exception('Invalid MO file revision: ' . $revision . '.');
  77. }
  78. $count = $this->readInteger($fileHandle);
  79. $sourceOffset = $this->readInteger($fileHandle);
  80. $targetOffset = $this->readInteger($fileHandle);
  81. $sourceLengths = [];
  82. $sourceOffsets = [];
  83. fseek($fileHandle, $sourceOffset);
  84. for ($i = 0; $i < $count; ++$i) {
  85. $sourceLengths[] = $this->readInteger($fileHandle);
  86. $sourceOffsets[] = $this->readInteger($fileHandle);
  87. }
  88. $targetLengths = [];
  89. $targetOffsets = [];
  90. fseek($fileHandle, $targetOffset);
  91. for ($i = 0; $i < $count; ++$i) {
  92. $targetLengths[] = $this->readInteger($fileHandle);
  93. $targetOffsets[] = $this->readInteger($fileHandle);
  94. }
  95. $messages = [];
  96. for ($i = 0; $i < $count; ++$i) {
  97. $id = $this->readString($fileHandle, $sourceLengths[$i], $sourceOffsets[$i]);
  98. $separatorPosition = strpos($id, chr(4));
  99. if ((!$context && $separatorPosition === false) || ($context && $separatorPosition !== false && strncmp($id, $context, $separatorPosition) === 0)) {
  100. if ($separatorPosition !== false) {
  101. $id = substr($id, $separatorPosition + 1);
  102. }
  103. $message = $this->readString($fileHandle, $targetLengths[$i], $targetOffsets[$i]);
  104. $messages[$id] = $message;
  105. }
  106. }
  107. @flock($fileHandle, LOCK_UN);
  108. @fclose($fileHandle);
  109. return $messages;
  110. }
  111. /**
  112. * Saves messages to an MO file.
  113. * @param string $filePath file path
  114. * @param array $messages message translations. Array keys are source messages and array values are
  115. * translated messages: source message => translated message. Note if the message has a context,
  116. * the message ID must be prefixed with the context with chr(4) as the separator.
  117. * @throws Exception if unable to save the MO file
  118. */
  119. public function save($filePath, $messages)
  120. {
  121. if (false === ($fileHandle = @fopen($filePath, 'wb'))) {
  122. throw new Exception('Unable to write file "' . $filePath . '".');
  123. }
  124. if (false === @flock($fileHandle, LOCK_EX)) {
  125. throw new Exception('Unable to lock file "' . $filePath . '" for reading.');
  126. }
  127. // magic
  128. if ($this->useBigEndian) {
  129. $this->writeBytes($fileHandle, pack('c*', 0x95, 0x04, 0x12, 0xde)); // -107
  130. } else {
  131. $this->writeBytes($fileHandle, pack('c*', 0xde, 0x12, 0x04, 0x95)); // -34
  132. }
  133. // revision
  134. $this->writeInteger($fileHandle, 0);
  135. // message count
  136. $messageCount = count($messages);
  137. $this->writeInteger($fileHandle, $messageCount);
  138. // offset of source message table
  139. $offset = 28;
  140. $this->writeInteger($fileHandle, $offset);
  141. $offset += $messageCount * 8;
  142. $this->writeInteger($fileHandle, $offset);
  143. // hashtable size, omitted
  144. $this->writeInteger($fileHandle, 0);
  145. $offset += $messageCount * 8;
  146. $this->writeInteger($fileHandle, $offset);
  147. // length and offsets for source messages
  148. foreach (array_keys($messages) as $id) {
  149. $length = strlen($id);
  150. $this->writeInteger($fileHandle, $length);
  151. $this->writeInteger($fileHandle, $offset);
  152. $offset += $length + 1;
  153. }
  154. // length and offsets for target messages
  155. foreach ($messages as $message) {
  156. $length = strlen($message);
  157. $this->writeInteger($fileHandle, $length);
  158. $this->writeInteger($fileHandle, $offset);
  159. $offset += $length + 1;
  160. }
  161. // source messages
  162. foreach (array_keys($messages) as $id) {
  163. $this->writeString($fileHandle, $id);
  164. }
  165. // target messages
  166. foreach ($messages as $message) {
  167. $this->writeString($fileHandle, $message);
  168. }
  169. @flock($fileHandle, LOCK_UN);
  170. @fclose($fileHandle);
  171. }
  172. /**
  173. * Reads one or several bytes.
  174. * @param resource $fileHandle to read from
  175. * @param int $byteCount to be read
  176. * @return string bytes
  177. */
  178. protected function readBytes($fileHandle, $byteCount = 1)
  179. {
  180. if ($byteCount > 0) {
  181. return fread($fileHandle, $byteCount);
  182. }
  183. return null;
  184. }
  185. /**
  186. * Write bytes.
  187. * @param resource $fileHandle to write to
  188. * @param string $bytes to be written
  189. * @return int how many bytes are written
  190. */
  191. protected function writeBytes($fileHandle, $bytes)
  192. {
  193. return fwrite($fileHandle, $bytes);
  194. }
  195. /**
  196. * Reads a 4-byte integer.
  197. * @param resource $fileHandle to read from
  198. * @return int the result
  199. */
  200. protected function readInteger($fileHandle)
  201. {
  202. $array = unpack($this->useBigEndian ? 'N' : 'V', $this->readBytes($fileHandle, 4));
  203. return current($array);
  204. }
  205. /**
  206. * Writes a 4-byte integer.
  207. * @param resource $fileHandle to write to
  208. * @param int $integer to be written
  209. * @return int how many bytes are written
  210. */
  211. protected function writeInteger($fileHandle, $integer)
  212. {
  213. return $this->writeBytes($fileHandle, pack($this->useBigEndian ? 'N' : 'V', (int) $integer));
  214. }
  215. /**
  216. * Reads a string.
  217. * @param resource $fileHandle file handle
  218. * @param int $length of the string
  219. * @param int $offset of the string in the file. If null, it reads from the current position.
  220. * @return string the result
  221. */
  222. protected function readString($fileHandle, $length, $offset = null)
  223. {
  224. if ($offset !== null) {
  225. fseek($fileHandle, $offset);
  226. }
  227. return $this->readBytes($fileHandle, $length);
  228. }
  229. /**
  230. * Writes a string.
  231. * @param resource $fileHandle to write to
  232. * @param string $string to be written
  233. * @return int how many bytes are written
  234. */
  235. protected function writeString($fileHandle, $string)
  236. {
  237. return $this->writeBytes($fileHandle, $string . "\0");
  238. }
  239. }