PageRenderTime 26ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/gauravk90/site
PHP | 178 lines | 94 code | 17 blank | 67 comment | 21 complexity | e2d9fb9795632dc0fcc7746ea6bcbde2 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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id: Gettext.php 16971 2009-07-22 18:05:45Z mikaelkael $
  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. * 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. $original = explode(chr(00), $original);
  116. } else {
  117. $original[0] = '';
  118. }
  119. if ($transtemp[$count * 2 + 1] != 0) {
  120. fseek($this->_file, $transtemp[$count * 2 + 2]);
  121. $translate = fread($this->_file, $transtemp[$count * 2 + 1]);
  122. $translate = explode(chr(00), $translate);
  123. if ((count($original) > 1) && (count($translate) > 1)) {
  124. $this->_data[$locale][$original[0]] = $translate;
  125. array_shift($original);
  126. foreach ($original as $orig) {
  127. $this->_data[$locale][$orig] = '';
  128. }
  129. } else {
  130. $this->_data[$locale][$original[0]] = $translate[0];
  131. }
  132. }
  133. }
  134. $this->_data[$locale][''] = trim($this->_data[$locale]['']);
  135. if (empty($this->_data[$locale][''])) {
  136. $this->_adapterInfo[$filename] = 'No adapter information available';
  137. } else {
  138. $this->_adapterInfo[$filename] = $this->_data[$locale][''];
  139. }
  140. unset($this->_data[$locale]['']);
  141. return $this->_data;
  142. }
  143. /**
  144. * Returns the adapter informations
  145. *
  146. * @return array Each loaded adapter information as array value
  147. */
  148. public function getAdapterInfo()
  149. {
  150. return $this->_adapterInfo;
  151. }
  152. /**
  153. * Returns the adapter name
  154. *
  155. * @return string
  156. */
  157. public function toString()
  158. {
  159. return "Gettext";
  160. }
  161. }