2
0

Gettext.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Translate
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id: Date.php 2498 2006-12-23 22:13:38Z thomas $
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Locale */
  22. require_once 'Zend/Locale.php';
  23. /** Zend_Translate_Adapter */
  24. require_once 'Zend/Translate/Adapter.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Translate
  28. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Translate_Adapter_Gettext extends Zend_Translate_Adapter {
  32. // Internal variables
  33. private $_bigEndian = false;
  34. private $_file = false;
  35. private $_adapterInfo = array();
  36. private $_data = array();
  37. /**
  38. * Generates the adapter
  39. *
  40. * @param string $data Translation data
  41. * @param string|Zend_Locale $locale OPTIONAL Locale/Language to set, identical with locale identifier,
  42. * see Zend_Locale for more information
  43. * @param array $options OPTIONAL Options to set
  44. */
  45. public function __construct($data, $locale = null, array $options = array())
  46. {
  47. parent::__construct($data, $locale, $options);
  48. }
  49. /**
  50. * Read values from the MO file
  51. *
  52. * @param string $bytes
  53. */
  54. private function _readMOData($bytes)
  55. {
  56. if ($this->_bigEndian === false) {
  57. return unpack('V' . $bytes, fread($this->_file, 4 * $bytes));
  58. } else {
  59. return unpack('N' . $bytes, fread($this->_file, 4 * $bytes));
  60. }
  61. }
  62. /**
  63. * Load translation data (MO file reader)
  64. *
  65. * @param string $filename MO file to add, full path must be given for access
  66. * @param string $locale New Locale/Language to set, identical with locale identifier,
  67. * see Zend_Locale for more information
  68. * @param array $option OPTIONAL Options to use
  69. * @throws Zend_Translation_Exception
  70. * @return array
  71. */
  72. protected function _loadTranslationData($filename, $locale, array $options = array())
  73. {
  74. $this->_data = array();
  75. $this->_bigEndian = false;
  76. $this->_file = @fopen($filename, 'rb');
  77. if (!$this->_file) {
  78. require_once 'Zend/Translate/Exception.php';
  79. throw new Zend_Translate_Exception('Error opening translation file \'' . $filename . '\'.');
  80. }
  81. if (@filesize($filename) < 10) {
  82. require_once 'Zend/Translate/Exception.php';
  83. throw new Zend_Translate_Exception('\'' . $filename . '\' is not a gettext file');
  84. }
  85. // get Endian
  86. $input = $this->_readMOData(1);
  87. if (strtolower(substr(dechex($input[1]), -8)) == "950412de") {
  88. $this->_bigEndian = false;
  89. } else if (strtolower(substr(dechex($input[1]), -8)) == "de120495") {
  90. $this->_bigEndian = true;
  91. } else {
  92. require_once 'Zend/Translate/Exception.php';
  93. throw new Zend_Translate_Exception('\'' . $filename . '\' is not a gettext file');
  94. }
  95. // read revision - not supported for now
  96. $input = $this->_readMOData(1);
  97. // number of bytes
  98. $input = $this->_readMOData(1);
  99. $total = $input[1];
  100. // number of original strings
  101. $input = $this->_readMOData(1);
  102. $OOffset = $input[1];
  103. // number of translation strings
  104. $input = $this->_readMOData(1);
  105. $TOffset = $input[1];
  106. // fill the original table
  107. fseek($this->_file, $OOffset);
  108. $origtemp = $this->_readMOData(2 * $total);
  109. fseek($this->_file, $TOffset);
  110. $transtemp = $this->_readMOData(2 * $total);
  111. for($count = 0; $count < $total; ++$count) {
  112. if ($origtemp[$count * 2 + 1] != 0) {
  113. fseek($this->_file, $origtemp[$count * 2 + 2]);
  114. $original = @fread($this->_file, $origtemp[$count * 2 + 1]);
  115. } else {
  116. $original = '';
  117. }
  118. if ($transtemp[$count * 2 + 1] != 0) {
  119. fseek($this->_file, $transtemp[$count * 2 + 2]);
  120. $this->_data[$locale][$original] = fread($this->_file, $transtemp[$count * 2 + 1]);
  121. }
  122. }
  123. $this->_data[$locale][''] = trim($this->_data[$locale]['']);
  124. if (empty($this->_data[$locale][''])) {
  125. $this->_adapterInfo[$filename] = 'No adapter information available';
  126. } else {
  127. $this->_adapterInfo[$filename] = $this->_data[$locale][''];
  128. }
  129. unset($this->_data[$locale]['']);
  130. return $this->_data;
  131. }
  132. /**
  133. * Returns the adapter informations
  134. *
  135. * @return array Each loaded adapter information as array value
  136. */
  137. public function getAdapterInfo()
  138. {
  139. return $this->_adapterInfo;
  140. }
  141. /**
  142. * Returns the adapter name
  143. *
  144. * @return string
  145. */
  146. public function toString()
  147. {
  148. return "Gettext";
  149. }
  150. }