PageRenderTime 86ms CodeModel.GetById 57ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/i18n/gettext/CGettextMoFile.php

http://github.com/yiisoft/yii
PHP | 276 lines | 136 code | 31 blank | 109 comment | 18 complexity | 967d274a320f1fb8e22838739822308f MD5 | raw file
Possible License(s): BSD-3-Clause
  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. * @throws CException
  63. */
  64. public function load($file,$context)
  65. {
  66. if(!($fr=@fopen($file,'rb')))
  67. throw new CException(Yii::t('yii','Unable to read file "{file}".',
  68. array('{file}'=>$file)));
  69. if(!@flock($fr,LOCK_SH))
  70. throw new CException(Yii::t('yii','Unable to lock file "{file}" for reading.',
  71. array('{file}'=>$file)));
  72. $array=unpack('c',$this->readByte($fr,4));
  73. $magic=current($array);
  74. if($magic==-34)
  75. $this->useBigEndian=false;
  76. elseif($magic==-107)
  77. $this->useBigEndian=true;
  78. else
  79. throw new CException(Yii::t('yii','Invalid MO file: {file} (magic: {magic}).',
  80. array('{file}'=>$file,'{magic}'=>$magic)));
  81. if(($revision=$this->readInteger($fr))!=0)
  82. throw new CException(Yii::t('yii','Invalid MO file revision: {revision}.',
  83. array('{revision}'=>$revision)));
  84. $count=$this->readInteger($fr);
  85. $sourceOffset=$this->readInteger($fr);
  86. $targetOffset=$this->readInteger($fr);
  87. $sourceLengths=array();
  88. $sourceOffsets=array();
  89. fseek($fr,$sourceOffset);
  90. for($i=0;$i<$count;++$i)
  91. {
  92. $sourceLengths[]=$this->readInteger($fr);
  93. $sourceOffsets[]=$this->readInteger($fr);
  94. }
  95. $targetLengths=array();
  96. $targetOffsets=array();
  97. fseek($fr,$targetOffset);
  98. for($i=0;$i<$count;++$i)
  99. {
  100. $targetLengths[]=$this->readInteger($fr);
  101. $targetOffsets[]=$this->readInteger($fr);
  102. }
  103. $messages=array();
  104. for($i=0;$i<$count;++$i)
  105. {
  106. $id=$this->readString($fr,$sourceLengths[$i],$sourceOffsets[$i]);
  107. $pos = strpos($id,chr(4));
  108. if(($context && $pos!==false && substr($id,0,$pos)===$context) || (!$context && $pos===false))
  109. {
  110. if($pos !== false)
  111. $id=substr($id,$pos+1);
  112. $message=$this->readString($fr,$targetLengths[$i],$targetOffsets[$i]);
  113. $messages[$id]=$message;
  114. }
  115. }
  116. @flock($fr,LOCK_UN);
  117. @fclose($fr);
  118. return $messages;
  119. }
  120. /**
  121. * Saves messages to an MO file.
  122. * @param string $file file path
  123. * @param array $messages message translations (message id => translated message).
  124. * Note if the message has a context, the message id must be prefixed with
  125. * the context with chr(4) as the separator.
  126. * @throws CException
  127. */
  128. public function save($file,$messages)
  129. {
  130. if(!($fw=@fopen($file,'wb')))
  131. throw new CException(Yii::t('yii','Unable to write file "{file}".',
  132. array('{file}'=>$file)));
  133. if(!@flock($fw,LOCK_EX))
  134. throw new CException(Yii::t('yii','Unable to lock file "{file}" for writing.',
  135. array('{file}'=>$file)));
  136. // magic
  137. if($this->useBigEndian)
  138. $this->writeByte($fw,pack('c*', 0x95, 0x04, 0x12, 0xde));
  139. else
  140. $this->writeByte($fw,pack('c*', 0xde, 0x12, 0x04, 0x95));
  141. // revision
  142. $this->writeInteger($fw,0);
  143. // message count
  144. $n=count($messages);
  145. $this->writeInteger($fw,$n);
  146. // offset of source message table
  147. $offset=28;
  148. $this->writeInteger($fw,$offset);
  149. $offset+=($n*8);
  150. $this->writeInteger($fw,$offset);
  151. // hashtable size, omitted
  152. $this->writeInteger($fw,0);
  153. $offset+=($n*8);
  154. $this->writeInteger($fw,$offset);
  155. // length and offsets for source messagess
  156. foreach(array_keys($messages) as $id)
  157. {
  158. $len=strlen($id);
  159. $this->writeInteger($fw,$len);
  160. $this->writeInteger($fw,$offset);
  161. $offset+=$len+1;
  162. }
  163. // length and offsets for target messagess
  164. foreach($messages as $message)
  165. {
  166. $len=strlen($message);
  167. $this->writeInteger($fw,$len);
  168. $this->writeInteger($fw,$offset);
  169. $offset+=$len+1;
  170. }
  171. // source messages
  172. foreach(array_keys($messages) as $id)
  173. $this->writeString($fw,$id);
  174. // target messages
  175. foreach($messages as $message)
  176. $this->writeString($fw,$message);
  177. @flock($fw,LOCK_UN);
  178. @fclose($fw);
  179. }
  180. /**
  181. * Reads one or several bytes.
  182. * @param resource $fr file handle
  183. * @param integer $n number of bytes to read
  184. * @return string bytes
  185. */
  186. protected function readByte($fr,$n=1)
  187. {
  188. if($n>0)
  189. return fread($fr,$n);
  190. }
  191. /**
  192. * Writes bytes.
  193. * @param resource $fw file handle
  194. * @param string $data the data
  195. * @return integer how many bytes are written
  196. */
  197. protected function writeByte($fw,$data)
  198. {
  199. return fwrite($fw,$data);
  200. }
  201. /**
  202. * Reads a 4-byte integer.
  203. * @param resource $fr file handle
  204. * @return integer the result
  205. * @see useBigEndian
  206. */
  207. protected function readInteger($fr)
  208. {
  209. $array=unpack($this->useBigEndian ? 'N' : 'V', $this->readByte($fr,4));
  210. return current($array);
  211. }
  212. /**
  213. * Writes a 4-byte integer.
  214. * @param resource $fw file handle
  215. * @param integer $data the data
  216. * @return integer how many bytes are written
  217. */
  218. protected function writeInteger($fw,$data)
  219. {
  220. return $this->writeByte($fw,pack($this->useBigEndian ? 'N' : 'V', (int)$data));
  221. }
  222. /**
  223. * Reads a string.
  224. * @param resource $fr file handle
  225. * @param integer $length string length
  226. * @param integer $offset offset of the string in the file. If null, it reads from the current position.
  227. * @return string the result
  228. */
  229. protected function readString($fr,$length,$offset=null)
  230. {
  231. if($offset!==null)
  232. fseek($fr,$offset);
  233. return $this->readByte($fr,$length);
  234. }
  235. /**
  236. * Writes a string.
  237. * @param resource $fw file handle
  238. * @param string $data the string
  239. * @return integer how many bytes are written
  240. */
  241. protected function writeString($fw,$data)
  242. {
  243. return $this->writeByte($fw,$data."\0");
  244. }
  245. }