/src/frapi/library/Zend/Translate/Adapter/Gettext.php

https://github.com/Martin1982/IBMessagingWorkshopServer · PHP · 167 lines · 86 code · 17 blank · 64 comment · 21 complexity · 1e609e2ecde9dcb17edbc01ae11711eb MD5 · raw file

  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id: Gettext.php 20096 2010-01-06 02:05:09Z bkarwin $
  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-2010 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][''] =
  123. isset($this->_data[$locale]['']) ? trim($this->_data[$locale]['']) : false;
  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. }