Translate.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id: Date.php 2498 2006-12-23 22:13:38Z thomas $
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  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-2008 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 string $adapter Adapter to use
  57. * @param array $data Translation source data for the adapter
  58. * Depends on the Adapter
  59. * @param string|Zend_Locale $locale OPTIONAL locale to use
  60. * @param array $options OPTIONAL options for the adapter
  61. * @throws Zend_Translate_Exception
  62. */
  63. public function __construct($adapter, $data, $locale = null, array $options = array())
  64. {
  65. $this->setAdapter($adapter, $data, $locale, $options);
  66. }
  67. /**
  68. * Sets a new adapter
  69. *
  70. * @param string $adapter Adapter to use
  71. * @param string|array $data Translation data
  72. * @param string|Zend_Locale $locale OPTIONAL locale to use
  73. * @param array $options OPTIONAL Options to use
  74. * @throws Zend_Translate_Exception
  75. */
  76. public function setAdapter($adapter, $data, $locale = null, array $options = array())
  77. {
  78. if (Zend_Loader::isReadable('Zend/Translate/Adapter/' . ucfirst($adapter). '.php')) {
  79. $adapter = 'Zend_Translate_Adapter_' . ucfirst($adapter);
  80. }
  81. Zend_Loader::loadClass($adapter);
  82. if (self::$_cache !== null) {
  83. call_user_func(array($adapter, 'setCache'), self::$_cache);
  84. }
  85. $this->_adapter = new $adapter($data, $locale, $options);
  86. if (!$this->_adapter instanceof Zend_Translate_Adapter) {
  87. require_once 'Zend/Translate/Exception.php';
  88. throw new Zend_Translate_Exception("Adapter " . $adapter . " does not extend Zend_Translate_Adapter");
  89. }
  90. }
  91. /**
  92. * Returns the adapters name and it's options
  93. *
  94. * @return Zend_Translate_Adapter
  95. */
  96. public function getAdapter()
  97. {
  98. return $this->_adapter;
  99. }
  100. /**
  101. * Returns the set cache
  102. *
  103. * @return Zend_Cache_Core The set cache
  104. */
  105. public static function getCache()
  106. {
  107. return self::$_cache;
  108. }
  109. /**
  110. * Sets a cache for all instances of Zend_Translate
  111. *
  112. * @param Zend_Cache_Core $cache Cache to store to
  113. * @return void
  114. */
  115. public static function setCache(Zend_Cache_Core $cache)
  116. {
  117. self::$_cache = $cache;
  118. }
  119. /**
  120. * Returns true when a cache is set
  121. *
  122. * @return boolean
  123. */
  124. public static function hasCache()
  125. {
  126. if (self::$_cache !== null) {
  127. return true;
  128. }
  129. return false;
  130. }
  131. /**
  132. * Removes any set cache
  133. *
  134. * @return void
  135. */
  136. public static function removeCache()
  137. {
  138. self::$_cache = null;
  139. }
  140. /**
  141. * Clears all set cache data
  142. *
  143. * @return void
  144. */
  145. public static function clearCache()
  146. {
  147. self::$_cache->clean();
  148. }
  149. /**
  150. * Calls all methods from the adapter
  151. */
  152. public function __call($method, array $options)
  153. {
  154. if (method_exists($this->_adapter, $method)) {
  155. return call_user_func_array(array($this->_adapter, $method), $options);
  156. }
  157. require_once 'Zend/Translate/Exception.php';
  158. throw new Zend_Translate_Exception("Unknown method '" . $method . "' called!");
  159. }
  160. }