PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/www/system/library/Zend/Translate/Adapter.php

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