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.
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
25 require_once 'Zend/Loader.php';
30 * @package Zend_Translate
31 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
32 * @license http://framework.zend.com/license/new-bsd New BSD License
34 class Zend_Translate
{
36 * Adapter names constants
38 const AN_ARRAY
= 'Array';
40 const AN_GETTEXT
= 'Gettext';
45 const AN_XLIFF
= 'Xliff';
46 const AN_XMLTM
= 'XmlTm';
48 const LOCALE_DIRECTORY
= 'directory';
49 const LOCALE_FILENAME
= 'filename';
54 * @var Zend_Translate_Adapter
57 private static $_cache = null;
60 * Generates the standard translation object
62 * @param array|Zend_Config $options Options to use
63 * @throws Zend_Translate_Exception
65 public function __construct($options = array())
67 if ($options instanceof Zend_Config
) {
68 $options = $options->toArray();
69 } else if (func_num_args() > 1) {
70 $args = func_get_args();
72 $options['adapter'] = array_shift($args);
74 $options['content'] = array_shift($args);
78 $options['locale'] = array_shift($args);
82 $opt = array_shift($args);
83 $options = array_merge($opt, $options);
85 } else if (!is_array($options)) {
86 $options = array('adapter' => $options);
89 $this->setAdapter($options);
95 * @param array|Zend_Config $options Options to use
96 * @throws Zend_Translate_Exception
98 public function setAdapter($options = array())
100 if ($options instanceof Zend_Config
) {
101 $options = $options->toArray();
102 } else if (func_num_args() > 1) {
103 $args = func_get_args();
105 $options['adapter'] = array_shift($args);
107 $options['content'] = array_shift($args);
111 $options['locale'] = array_shift($args);
115 $opt = array_shift($args);
116 $options = array_merge($opt, $options);
118 } else if (!is_array($options)) {
119 $options = array('adapter' => $options);
122 if (Zend_Loader
::isReadable('Zend/Translate/Adapter/' . ucfirst($options['adapter']). '.php')) {
123 $options['adapter'] = 'Zend_Translate_Adapter_' . ucfirst($options['adapter']);
126 if (!class_exists($options['adapter'])) {
127 Zend_Loader
::loadClass($options['adapter']);
130 if (array_key_exists('cache', $options)) {
131 self
::setCache($options['cache']);
134 if (self
::$_cache !== null) {
135 $options['cache'] = self
::getCache();
138 $adapter = $options['adapter'];
139 unset($options['adapter']);
140 $this->_adapter
= new $adapter($options);
141 if (!$this->_adapter
instanceof Zend_Translate_Adapter
) {
142 require_once 'Zend/Translate/Exception.php';
143 throw new Zend_Translate_Exception("Adapter " . $adapter . " does not extend Zend_Translate_Adapter");
148 * Returns the adapters name and it's options
150 * @return Zend_Translate_Adapter
152 public function getAdapter()
154 return $this->_adapter
;
158 * Returns the set cache
160 * @return Zend_Cache_Core The set cache
162 public static function getCache()
164 return self
::$_cache;
168 * Sets a cache for all instances of Zend_Translate
170 * @param Zend_Cache_Core $cache Cache to store to
173 public static function setCache(Zend_Cache_Core
$cache)
175 self
::$_cache = $cache;
179 * Returns true when a cache is set
183 public static function hasCache()
185 if (self
::$_cache !== null) {
193 * Removes any set cache
197 public static function removeCache()
199 self
::$_cache = null;
203 * Clears all set cache data
207 public static function clearCache()
209 self
::$_cache->clean();
213 * Calls all methods from the adapter
215 public function __call($method, array $options)
217 if (method_exists($this->_adapter
, $method)) {
218 return call_user_func_array(array($this->_adapter
, $method), $options);
220 require_once 'Zend/Translate/Exception.php';
221 throw new Zend_Translate_Exception("Unknown method '" . $method . "' called!");