Adapter.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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. * @subpackage Zend_Translate_Adapter
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Locale
  24. */
  25. require_once 'Zend/Locale.php';
  26. /**
  27. * @see Zend_Translate_Plural
  28. */
  29. require_once 'Zend/Translate/Plural.php';
  30. /**
  31. * Basic adapter class for each translation source adapter
  32. *
  33. * @category Zend
  34. * @package Zend_Translate
  35. * @subpackage Zend_Translate_Adapter
  36. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. abstract class Zend_Translate_Adapter {
  40. /**
  41. * Shows if locale detection is in automatic level
  42. * @var boolean
  43. */
  44. private $_automatic = true;
  45. /**
  46. * Internal cache for all adapters
  47. * @var Zend_Cache_Core
  48. */
  49. protected static $_cache = null;
  50. /**
  51. * Scans for the locale within the name of the directory
  52. * @constant integer
  53. */
  54. const LOCALE_DIRECTORY = 'directory';
  55. /**
  56. * Scans for the locale within the name of the file
  57. * @constant integer
  58. */
  59. const LOCALE_FILENAME = 'filename';
  60. /**
  61. * Array with all options, each adapter can have own additional options
  62. * 'clear' => when true, clears already loaded data when adding new files
  63. * 'content' => content to translate or file or directory with content
  64. * 'disableNotices' => when true, omits notices from being displayed
  65. * 'ignore' => a prefix for files and directories which are not being added
  66. * 'locale' => the actual set locale to use
  67. * 'log' => a instance of Zend_Log where logs are written to
  68. * 'logMessage' => message to be logged
  69. * 'logUntranslated' => when true, untranslated messages are not logged
  70. * 'reload' => reloads the cache by reading the content again
  71. * 'scan' => searches for translation files using the LOCALE constants
  72. *
  73. * @var array
  74. */
  75. protected $_options = array(
  76. 'clear' => false,
  77. 'content' => null,
  78. 'disableNotices' => false,
  79. 'ignore' => '.',
  80. 'locale' => 'auto',
  81. 'log' => null,
  82. 'logMessage' => "Untranslated message within '%locale%': %message%",
  83. 'logUntranslated' => false,
  84. 'reload' => false,
  85. 'scan' => null
  86. );
  87. /**
  88. * Translation table
  89. * @var array
  90. */
  91. protected $_translate = array();
  92. /**
  93. * Generates the adapter
  94. *
  95. * @param string|array $content OPTIONAL Translation content or filename for this adapter
  96. * @param string|Zend_Locale $locale OPTIONAL Locale/Language to set, identical with Locale
  97. * identifiers see Zend_Locale for more information
  98. * @param array $options OPTIONAL Options for the adaptor
  99. * @throws Zend_Translate_Exception
  100. * @return void
  101. */
  102. public function __construct($content = null, $locale = null, array $options = array())
  103. {
  104. if (isset(self::$_cache)) {
  105. $id = 'Zend_Translate_' . $this->toString() . '_Options';
  106. $result = self::$_cache->load($id);
  107. if ($result) {
  108. $this->_options = $result;
  109. }
  110. }
  111. if (($locale === "auto") or ($locale === null)) {
  112. $this->_automatic = true;
  113. } else {
  114. $this->_automatic = false;
  115. }
  116. $this->setOptions($options);
  117. if ($content !== null) {
  118. $this->addTranslation($content, $locale, $options);
  119. }
  120. if ($this->getLocale() !== (string) $locale) {
  121. $this->setLocale($locale);
  122. }
  123. }
  124. /**
  125. * Add translation data
  126. *
  127. * It may be a new language or additional data for existing language
  128. * If $clear parameter is true, then translation data for specified
  129. * language is replaced and added otherwise
  130. *
  131. * @param array|string $data Translation data
  132. * @param string|Zend_Locale $locale (optional) Locale/Language to add data for, identical
  133. * with locale identifier, see Zend_Locale for more information
  134. * @param array $options (optional) Option for this Adapter
  135. * @throws Zend_Translate_Exception
  136. * @return Zend_Translate_Adapter Provides fluent interface
  137. */
  138. public function addTranslation($data, $locale = null, array $options = array())
  139. {
  140. $originate = (string) $locale;
  141. if (array_key_exists('locale', $options)) {
  142. if ($locale == null) {
  143. $locale = $options['locale'];
  144. }
  145. unset($options['locale']);
  146. }
  147. if ((array_key_exists('log', $options)) && !($options['log'] instanceof Zend_Log)) {
  148. require_once 'Zend/Translate/Exception.php';
  149. throw new Zend_Translate_Exception('Instance of Zend_Log expected for option log');
  150. }
  151. try {
  152. if (!($data instanceof Zend_Translate) && !($data instanceof Zend_Translate_Adapter)) {
  153. $locale = Zend_Locale::findLocale($locale);
  154. }
  155. } catch (Zend_Locale_Exception $e) {
  156. require_once 'Zend/Translate/Exception.php';
  157. throw new Zend_Translate_Exception("The given Language '{$locale}' does not exist", 0, $e);
  158. }
  159. $options = $options + $this->_options;
  160. if (is_string($data) and is_dir($data)) {
  161. $data = realpath($data);
  162. $prev = '';
  163. foreach (new RecursiveIteratorIterator(
  164. new RecursiveDirectoryIterator($data, RecursiveDirectoryIterator::KEY_AS_PATHNAME),
  165. RecursiveIteratorIterator::SELF_FIRST) as $directory => $info) {
  166. $file = $info->getFilename();
  167. if (is_array($options['ignore'])) {
  168. foreach ($options['ignore'] as $key => $ignore) {
  169. if (strpos($key, 'regex') !== false) {
  170. if (preg_match($ignore, $directory)) {
  171. // ignore files matching the given regex from option 'ignore' and all files below
  172. continue 2;
  173. }
  174. } else if (strpos($directory, DIRECTORY_SEPARATOR . $ignore) !== false) {
  175. // ignore files matching first characters from option 'ignore' and all files below
  176. continue 2;
  177. }
  178. }
  179. } else {
  180. if (strpos($directory, DIRECTORY_SEPARATOR . $options['ignore']) !== false) {
  181. // ignore files matching first characters from option 'ignore' and all files below
  182. continue;
  183. }
  184. }
  185. if ($info->isDir()) {
  186. // pathname as locale
  187. if (($options['scan'] === self::LOCALE_DIRECTORY) and (Zend_Locale::isLocale($file, true, false))) {
  188. $locale = $file;
  189. $prev = (string) $locale;
  190. }
  191. } else if ($info->isFile()) {
  192. // filename as locale
  193. if ($options['scan'] === self::LOCALE_FILENAME) {
  194. $filename = explode('.', $file);
  195. array_pop($filename);
  196. $filename = implode('.', $filename);
  197. if (Zend_Locale::isLocale((string) $filename, true, false)) {
  198. $locale = (string) $filename;
  199. } else {
  200. $parts = explode('.', $file);
  201. $parts2 = array();
  202. foreach($parts as $token) {
  203. $parts2 += explode('_', $token);
  204. }
  205. $parts = array_merge($parts, $parts2);
  206. $parts2 = array();
  207. foreach($parts as $token) {
  208. $parts2 += explode('-', $token);
  209. }
  210. $parts = array_merge($parts, $parts2);
  211. $parts = array_unique($parts);
  212. $prev = '';
  213. foreach($parts as $token) {
  214. if (Zend_Locale::isLocale($token, true, false)) {
  215. if (strlen($prev) <= strlen($token)) {
  216. $locale = $token;
  217. $prev = $token;
  218. }
  219. }
  220. }
  221. }
  222. }
  223. try {
  224. $this->_addTranslationData($info->getPathname(), (string) $locale, $options);
  225. } catch (Zend_Translate_Exception $e) {
  226. // ignore failed sources while scanning
  227. }
  228. }
  229. }
  230. } else {
  231. $this->_addTranslationData($data, (string) $locale, $options);
  232. }
  233. if ((isset($this->_translate[$originate]) === true) and (count($this->_translate[$originate]) > 0)) {
  234. $this->setLocale($originate);
  235. }
  236. return $this;
  237. }
  238. /**
  239. * Sets new adapter options
  240. *
  241. * @param array $options Adapter options
  242. * @throws Zend_Translate_Exception
  243. * @return Zend_Translate_Adapter Provides fluent interface
  244. */
  245. public function setOptions(array $options = array())
  246. {
  247. $change = false;
  248. $locale = null;
  249. foreach ($options as $key => $option) {
  250. if ($key == 'locale') {
  251. $locale = $option;
  252. } else if ((isset($this->_options[$key]) and ($this->_options[$key] != $option)) or
  253. !isset($this->_options[$key])) {
  254. if (($key == 'log') && !($option instanceof Zend_Log)) {
  255. require_once 'Zend/Translate/Exception.php';
  256. throw new Zend_Translate_Exception('Instance of Zend_Log expected for option log');
  257. }
  258. $this->_options[$key] = $option;
  259. $change = true;
  260. }
  261. }
  262. if ($locale !== null) {
  263. $this->setLocale($locale);
  264. }
  265. if (isset(self::$_cache) and ($change == true)) {
  266. $id = 'Zend_Translate_' . $this->toString() . '_Options';
  267. self::$_cache->save($this->_options, $id, array('Zend_Translate'));
  268. }
  269. return $this;
  270. }
  271. /**
  272. * Returns the adapters name and it's options
  273. *
  274. * @param string|null $optionKey String returns this option
  275. * null returns all options
  276. * @return integer|string|array|null
  277. */
  278. public function getOptions($optionKey = null)
  279. {
  280. if ($optionKey === null) {
  281. return $this->_options;
  282. }
  283. if (isset($this->_options[$optionKey]) === true) {
  284. return $this->_options[$optionKey];
  285. }
  286. return null;
  287. }
  288. /**
  289. * Gets locale
  290. *
  291. * @return Zend_Locale|string|null
  292. */
  293. public function getLocale()
  294. {
  295. return $this->_options['locale'];
  296. }
  297. /**
  298. * Sets locale
  299. *
  300. * @param string|Zend_Locale $locale Locale to set
  301. * @throws Zend_Translate_Exception
  302. * @return Zend_Translate_Adapter Provides fluent interface
  303. */
  304. public function setLocale($locale)
  305. {
  306. if (($locale === "auto") or ($locale === null)) {
  307. $this->_automatic = true;
  308. } else {
  309. $this->_automatic = false;
  310. }
  311. try {
  312. $locale = Zend_Locale::findLocale($locale);
  313. } catch (Zend_Locale_Exception $e) {
  314. require_once 'Zend/Translate/Exception.php';
  315. throw new Zend_Translate_Exception("The given Language ({$locale}) does not exist", 0, $e);
  316. }
  317. if (!isset($this->_translate[$locale])) {
  318. $temp = explode('_', $locale);
  319. if (!isset($this->_translate[$temp[0]]) and !isset($this->_translate[$locale])) {
  320. if (!$this->_options['disableNotices']) {
  321. if ($this->_options['log']) {
  322. $this->_options['log']->notice("The language '{$locale}' has to be added before it can be used.");
  323. } else {
  324. trigger_error("The language '{$locale}' has to be added before it can be used.", E_USER_NOTICE);
  325. }
  326. }
  327. }
  328. $locale = $temp[0];
  329. }
  330. if (empty($this->_translate[$locale])) {
  331. if (!$this->_options['disableNotices']) {
  332. if ($this->_options['log']) {
  333. $this->_options['log']->notice("No translation for the language '{$locale}' available.");
  334. } else {
  335. trigger_error("No translation for the language '{$locale}' available.", E_USER_NOTICE);
  336. }
  337. }
  338. }
  339. if ($this->_options['locale'] != $locale) {
  340. $this->_options['locale'] = $locale;
  341. if (isset(self::$_cache)) {
  342. $id = 'Zend_Translate_' . $this->toString() . '_Options';
  343. self::$_cache->save($this->_options, $id, array('Zend_Translate'));
  344. }
  345. }
  346. return $this;
  347. }
  348. /**
  349. * Returns the available languages from this adapter
  350. *
  351. * @return array
  352. */
  353. public function getList()
  354. {
  355. $list = array_keys($this->_translate);
  356. $result = null;
  357. foreach($list as $value) {
  358. if (!empty($this->_translate[$value])) {
  359. $result[$value] = $value;
  360. }
  361. }
  362. return $result;
  363. }
  364. /**
  365. * Returns all available message ids from this adapter
  366. * If no locale is given, the actual language will be used
  367. *
  368. * @param string|Zend_Locale $locale (optional) Language to return the message ids from
  369. * @return array
  370. */
  371. public function getMessageIds($locale = null)
  372. {
  373. if (empty($locale) or !$this->isAvailable($locale)) {
  374. $locale = $this->_options['locale'];
  375. }
  376. return array_keys($this->_translate[(string) $locale]);
  377. }
  378. /**
  379. * Returns all available translations from this adapter
  380. * If no locale is given, the actual language will be used
  381. * If 'all' is given the complete translation dictionary will be returned
  382. *
  383. * @param string|Zend_Locale $locale (optional) Language to return the messages from
  384. * @return array
  385. */
  386. public function getMessages($locale = null)
  387. {
  388. if ($locale === 'all') {
  389. return $this->_translate;
  390. }
  391. if ((empty($locale) === true) or ($this->isAvailable($locale) === false)) {
  392. $locale = $this->_options['locale'];
  393. }
  394. return $this->_translate[(string) $locale];
  395. }
  396. /**
  397. * Is the wished language available ?
  398. *
  399. * @see Zend_Locale
  400. * @param string|Zend_Locale $locale Language to search for, identical with locale identifier,
  401. * @see Zend_Locale for more information
  402. * @return boolean
  403. */
  404. public function isAvailable($locale)
  405. {
  406. $return = isset($this->_translate[(string) $locale]);
  407. return $return;
  408. }
  409. /**
  410. * Load translation data
  411. *
  412. * @param mixed $data
  413. * @param string|Zend_Locale $locale
  414. * @param array $options (optional)
  415. * @return array
  416. */
  417. abstract protected function _loadTranslationData($data, $locale, array $options = array());
  418. /**
  419. * Internal function for adding translation data
  420. *
  421. * It may be a new language or additional data for existing language
  422. * If $clear parameter is true, then translation data for specified
  423. * language is replaced and added otherwise
  424. *
  425. * @see Zend_Locale
  426. * @param array|string $data Translation data
  427. * @param string|Zend_Locale $locale Locale/Language to add data for, identical with locale identifier,
  428. * @see Zend_Locale for more information
  429. * @param array $options (optional) Option for this Adapter
  430. * @throws Zend_Translate_Exception
  431. * @return Zend_Translate_Adapter Provides fluent interface
  432. */
  433. private function _addTranslationData($data, $locale, array $options = array())
  434. {
  435. if (($data instanceof Zend_Translate) || ($data instanceof Zend_Translate_Adapter)) {
  436. $options['usetranslateadapter'] = true;
  437. if (!empty($locale)) {
  438. $data = $data->getMessages($locale);
  439. } else {
  440. $locales = $data->getList();
  441. foreach ($locales as $locale) {
  442. $trans = $data->getMessages($locale);
  443. $this->_addTranslationData($trans, $locale, $options);
  444. }
  445. return $this;
  446. }
  447. }
  448. try {
  449. $locale = Zend_Locale::findLocale($locale);
  450. } catch (Zend_Locale_Exception $e) {
  451. require_once 'Zend/Translate/Exception.php';
  452. throw new Zend_Translate_Exception("The given Language '{$locale}' does not exist", 0, $e);
  453. }
  454. if ($options['clear'] || !isset($this->_translate[$locale])) {
  455. $this->_translate[$locale] = array();
  456. }
  457. $read = true;
  458. if (isset(self::$_cache)) {
  459. $id = 'Zend_Translate_' . md5(serialize($data)) . '_' . $this->toString();
  460. $temp = self::$_cache->load($id);
  461. if ($temp) {
  462. $read = false;
  463. }
  464. }
  465. if ($options['reload']) {
  466. $read = true;
  467. }
  468. if ($read) {
  469. if (!empty($options['usetranslateadapter'])) {
  470. $temp = array($locale => $data);
  471. } else {
  472. $temp = $this->_loadTranslationData($data, $locale, $options);
  473. }
  474. }
  475. if (empty($temp)) {
  476. $temp = array();
  477. }
  478. $keys = array_keys($temp);
  479. foreach($keys as $key) {
  480. if (!isset($this->_translate[$key])) {
  481. $this->_translate[$key] = array();
  482. }
  483. if (array_key_exists($key, $temp) && is_array($temp[$key])) {
  484. $this->_translate[$key] = $temp[$key] + $this->_translate[$key];
  485. }
  486. }
  487. if ($this->_automatic === true) {
  488. $find = new Zend_Locale($locale);
  489. $browser = $find->getEnvironment() + $find->getBrowser();
  490. arsort($browser);
  491. foreach($browser as $language => $quality) {
  492. if (isset($this->_translate[$language])) {
  493. $this->_options['locale'] = $language;
  494. break;
  495. }
  496. }
  497. }
  498. if (($read) and (isset(self::$_cache))) {
  499. $id = 'Zend_Translate_' . md5(serialize($data)) . '_' . $this->toString();
  500. self::$_cache->save($temp, $id, array('Zend_Translate'));
  501. }
  502. return $this;
  503. }
  504. /**
  505. * Translates the given string
  506. * returns the translation
  507. *
  508. * @see Zend_Locale
  509. * @param string|array $messageId Translation string, or Array for plural translations
  510. * @param string|Zend_Locale $locale (optional) Locale/Language to use, identical with
  511. * locale identifier, @see Zend_Locale for more information
  512. * @return string
  513. */
  514. public function translate($messageId, $locale = null)
  515. {
  516. if ($locale === null) {
  517. $locale = $this->_options['locale'];
  518. }
  519. $plural = null;
  520. if (is_array($messageId)) {
  521. if (count($messageId) > 2) {
  522. $number = array_pop($messageId);
  523. if (!is_numeric($number)) {
  524. $plocale = $number;
  525. $number = array_pop($messageId);
  526. } else {
  527. $plocale = 'en';
  528. }
  529. $plural = $messageId;
  530. $messageId = $messageId[0];
  531. } else {
  532. $messageId = $messageId[0];
  533. }
  534. }
  535. if (!Zend_Locale::isLocale($locale, true, false)) {
  536. if (!Zend_Locale::isLocale($locale, false, false)) {
  537. // language does not exist, return original string
  538. $this->_log($messageId, $locale);
  539. if ($plural === null) {
  540. return $messageId;
  541. }
  542. $rule = Zend_Translate_Plural::getPlural($number, $plocale);
  543. if (!isset($plural[$rule])) {
  544. $rule = 0;
  545. }
  546. return $plural[$rule];
  547. }
  548. $locale = new Zend_Locale($locale);
  549. }
  550. $locale = (string) $locale;
  551. if ((is_string($messageId) || is_int($messageId)) && isset($this->_translate[$locale][$messageId])) {
  552. // return original translation
  553. if ($plural === null) {
  554. return $this->_translate[$locale][$messageId];
  555. }
  556. $rule = Zend_Translate_Plural::getPlural($number, $locale);
  557. if (isset($this->_translate[$locale][$plural[0]][$rule])) {
  558. return $this->_translate[$locale][$plural[0]][$rule];
  559. }
  560. } else if (strlen($locale) != 2) {
  561. // faster than creating a new locale and separate the leading part
  562. $locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
  563. if ((is_string($messageId) || is_int($messageId)) && isset($this->_translate[$locale][$messageId])) {
  564. // return regionless translation (en_US -> en)
  565. if ($plural === null) {
  566. return $this->_translate[$locale][$messageId];
  567. }
  568. $rule = Zend_Translate_Plural::getPlural($number, $locale);
  569. if (isset($this->_translate[$locale][$plural[0]][$rule])) {
  570. return $this->_translate[$locale][$plural[0]][$rule];
  571. }
  572. }
  573. }
  574. $this->_log($messageId, $locale);
  575. if ($plural === null) {
  576. return $messageId;
  577. }
  578. $rule = Zend_Translate_Plural::getPlural($number, $plocale);
  579. if (!isset($plural[$rule])) {
  580. $rule = 0;
  581. }
  582. return $plural[$rule];
  583. }
  584. /**
  585. * Translates the given string using plural notations
  586. * Returns the translated string
  587. *
  588. * @see Zend_Locale
  589. * @param string $singular Singular translation string
  590. * @param string $plural Plural translation string
  591. * @param integer $number Number for detecting the correct plural
  592. * @param string|Zend_Locale $locale (Optional) Locale/Language to use, identical with
  593. * locale identifier, @see Zend_Locale for more information
  594. * @return string
  595. */
  596. public function plural($singular, $plural, $number, $locale = null)
  597. {
  598. return $this->translate(array($singular, $plural, $number), $locale);
  599. }
  600. /**
  601. * Logs a message when the log option is set
  602. *
  603. * @param string $message Message to log
  604. * @param String $locale Locale to log
  605. */
  606. protected function _log($message, $locale) {
  607. if ($this->_options['logUntranslated']) {
  608. $message = str_replace('%message%', $message, $this->_options['logMessage']);
  609. $message = str_replace('%locale%', $locale, $message);
  610. if ($this->_options['log']) {
  611. $this->_options['log']->notice($message);
  612. } else {
  613. trigger_error($message, E_USER_NOTICE);
  614. }
  615. }
  616. }
  617. /**
  618. * Translates the given string
  619. * returns the translation
  620. *
  621. * @param string $messageId Translation string
  622. * @param string|Zend_Locale $locale (optional) Locale/Language to use, identical with locale
  623. * identifier, @see Zend_Locale for more information
  624. * @return string
  625. */
  626. public function _($messageId, $locale = null)
  627. {
  628. return $this->translate($messageId, $locale);
  629. }
  630. /**
  631. * Checks if a string is translated within the source or not
  632. * returns boolean
  633. *
  634. * @param string $messageId Translation string
  635. * @param boolean $original (optional) Allow translation only for original language
  636. * when true, a translation for 'en_US' would give false when it can
  637. * be translated with 'en' only
  638. * @param string|Zend_Locale $locale (optional) Locale/Language to use, identical with locale identifier,
  639. * see Zend_Locale for more information
  640. * @return boolean
  641. */
  642. public function isTranslated($messageId, $original = false, $locale = null)
  643. {
  644. if (($original !== false) and ($original !== true)) {
  645. $locale = $original;
  646. $original = false;
  647. }
  648. if ($locale === null) {
  649. $locale = $this->_options['locale'];
  650. }
  651. if (!Zend_Locale::isLocale($locale, true, false)) {
  652. if (!Zend_Locale::isLocale($locale, false, false)) {
  653. // language does not exist, return original string
  654. return false;
  655. }
  656. $locale = new Zend_Locale($locale);
  657. }
  658. $locale = (string) $locale;
  659. if ((is_string($messageId) || is_int($messageId)) && isset($this->_translate[$locale][$messageId])) {
  660. // return original translation
  661. return true;
  662. } else if ((strlen($locale) != 2) and ($original === false)) {
  663. // faster than creating a new locale and separate the leading part
  664. $locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
  665. if ((is_string($messageId) || is_int($messageId)) && isset($this->_translate[$locale][$messageId])) {
  666. // return regionless translation (en_US -> en)
  667. return true;
  668. }
  669. }
  670. // No translation found, return original
  671. return false;
  672. }
  673. /**
  674. * Returns the set cache
  675. *
  676. * @return Zend_Cache_Core The set cache
  677. */
  678. public static function getCache()
  679. {
  680. return self::$_cache;
  681. }
  682. /**
  683. * Sets a cache for all Zend_Translate_Adapters
  684. *
  685. * @param Zend_Cache_Core $cache Cache to store to
  686. */
  687. public static function setCache(Zend_Cache_Core $cache)
  688. {
  689. self::$_cache = $cache;
  690. }
  691. /**
  692. * Returns true when a cache is set
  693. *
  694. * @return boolean
  695. */
  696. public static function hasCache()
  697. {
  698. if (self::$_cache !== null) {
  699. return true;
  700. }
  701. return false;
  702. }
  703. /**
  704. * Removes any set cache
  705. *
  706. * @return void
  707. */
  708. public static function removeCache()
  709. {
  710. self::$_cache = null;
  711. }
  712. /**
  713. * Clears all set cache data
  714. *
  715. * @return void
  716. */
  717. public static function clearCache()
  718. {
  719. require_once 'Zend/Cache.php';
  720. self::$_cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('Zend_Translate'));
  721. }
  722. /**
  723. * Returns the adapter name
  724. *
  725. * @return string
  726. */
  727. abstract public function toString();
  728. }