PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/Translate/Adapter.php

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