PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Zend/Translate.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 220 lines | 106 code | 24 blank | 90 comment | 21 complexity | ad818547a50732aca66bbf04eae3ea5e MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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: Translate.php 22591 2010-07-16 20:58:05Z thomas $
  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-2010 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 $options Options to use
  60. * @throws Zend_Translate_Exception
  61. */
  62. public function __construct($options = array())
  63. {
  64. if ($options instanceof Zend_Config) {
  65. $options = $options->toArray();
  66. } else if (func_num_args() > 1) {
  67. $args = func_get_args();
  68. $options = array();
  69. $options['adapter'] = array_shift($args);
  70. if (!empty($args)) {
  71. $options['content'] = array_shift($args);
  72. }
  73. if (!empty($args)) {
  74. $options['locale'] = array_shift($args);
  75. }
  76. if (!empty($args)) {
  77. $opt = array_shift($args);
  78. $options = array_merge($opt, $options);
  79. }
  80. } else if (!is_array($options)) {
  81. $options = array('adapter' => $options);
  82. }
  83. $this->setAdapter($options);
  84. }
  85. /**
  86. * Sets a new adapter
  87. *
  88. * @param array|Zend_Config $options Options to use
  89. * @throws Zend_Translate_Exception
  90. */
  91. public function setAdapter($options = array())
  92. {
  93. if ($options instanceof Zend_Config) {
  94. $options = $options->toArray();
  95. } else if (func_num_args() > 1) {
  96. $args = func_get_args();
  97. $options = array();
  98. $options['adapter'] = array_shift($args);
  99. if (!empty($args)) {
  100. $options['content'] = array_shift($args);
  101. }
  102. if (!empty($args)) {
  103. $options['locale'] = array_shift($args);
  104. }
  105. if (!empty($args)) {
  106. $opt = array_shift($args);
  107. $options = array_merge($opt, $options);
  108. }
  109. } else if (!is_array($options)) {
  110. $options = array('adapter' => $options);
  111. }
  112. if (Zend_Loader::isReadable('Zend/Translate/Adapter/' . ucfirst($options['adapter']). '.php')) {
  113. $options['adapter'] = 'Zend_Translate_Adapter_' . ucfirst($options['adapter']);
  114. }
  115. if (!class_exists($options['adapter'])) {
  116. Zend_Loader::loadClass($options['adapter']);
  117. }
  118. if (array_key_exists('cache', $options)) {
  119. Zend_Translate_Adapter::setCache($options['cache']);
  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 Zend_Translate_Adapter::getCache();
  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. Zend_Translate_Adapter::setCache($cache);
  156. }
  157. /**
  158. * Returns true when a cache is set
  159. *
  160. * @return boolean
  161. */
  162. public static function hasCache()
  163. {
  164. return Zend_Translate_Adapter::hasCache();
  165. }
  166. /**
  167. * Removes any set cache
  168. *
  169. * @return void
  170. */
  171. public static function removeCache()
  172. {
  173. Zend_Translate_Adapter::removeCache();
  174. }
  175. /**
  176. * Clears all set cache data
  177. *
  178. * @param string $tag Tag to clear when the default tag name is not used
  179. * @return void
  180. */
  181. public static function clearCache($tag = null)
  182. {
  183. Zend_Translate_Adapter::clearCache($tag);
  184. }
  185. /**
  186. * Calls all methods from the adapter
  187. */
  188. public function __call($method, array $options)
  189. {
  190. if (method_exists($this->_adapter, $method)) {
  191. return call_user_func_array(array($this->_adapter, $method), $options);
  192. }
  193. #require_once 'Zend/Translate/Exception.php';
  194. throw new Zend_Translate_Exception("Unknown method '" . $method . "' called!");
  195. }
  196. }