Translate.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Loader
  23. */
  24. require_once 'Zend/Loader.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 {
  32. /**
  33. * Adapter names constants
  34. */
  35. const AN_ARRAY = 'Array';
  36. const AN_CSV = 'Csv';
  37. const AN_GETTEXT = 'Gettext';
  38. const AN_INI = 'Ini';
  39. const AN_QT = 'Qt';
  40. const AN_TBX = 'Tbx';
  41. const AN_TMX = 'Tmx';
  42. const AN_XLIFF = 'Xliff';
  43. const AN_XMLTM = 'XmlTm';
  44. const LOCALE_DIRECTORY = 'directory';
  45. const LOCALE_FILENAME = 'filename';
  46. /**
  47. * Adapter
  48. *
  49. * @var Zend_Translate_Adapter
  50. */
  51. private $_adapter;
  52. private static $_cache = null;
  53. /**
  54. * Generates the standard translation object
  55. *
  56. * @param array|Zend_Config $options Options to use
  57. * @throws Zend_Translate_Exception
  58. */
  59. public function __construct($options = array())
  60. {
  61. if ($options instanceof Zend_Config) {
  62. $options = $options->toArray();
  63. } else if (func_num_args() > 1) {
  64. $args = func_get_args();
  65. $options = array();
  66. $options['adapter'] = array_shift($args);
  67. if (!empty($args)) {
  68. $options['content'] = array_shift($args);
  69. }
  70. if (!empty($args)) {
  71. $options['locale'] = array_shift($args);
  72. }
  73. if (!empty($args)) {
  74. $opt = array_shift($args);
  75. $options = array_merge($opt, $options);
  76. }
  77. } else if (!is_array($options)) {
  78. $options = array('adapter' => $options);
  79. }
  80. $this->setAdapter($options);
  81. }
  82. /**
  83. * Sets a new adapter
  84. *
  85. * @param array|Zend_Config $options Options to use
  86. * @throws Zend_Translate_Exception
  87. */
  88. public function setAdapter($options = array())
  89. {
  90. if ($options instanceof Zend_Config) {
  91. $options = $options->toArray();
  92. } else if (func_num_args() > 1) {
  93. $args = func_get_args();
  94. $options = array();
  95. $options['adapter'] = array_shift($args);
  96. if (!empty($args)) {
  97. $options['content'] = array_shift($args);
  98. }
  99. if (!empty($args)) {
  100. $options['locale'] = array_shift($args);
  101. }
  102. if (!empty($args)) {
  103. $opt = array_shift($args);
  104. $options = array_merge($opt, $options);
  105. }
  106. } else if (!is_array($options)) {
  107. $options = array('adapter' => $options);
  108. }
  109. if (Zend_Loader::isReadable('Zend/Translate/Adapter/' . ucfirst($options['adapter']). '.php')) {
  110. $options['adapter'] = 'Zend_Translate_Adapter_' . ucfirst($options['adapter']);
  111. }
  112. if (!class_exists($options['adapter'])) {
  113. Zend_Loader::loadClass($options['adapter']);
  114. }
  115. if (self::$_cache !== null) {
  116. call_user_func(array($options['adapter'], 'setCache'), self::$_cache);
  117. }
  118. $adapter = $options['adapter'];
  119. unset($options['adapter']);
  120. $this->_adapter = new $adapter($options);
  121. if (!$this->_adapter instanceof Zend_Translate_Adapter) {
  122. require_once 'Zend/Translate/Exception.php';
  123. throw new Zend_Translate_Exception("Adapter " . $adapter . " does not extend Zend_Translate_Adapter");
  124. }
  125. }
  126. /**
  127. * Returns the adapters name and it's options
  128. *
  129. * @return Zend_Translate_Adapter
  130. */
  131. public function getAdapter()
  132. {
  133. return $this->_adapter;
  134. }
  135. /**
  136. * Returns the set cache
  137. *
  138. * @return Zend_Cache_Core The set cache
  139. */
  140. public static function getCache()
  141. {
  142. return self::$_cache;
  143. }
  144. /**
  145. * Sets a cache for all instances of Zend_Translate
  146. *
  147. * @param Zend_Cache_Core $cache Cache to store to
  148. * @return void
  149. */
  150. public static function setCache(Zend_Cache_Core $cache)
  151. {
  152. self::$_cache = $cache;
  153. }
  154. /**
  155. * Returns true when a cache is set
  156. *
  157. * @return boolean
  158. */
  159. public static function hasCache()
  160. {
  161. if (self::$_cache !== null) {
  162. return true;
  163. }
  164. return false;
  165. }
  166. /**
  167. * Removes any set cache
  168. *
  169. * @return void
  170. */
  171. public static function removeCache()
  172. {
  173. self::$_cache = null;
  174. }
  175. /**
  176. * Clears all set cache data
  177. *
  178. * @return void
  179. */
  180. public static function clearCache()
  181. {
  182. self::$_cache->clean();
  183. }
  184. /**
  185. * Calls all methods from the adapter
  186. */
  187. public function __call($method, array $options)
  188. {
  189. if (method_exists($this->_adapter, $method)) {
  190. return call_user_func_array(array($this->_adapter, $method), $options);
  191. }
  192. require_once 'Zend/Translate/Exception.php';
  193. throw new Zend_Translate_Exception("Unknown method '" . $method . "' called!");
  194. }
  195. }