Gettext.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id$
  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-2009 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. * Read values from the MO file
  39. *
  40. * @param string $bytes
  41. */
  42. private function _readMOData($bytes)
  43. {
  44. if ($this->_bigEndian === false) {
  45. return unpack('V' . $bytes, fread($this->_file, 4 * $bytes));
  46. } else {
  47. return unpack('N' . $bytes, fread($this->_file, 4 * $bytes));
  48. }
  49. }
  50. /**
  51. * Load translation data (MO file reader)
  52. *
  53. * @param string $filename MO file to add, full path must be given for access
  54. * @param string $locale New Locale/Language to set, identical with locale identifier,
  55. * see Zend_Locale for more information
  56. * @param array $option OPTIONAL Options to use
  57. * @throws Zend_Translation_Exception
  58. * @return array
  59. */
  60. protected function _loadTranslationData($filename, $locale, array $options = array())
  61. {
  62. $this->_data = array();
  63. $this->_bigEndian = false;
  64. $this->_file = @fopen($filename, 'rb');
  65. if (!$this->_file) {
  66. require_once 'Zend/Translate/Exception.php';
  67. throw new Zend_Translate_Exception('Error opening translation file \'' . $filename . '\'.');
  68. }
  69. if (@filesize($filename) < 10) {
  70. require_once 'Zend/Translate/Exception.php';
  71. throw new Zend_Translate_Exception('\'' . $filename . '\' is not a gettext file');
  72. }
  73. // get Endian
  74. $input = $this->_readMOData(1);
  75. if (strtolower(substr(dechex($input[1]), -8)) == "950412de") {
  76. $this->_bigEndian = false;
  77. } else if (strtolower(substr(dechex($input[1]), -8)) == "de120495") {
  78. $this->_bigEndian = true;
  79. } else {
  80. require_once 'Zend/Translate/Exception.php';
  81. throw new Zend_Translate_Exception('\'' . $filename . '\' is not a gettext file');
  82. }
  83. // read revision - not supported for now
  84. $input = $this->_readMOData(1);
  85. // number of bytes
  86. $input = $this->_readMOData(1);
  87. $total = $input[1];
  88. // number of original strings
  89. $input = $this->_readMOData(1);
  90. $OOffset = $input[1];
  91. // number of translation strings
  92. $input = $this->_readMOData(1);
  93. $TOffset = $input[1];
  94. // fill the original table
  95. fseek($this->_file, $OOffset);
  96. $origtemp = $this->_readMOData(2 * $total);
  97. fseek($this->_file, $TOffset);
  98. $transtemp = $this->_readMOData(2 * $total);
  99. for($count = 0; $count < $total; ++$count) {
  100. if ($origtemp[$count * 2 + 1] != 0) {
  101. fseek($this->_file, $origtemp[$count * 2 + 2]);
  102. $original = @fread($this->_file, $origtemp[$count * 2 + 1]);
  103. $original = explode(chr(00), $original);
  104. } else {
  105. $original[0] = '';
  106. }
  107. if ($transtemp[$count * 2 + 1] != 0) {
  108. fseek($this->_file, $transtemp[$count * 2 + 2]);
  109. $translate = fread($this->_file, $transtemp[$count * 2 + 1]);
  110. $translate = explode(chr(00), $translate);
  111. if ((count($original) > 1) && (count($translate) > 1)) {
  112. $this->_data[$locale][$original[0]] = $translate;
  113. array_shift($original);
  114. foreach ($original as $orig) {
  115. $this->_data[$locale][$orig] = '';
  116. }
  117. } else {
  118. $this->_data[$locale][$original[0]] = $translate[0];
  119. }
  120. }
  121. }
  122. $this->_data[$locale][''] = trim($this->_data[$locale]['']);
  123. if (empty($this->_data[$locale][''])) {
  124. $this->_adapterInfo[$filename] = 'No adapter information available';
  125. } else {
  126. $this->_adapterInfo[$filename] = $this->_data[$locale][''];
  127. }
  128. unset($this->_data[$locale]['']);
  129. return $this->_data;
  130. }
  131. /**
  132. * Returns the adapter informations
  133. *
  134. * @return array Each loaded adapter information as array value
  135. */
  136. public function getAdapterInfo()
  137. {
  138. return $this->_adapterInfo;
  139. }
  140. /**
  141. * Returns the adapter name
  142. *
  143. * @return string
  144. */
  145. public function toString()
  146. {
  147. return "Gettext";
  148. }
  149. }