Translate.php 6.2 KB

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