Adapter.php 27 KB

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