Adapter.php 34 KB

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