Adapter.php 34 KB

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