Adapter.php 29 KB

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