/XoopsEngine/lib/Xoops/Zend/Translate.php

https://github.com/xoops-pi/engine · PHP · 164 lines · 74 code · 14 blank · 76 comment · 14 complexity · 81a69b4735f4b5a16d72773b7e1d09f0 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework for Xoops Engine
  4. *
  5. * You may not change or alter any portion of this comment or credits
  6. * of supporting developers from this source code or any supporting source code
  7. * which is considered copyrighted (c) material of the original comment or credit authors.
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * @copyright Xoops Engine http://www.xoopsengine.org/
  13. * @license http://www.fsf.org/copyleft/gpl.html GNU public license
  14. * @author Taiwen Jiang <phppp@users.sourceforge.net>
  15. * @since 3.0
  16. * @category Xoops_Zend
  17. * @package Translate
  18. * @version $Id$
  19. */
  20. class Xoops_Zend_Translate extends Zend_Translate
  21. {
  22. const DEFAULT_ADAPTER = 'Legacy';
  23. //protected static $loaded;
  24. protected $adapter;
  25. /**
  26. * Adapter
  27. *
  28. * @var Zend_Translate_Adapter
  29. */
  30. //protected $_adapter;
  31. //protected $_currentAdapter;
  32. //protected static $adapters = array();
  33. //protected static $_cache = null;
  34. //protected $options = array();
  35. //protected $locale = null;
  36. //protected static $loaded = array();
  37. /**
  38. * Generates the standard translation object
  39. *
  40. * @param array|Zend_Config $options Options to use
  41. */
  42. public function ______construct($options = array())
  43. {
  44. if ($options instanceof Zend_Config) {
  45. $options = $options->toArray();
  46. } else if (!is_array($options)) {
  47. $options = array('adapter' => $options);
  48. }
  49. $this->setAdapter($options);
  50. }
  51. /**
  52. * Sets a new adapter
  53. *
  54. * @param array|Zend_Config $options Options to use
  55. */
  56. public function setAdapter($options = array())
  57. {
  58. if ($options instanceof Zend_Config) {
  59. $options = $options->toArray();
  60. } else if (func_num_args() > 1) {
  61. $args = func_get_args();
  62. $options = array();
  63. $options['adapter'] = array_shift($args);
  64. if (!empty($args)) {
  65. $options['content'] = array_shift($args);
  66. }
  67. if (!empty($args)) {
  68. $options['locale'] = array_shift($args);
  69. }
  70. if (!empty($args)) {
  71. $opt = array_shift($args);
  72. $options = array_merge($opt, $options);
  73. }
  74. } else if (!is_array($options)) {
  75. $options = array('adapter' => $options);
  76. }
  77. if (array_key_exists('cache', $options)) {
  78. Zend_Translate_Adapter::setCache($options['cache']);
  79. }
  80. $adapter = !empty($options['adapter']) ? $options['adapter'] : static::DEFAULT_ADAPTER;
  81. unset($options['adapter']);
  82. $adapter = 'Zend_Translate_Adapter_' . ucfirst($adapter);
  83. if (class_exists('Xoops_' . $adapter)) {
  84. $adapter = 'Xoops_' . $adapter;
  85. }
  86. $this->adapter = new $adapter($options);
  87. }
  88. /**
  89. * Returns the adapters name and it's options
  90. *
  91. * @return Zend_Translate_Adapter
  92. */
  93. public function getAdapter()
  94. {
  95. return $this->adapter;
  96. }
  97. /**
  98. * Calls all methods from the adapter
  99. */
  100. public function __call($method, array $options)
  101. {
  102. if (method_exists($this->adapter, $method)) {
  103. return call_user_func_array(array($this->adapter, $method), $options);
  104. }
  105. throw new Zend_Translate_Exception("Unknown method '" . $method . "' called!");
  106. }
  107. /**
  108. * Load translation data
  109. *
  110. * @param string $data Translation data
  111. * @param string $domain global, module, theme, plugin, etc.
  112. * @param string|Zend_Locale $locale (optional) Locale/Language to add data for, identical
  113. * with locale identifier, see Zend_Locale for more information
  114. * @param array $options (optional) Option for this Adapter
  115. * @return Xoops_Zend_Translate Provides fluent interface
  116. */
  117. public function loadTranslation($data, $domain = "", $locale = null, $options = array())
  118. {
  119. //$key = md5("{$data}.{$domain}.{$locale}." . get_class($this->adapter) . "." . serialize($options));
  120. //if (!isset(self::$loaded[$key])) {
  121. //self::$loaded[$key] = 1;
  122. //$path = $this->adapter->getPath($domain, $locale);
  123. //if ($path) {
  124. //$data = $path . "/" . $data;
  125. try {
  126. $options['content'] = $data;
  127. $options['domain'] = $domain;
  128. $options['locale'] = $locale;
  129. //Debug::e($options);
  130. $this->adapter->addTranslation($options);
  131. } catch (Zend_Translate_Exception $e) {
  132. trigger_error("Translation data '{$data}' is failed: " . $e->getMessage());
  133. }
  134. //XOOPS::service('logger')->info("Translation data '{$data}' is loaded", "resource");
  135. //} else {
  136. // XOOPS::service('logger')->info("Translation data '{$data}' is failed: path not found", "resource");
  137. //}
  138. //}
  139. return $this;
  140. }
  141. /**
  142. * Get translation data path
  143. *
  144. *
  145. */
  146. public function getPath($domain = "", $locale = null)
  147. {
  148. return $this->adapter->getPath($domain, $locale);
  149. }
  150. }