GettextMoFile.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 <[email protected]>.
  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 <[email protected]>
  39. * @since 2.0
  40. */
  41. class GettextMoFile extends GettextFile
  42. {
  43. /**
  44. * @var boolean 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 && substr($id, 0, $separatorPosition) === $context) ||
  100. (!$context && $separatorPosition === false)) {
  101. if ($separatorPosition !== false) {
  102. $id = substr($id, $separatorPosition+1);
  103. }
  104. $message = $this->readString($fileHandle, $targetLengths[$i], $targetOffsets[$i]);
  105. $messages[$id] = $message;
  106. }
  107. }
  108. @flock($fileHandle, LOCK_UN);
  109. @fclose($fileHandle);
  110. return $messages;
  111. }
  112. /**
  113. * Saves messages to an MO file.
  114. * @param string $filePath file path
  115. * @param array $messages message translations. Array keys are source messages and array values are
  116. * translated messages: source message => translated message. Note if the message has a context,
  117. * the message ID must be prefixed with the context with chr(4) as the separator.
  118. * @throws Exception if unable to save the MO file
  119. */
  120. public function save($filePath, $messages)
  121. {
  122. if (false === ($fileHandle = @fopen($filePath, 'wb'))) {
  123. throw new Exception('Unable to write file "' . $filePath . '".');
  124. }
  125. if (false === @flock($fileHandle, LOCK_EX)) {
  126. throw new Exception('Unable to lock file "' . $filePath . '" for reading.');
  127. }
  128. // magic
  129. if ($this->useBigEndian) {
  130. $this->writeBytes($fileHandle, pack('c*', 0x95, 0x04, 0x12, 0xde)); // -107
  131. } else {
  132. $this->writeBytes($fileHandle, pack('c*', 0xde, 0x12, 0x04, 0x95)); // -34
  133. }
  134. // revision
  135. $this->writeInteger($fileHandle, 0);
  136. // message count
  137. $messageCount = count($messages);
  138. $this->writeInteger($fileHandle, $messageCount);
  139. // offset of source message table
  140. $offset = 28;
  141. $this->writeInteger($fileHandle, $offset);
  142. $offset += $messageCount * 8;
  143. $this->writeInteger($fileHandle, $offset);
  144. // hashtable size, omitted
  145. $this->writeInteger($fileHandle, 0);
  146. $offset += $messageCount * 8;
  147. $this->writeInteger($fileHandle, $offset);
  148. // length and offsets for source messages
  149. foreach (array_keys($messages) as $id) {
  150. $length = strlen($id);
  151. $this->writeInteger($fileHandle, $length);
  152. $this->writeInteger($fileHandle, $offset);
  153. $offset += $length + 1;
  154. }
  155. // length and offsets for target messages
  156. foreach ($messages as $message) {
  157. $length = strlen($message);
  158. $this->writeInteger($fileHandle, $length);
  159. $this->writeInteger($fileHandle, $offset);
  160. $offset += $length + 1;
  161. }
  162. // source messages
  163. foreach (array_keys($messages) as $id) {
  164. $this->writeString($fileHandle, $id);
  165. }
  166. // target messages
  167. foreach ($messages as $message) {
  168. $this->writeString($fileHandle, $message);
  169. }
  170. @flock($fileHandle, LOCK_UN);
  171. @fclose($fileHandle);
  172. }
  173. /**
  174. * Reads one or several bytes.
  175. * @param resource $fileHandle to read from
  176. * @param integer $byteCount to be read
  177. * @return string bytes
  178. */
  179. protected function readBytes($fileHandle, $byteCount = 1)
  180. {
  181. if ($byteCount > 0) {
  182. return fread($fileHandle, $byteCount);
  183. } else {
  184. return null;
  185. }
  186. }
  187. /**
  188. * Write bytes.
  189. * @param resource $fileHandle to write to
  190. * @param string $bytes to be written
  191. * @return integer how many bytes are written
  192. */
  193. protected function writeBytes($fileHandle, $bytes)
  194. {
  195. return fwrite($fileHandle, $bytes);
  196. }
  197. /**
  198. * Reads a 4-byte integer.
  199. * @param resource $fileHandle to read from
  200. * @return integer the result
  201. */
  202. protected function readInteger($fileHandle)
  203. {
  204. $array = unpack($this->useBigEndian ? 'N' : 'V', $this->readBytes($fileHandle, 4));
  205. return current($array);
  206. }
  207. /**
  208. * Writes a 4-byte integer.
  209. * @param resource $fileHandle to write to
  210. * @param integer $integer to be written
  211. * @return integer how many bytes are written
  212. */
  213. protected function writeInteger($fileHandle, $integer)
  214. {
  215. return $this->writeBytes($fileHandle, pack($this->useBigEndian ? 'N' : 'V', (int)$integer));
  216. }
  217. /**
  218. * Reads a string.
  219. * @param resource $fileHandle file handle
  220. * @param integer $length of the string
  221. * @param integer $offset of the string in the file. If null, it reads from the current position.
  222. * @return string the result
  223. */
  224. protected function readString($fileHandle, $length, $offset = null)
  225. {
  226. if ($offset !== null) {
  227. fseek($fileHandle, $offset);
  228. }
  229. return $this->readBytes($fileHandle, $length);
  230. }
  231. /**
  232. * Writes a string.
  233. * @param resource $fileHandle to write to
  234. * @param string $string to be written
  235. * @return integer how many bytes are written
  236. */
  237. protected function writeString($fileHandle, $string)
  238. {
  239. return $this->writeBytes($fileHandle, $string. "\0");
  240. }
  241. }