2
0

Translate.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. * @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-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. if (!class_exists($adapter)) {
  82. Zend_Loader::loadClass($adapter);
  83. }
  84. if (self::$_cache !== null) {
  85. call_user_func(array($adapter, 'setCache'), self::$_cache);
  86. }
  87. $this->_adapter = new $adapter($data, $locale, $options);
  88. if (!$this->_adapter instanceof Zend_Translate_Adapter) {
  89. require_once 'Zend/Translate/Exception.php';
  90. throw new Zend_Translate_Exception("Adapter " . $adapter . " does not extend Zend_Translate_Adapter");
  91. }
  92. }
  93. /**
  94. * Returns the adapters name and it's options
  95. *
  96. * @return Zend_Translate_Adapter
  97. */
  98. public function getAdapter()
  99. {
  100. return $this->_adapter;
  101. }
  102. /**
  103. * Returns the set cache
  104. *
  105. * @return Zend_Cache_Core The set cache
  106. */
  107. public static function getCache()
  108. {
  109. return self::$_cache;
  110. }
  111. /**
  112. * Sets a cache for all instances of Zend_Translate
  113. *
  114. * @param Zend_Cache_Core $cache Cache to store to
  115. * @return void
  116. */
  117. public static function setCache(Zend_Cache_Core $cache)
  118. {
  119. self::$_cache = $cache;
  120. }
  121. /**
  122. * Returns true when a cache is set
  123. *
  124. * @return boolean
  125. */
  126. public static function hasCache()
  127. {
  128. if (self::$_cache !== null) {
  129. return true;
  130. }
  131. return false;
  132. }
  133. /**
  134. * Removes any set cache
  135. *
  136. * @return void
  137. */
  138. public static function removeCache()
  139. {
  140. self::$_cache = null;
  141. }
  142. /**
  143. * Clears all set cache data
  144. *
  145. * @return void
  146. */
  147. public static function clearCache()
  148. {
  149. self::$_cache->clean();
  150. }
  151. /**
  152. * Calls all methods from the adapter
  153. */
  154. public function __call($method, array $options)
  155. {
  156. if (method_exists($this->_adapter, $method)) {
  157. return call_user_func_array(array($this->_adapter, $method), $options);
  158. }
  159. require_once 'Zend/Translate/Exception.php';
  160. throw new Zend_Translate_Exception("Unknown method '" . $method . "' called!");
  161. }
  162. }