276 lines
9.3KB

  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 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) || ($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 integer $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. } else {
  183. return null;
  184. }
  185. }
  186. /**
  187. * Write bytes.
  188. * @param resource $fileHandle to write to
  189. * @param string $bytes to be written
  190. * @return integer how many bytes are written
  191. */
  192. protected function writeBytes($fileHandle, $bytes)
  193. {
  194. return fwrite($fileHandle, $bytes);
  195. }
  196. /**
  197. * Reads a 4-byte integer.
  198. * @param resource $fileHandle to read from
  199. * @return integer the result
  200. */
  201. protected function readInteger($fileHandle)
  202. {
  203. $array = unpack($this->useBigEndian ? 'N' : 'V', $this->readBytes($fileHandle, 4));
  204. return current($array);
  205. }
  206. /**
  207. * Writes a 4-byte integer.
  208. * @param resource $fileHandle to write to
  209. * @param integer $integer to be written
  210. * @return integer how many bytes are written
  211. */
  212. protected function writeInteger($fileHandle, $integer)
  213. {
  214. return $this->writeBytes($fileHandle, pack($this->useBigEndian ? 'N' : 'V', (int) $integer));
  215. }
  216. /**
  217. * Reads a string.
  218. * @param resource $fileHandle file handle
  219. * @param integer $length of the string
  220. * @param integer $offset of the string in the file. If null, it reads from the current position.
  221. * @return string the result
  222. */
  223. protected function readString($fileHandle, $length, $offset = null)
  224. {
  225. if ($offset !== null) {
  226. fseek($fileHandle, $offset);
  227. }
  228. return $this->readBytes($fileHandle, $length);
  229. }
  230. /**
  231. * Writes a string.
  232. * @param resource $fileHandle to write to
  233. * @param string $string to be written
  234. * @return integer how many bytes are written
  235. */
  236. protected function writeString($fileHandle, $string)
  237. {
  238. return $this->writeBytes($fileHandle, $string. "\0");
  239. }
  240. }