Translate.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 (array_key_exists('cache', $options)) {
  116. self::setCache($options['cache']);
  117. }
  118. if (self::$_cache !== null) {
  119. $options['cache'] = self::getCache();
  120. }
  121. $adapter = $options['adapter'];
  122. unset($options['adapter']);
  123. $this->_adapter = new $adapter($options);
  124. if (!$this->_adapter instanceof Zend_Translate_Adapter) {
  125. require_once 'Zend/Translate/Exception.php';
  126. throw new Zend_Translate_Exception("Adapter " . $adapter . " does not extend Zend_Translate_Adapter");
  127. }
  128. }
  129. /**
  130. * Returns the adapters name and it's options
  131. *
  132. * @return Zend_Translate_Adapter
  133. */
  134. public function getAdapter()
  135. {
  136. return $this->_adapter;
  137. }
  138. /**
  139. * Returns the set cache
  140. *
  141. * @return Zend_Cache_Core The set cache
  142. */
  143. public static function getCache()
  144. {
  145. return self::$_cache;
  146. }
  147. /**
  148. * Sets a cache for all instances of Zend_Translate
  149. *
  150. * @param Zend_Cache_Core $cache Cache to store to
  151. * @return void
  152. */
  153. public static function setCache(Zend_Cache_Core $cache)
  154. {
  155. self::$_cache = $cache;
  156. }
  157. /**
  158. * Returns true when a cache is set
  159. *
  160. * @return boolean
  161. */
  162. public static function hasCache()
  163. {
  164. if (self::$_cache !== null) {
  165. return true;
  166. }
  167. return false;
  168. }
  169. /**
  170. * Removes any set cache
  171. *
  172. * @return void
  173. */
  174. public static function removeCache()
  175. {
  176. self::$_cache = null;
  177. }
  178. /**
  179. * Clears all set cache data
  180. *
  181. * @return void
  182. */
  183. public static function clearCache()
  184. {
  185. self::$_cache->clean();
  186. }
  187. /**
  188. * Calls all methods from the adapter
  189. */
  190. public function __call($method, array $options)
  191. {
  192. if (method_exists($this->_adapter, $method)) {
  193. return call_user_func_array(array($this->_adapter, $method), $options);
  194. }
  195. require_once 'Zend/Translate/Exception.php';
  196. throw new Zend_Translate_Exception("Unknown method '" . $method . "' called!");
  197. }
  198. }