[ZF-10089] Zend_Log
[zend.git] / library / Zend / Translate.php
blobc4f220b6f90c1b8c30756d134b2ff1982475e0d9
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
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$
22 /**
23 * @see Zend_Loader
25 require_once 'Zend/Loader.php';
28 /**
29 * @category Zend
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 {
35 /**
36 * Adapter names constants
38 const AN_ARRAY = 'Array';
39 const AN_CSV = 'Csv';
40 const AN_GETTEXT = 'Gettext';
41 const AN_INI = 'Ini';
42 const AN_QT = 'Qt';
43 const AN_TBX = 'Tbx';
44 const AN_TMX = 'Tmx';
45 const AN_XLIFF = 'Xliff';
46 const AN_XMLTM = 'XmlTm';
48 const LOCALE_DIRECTORY = 'directory';
49 const LOCALE_FILENAME = 'filename';
51 /**
52 * Adapter
54 * @var Zend_Translate_Adapter
56 private $_adapter;
57 private static $_cache = null;
59 /**
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();
71 $options = array();
72 $options['adapter'] = array_shift($args);
73 if (!empty($args)) {
74 $options['content'] = array_shift($args);
77 if (!empty($args)) {
78 $options['locale'] = array_shift($args);
81 if (!empty($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);
92 /**
93 * Sets a new adapter
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();
104 $options = array();
105 $options['adapter'] = array_shift($args);
106 if (!empty($args)) {
107 $options['content'] = array_shift($args);
110 if (!empty($args)) {
111 $options['locale'] = array_shift($args);
114 if (!empty($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
171 * @return void
173 public static function setCache(Zend_Cache_Core $cache)
175 self::$_cache = $cache;
179 * Returns true when a cache is set
181 * @return boolean
183 public static function hasCache()
185 if (self::$_cache !== null) {
186 return true;
189 return false;
193 * Removes any set cache
195 * @return void
197 public static function removeCache()
199 self::$_cache = null;
203 * Clears all set cache data
205 * @return void
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!");