Adapter.php 29 KB

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