PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Translate/Adapter/Gettext.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 169 lines | 93 code | 17 blank | 59 comment | 21 complexity | 167c3808e3ff869506d304c76695d5ce 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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id: Gettext.php 23961 2011-05-03 11:20:34Z yoshida@zend.co.jp $
  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-2011 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. @fclose($this->_file);
  71. require_once 'Zend/Translate/Exception.php';
  72. throw new Zend_Translate_Exception('\'' . $filename . '\' is not a gettext file');
  73. }
  74. // get Endian
  75. $input = $this->_readMOData(1);
  76. if (strtolower(substr(dechex($input[1]), -8)) == "950412de") {
  77. $this->_bigEndian = false;
  78. } else if (strtolower(substr(dechex($input[1]), -8)) == "de120495") {
  79. $this->_bigEndian = true;
  80. } else {
  81. @fclose($this->_file);
  82. require_once 'Zend/Translate/Exception.php';
  83. throw new Zend_Translate_Exception('\'' . $filename . '\' is not a gettext file');
  84. }
  85. // read revision - not supported for now
  86. $input = $this->_readMOData(1);
  87. // number of bytes
  88. $input = $this->_readMOData(1);
  89. $total = $input[1];
  90. // number of original strings
  91. $input = $this->_readMOData(1);
  92. $OOffset = $input[1];
  93. // number of translation strings
  94. $input = $this->_readMOData(1);
  95. $TOffset = $input[1];
  96. // fill the original table
  97. fseek($this->_file, $OOffset);
  98. $origtemp = $this->_readMOData(2 * $total);
  99. fseek($this->_file, $TOffset);
  100. $transtemp = $this->_readMOData(2 * $total);
  101. for($count = 0; $count < $total; ++$count) {
  102. if ($origtemp[$count * 2 + 1] != 0) {
  103. fseek($this->_file, $origtemp[$count * 2 + 2]);
  104. $original = @fread($this->_file, $origtemp[$count * 2 + 1]);
  105. $original = explode("\0", $original);
  106. } else {
  107. $original[0] = '';
  108. }
  109. if ($transtemp[$count * 2 + 1] != 0) {
  110. fseek($this->_file, $transtemp[$count * 2 + 2]);
  111. $translate = fread($this->_file, $transtemp[$count * 2 + 1]);
  112. $translate = explode("\0", $translate);
  113. if ((count($original) > 1) && (count($translate) > 1)) {
  114. $this->_data[$locale][$original[0]] = $translate;
  115. array_shift($original);
  116. foreach ($original as $orig) {
  117. $this->_data[$locale][$orig] = '';
  118. }
  119. } else {
  120. $this->_data[$locale][$original[0]] = $translate[0];
  121. }
  122. }
  123. }
  124. @fclose($this->_file);
  125. $this->_data[$locale][''] = trim($this->_data[$locale]['']);
  126. if (empty($this->_data[$locale][''])) {
  127. $this->_adapterInfo[$filename] = 'No adapter information available';
  128. } else {
  129. $this->_adapterInfo[$filename] = $this->_data[$locale][''];
  130. }
  131. unset($this->_data[$locale]['']);
  132. return $this->_data;
  133. }
  134. /**
  135. * Returns the adapter informations
  136. *
  137. * @return array Each loaded adapter information as array value
  138. */
  139. public function getAdapterInfo()
  140. {
  141. return $this->_adapterInfo;
  142. }
  143. /**
  144. * Returns the adapter name
  145. *
  146. * @return string
  147. */
  148. public function toString()
  149. {
  150. return "Gettext";
  151. }
  152. }