CGettextMoFile.php 7.5 KB

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