Adapter.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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 the message id for a given translation
  399. * If no locale is given, the actual language will be used
  400. *
  401. * @param string $message Message to get the key for
  402. * @param string|Zend_Locale $locale (optional) Language to return the message ids from
  403. * @return string|array|false
  404. */
  405. public function getMessageId($message, $locale = null)
  406. {
  407. if (empty($locale) or !$this->isAvailable($locale)) {
  408. $locale = $this->_options['locale'];
  409. }
  410. return array_search($message, $this->_translate[(string) $locale]);
  411. }
  412. /**
  413. * Returns all available message ids from this adapter
  414. * If no locale is given, the actual language will be used
  415. *
  416. * @param string|Zend_Locale $locale (optional) Language to return the message ids from
  417. * @return array
  418. */
  419. public function getMessageIds($locale = null)
  420. {
  421. if (empty($locale) or !$this->isAvailable($locale)) {
  422. $locale = $this->_options['locale'];
  423. }
  424. return array_keys($this->_translate[(string) $locale]);
  425. }
  426. /**
  427. * Returns all available translations from this adapter
  428. * If no locale is given, the actual language will be used
  429. * If 'all' is given the complete translation dictionary will be returned
  430. *
  431. * @param string|Zend_Locale $locale (optional) Language to return the messages from
  432. * @return array
  433. */
  434. public function getMessages($locale = null)
  435. {
  436. if ($locale === 'all') {
  437. return $this->_translate;
  438. }
  439. if ((empty($locale) === true) or ($this->isAvailable($locale) === false)) {
  440. $locale = $this->_options['locale'];
  441. }
  442. return $this->_translate[(string) $locale];
  443. }
  444. /**
  445. * Is the wished language available ?
  446. *
  447. * @see Zend_Locale
  448. * @param string|Zend_Locale $locale Language to search for, identical with locale identifier,
  449. * @see Zend_Locale for more information
  450. * @return boolean
  451. */
  452. public function isAvailable($locale)
  453. {
  454. $return = isset($this->_translate[(string) $locale]);
  455. return $return;
  456. }
  457. /**
  458. * Load translation data
  459. *
  460. * @param mixed $data
  461. * @param string|Zend_Locale $locale
  462. * @param array $options (optional)
  463. * @return array
  464. */
  465. abstract protected function _loadTranslationData($data, $locale, array $options = array());
  466. /**
  467. * Internal function for adding translation data
  468. *
  469. * This may be a new language or additional data for an existing language
  470. * If the options 'clear' is true, then the translation data for the specified
  471. * language is replaced and added otherwise
  472. *
  473. * @see Zend_Locale
  474. * @param array|Zend_Config $content Translation data to add
  475. * @throws Zend_Translate_Exception
  476. * @return Zend_Translate_Adapter Provides fluent interface
  477. */
  478. private function _addTranslationData($options = array())
  479. {
  480. if ($options instanceof Zend_Config) {
  481. $options = $options->toArray();
  482. } else if (func_num_args() > 1) {
  483. $args = func_get_args();
  484. $options['content'] = array_shift($args);
  485. if (!empty($args)) {
  486. $options['locale'] = array_shift($args);
  487. }
  488. if (!empty($args)) {
  489. $options += array_shift($args);
  490. }
  491. }
  492. if (($options['content'] instanceof Zend_Translate) || ($options['content'] instanceof Zend_Translate_Adapter)) {
  493. $options['usetranslateadapter'] = true;
  494. if (!empty($options['locale'])) {
  495. $options['content'] = $options['content']->getMessages($options['locale']);
  496. } else {
  497. $locales = $options['content']->getList();
  498. foreach ($locales as $locale) {
  499. $options['locale'] = $locale;
  500. $options['content'] = $options['content']->getMessages($locale);
  501. $this->_addTranslationData($options);
  502. }
  503. return $this;
  504. }
  505. }
  506. try {
  507. $options['locale'] = Zend_Locale::findLocale($options['locale']);
  508. } catch (Zend_Locale_Exception $e) {
  509. require_once 'Zend/Translate/Exception.php';
  510. throw new Zend_Translate_Exception("The given Language '{$options['locale']}' does not exist", 0, $e);
  511. }
  512. if ($options['clear'] || !isset($this->_translate[$options['locale']])) {
  513. $this->_translate[$options['locale']] = array();
  514. }
  515. $read = true;
  516. if (isset(self::$_cache)) {
  517. $id = 'Zend_Translate_' . md5(serialize($options['content'])) . '_' . $this->toString();
  518. $temp = self::$_cache->load($id);
  519. if ($temp) {
  520. $read = false;
  521. }
  522. }
  523. if ($options['reload']) {
  524. $read = true;
  525. }
  526. if ($read) {
  527. if (!empty($options['usetranslateadapter'])) {
  528. $temp = array($options['locale'] => $options['content']);
  529. } else {
  530. $temp = $this->_loadTranslationData($options['content'], $options['locale'], $options);
  531. }
  532. }
  533. if (empty($temp)) {
  534. $temp = array();
  535. }
  536. $keys = array_keys($temp);
  537. foreach($keys as $key) {
  538. if (!isset($this->_translate[$key])) {
  539. $this->_translate[$key] = array();
  540. }
  541. if (array_key_exists($key, $temp) && is_array($temp[$key])) {
  542. $this->_translate[$key] = $temp[$key] + $this->_translate[$key];
  543. }
  544. }
  545. if ($this->_automatic === true) {
  546. $find = new Zend_Locale($options['locale']);
  547. $browser = $find->getEnvironment() + $find->getBrowser();
  548. arsort($browser);
  549. foreach($browser as $language => $quality) {
  550. if (isset($this->_translate[$language])) {
  551. $this->_options['locale'] = $language;
  552. break;
  553. }
  554. }
  555. }
  556. if (($read) and (isset(self::$_cache))) {
  557. $id = 'Zend_Translate_' . md5(serialize($options['content'])) . '_' . $this->toString();
  558. self::$_cache->save($temp, $id, array('Zend_Translate'));
  559. }
  560. return $this;
  561. }
  562. /**
  563. * Translates the given string
  564. * returns the translation
  565. *
  566. * @see Zend_Locale
  567. * @param string|array $messageId Translation string, or Array for plural translations
  568. * @param string|Zend_Locale $locale (optional) Locale/Language to use, identical with
  569. * locale identifier, @see Zend_Locale for more information
  570. * @return string
  571. */
  572. public function translate($messageId, $locale = null)
  573. {
  574. if ($locale === null) {
  575. $locale = $this->_options['locale'];
  576. }
  577. $plural = null;
  578. if (is_array($messageId)) {
  579. if (count($messageId) > 2) {
  580. $number = array_pop($messageId);
  581. if (!is_numeric($number)) {
  582. $plocale = $number;
  583. $number = array_pop($messageId);
  584. } else {
  585. $plocale = 'en';
  586. }
  587. $plural = $messageId;
  588. $messageId = $messageId[0];
  589. } else {
  590. $messageId = $messageId[0];
  591. }
  592. }
  593. if (!Zend_Locale::isLocale($locale, true, false)) {
  594. if (!Zend_Locale::isLocale($locale, false, false)) {
  595. // language does not exist, return original string
  596. $this->_log($messageId, $locale);
  597. if ($plural === null) {
  598. return $messageId;
  599. }
  600. $rule = Zend_Translate_Plural::getPlural($number, $plocale);
  601. if (!isset($plural[$rule])) {
  602. $rule = 0;
  603. }
  604. return $plural[$rule];
  605. }
  606. $locale = new Zend_Locale($locale);
  607. }
  608. $locale = (string) $locale;
  609. if ((is_string($messageId) || is_int($messageId)) && isset($this->_translate[$locale][$messageId])) {
  610. // return original translation
  611. if ($plural === null) {
  612. return $this->_translate[$locale][$messageId];
  613. }
  614. $rule = Zend_Translate_Plural::getPlural($number, $locale);
  615. if (isset($this->_translate[$locale][$plural[0]][$rule])) {
  616. return $this->_translate[$locale][$plural[0]][$rule];
  617. }
  618. } else if (strlen($locale) != 2) {
  619. // faster than creating a new locale and separate the leading part
  620. $locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
  621. if ((is_string($messageId) || is_int($messageId)) && isset($this->_translate[$locale][$messageId])) {
  622. // return regionless translation (en_US -> en)
  623. if ($plural === null) {
  624. return $this->_translate[$locale][$messageId];
  625. }
  626. $rule = Zend_Translate_Plural::getPlural($number, $locale);
  627. if (isset($this->_translate[$locale][$plural[0]][$rule])) {
  628. return $this->_translate[$locale][$plural[0]][$rule];
  629. }
  630. }
  631. }
  632. $this->_log($messageId, $locale);
  633. if ($plural === null) {
  634. return $messageId;
  635. }
  636. $rule = Zend_Translate_Plural::getPlural($number, $plocale);
  637. if (!isset($plural[$rule])) {
  638. $rule = 0;
  639. }
  640. return $plural[$rule];
  641. }
  642. /**
  643. * Translates the given string using plural notations
  644. * Returns the translated string
  645. *
  646. * @see Zend_Locale
  647. * @param string $singular Singular translation string
  648. * @param string $plural Plural translation string
  649. * @param integer $number Number for detecting the correct plural
  650. * @param string|Zend_Locale $locale (Optional) Locale/Language to use, identical with
  651. * locale identifier, @see Zend_Locale for more information
  652. * @return string
  653. */
  654. public function plural($singular, $plural, $number, $locale = null)
  655. {
  656. return $this->translate(array($singular, $plural, $number), $locale);
  657. }
  658. /**
  659. * Logs a message when the log option is set
  660. *
  661. * @param string $message Message to log
  662. * @param String $locale Locale to log
  663. */
  664. protected function _log($message, $locale) {
  665. if ($this->_options['logUntranslated']) {
  666. $message = str_replace('%message%', $message, $this->_options['logMessage']);
  667. $message = str_replace('%locale%', $locale, $message);
  668. if ($this->_options['log']) {
  669. $this->_options['log']->notice($message);
  670. } else {
  671. trigger_error($message, E_USER_NOTICE);
  672. }
  673. }
  674. }
  675. /**
  676. * Translates the given string
  677. * returns the translation
  678. *
  679. * @param string $messageId Translation string
  680. * @param string|Zend_Locale $locale (optional) Locale/Language to use, identical with locale
  681. * identifier, @see Zend_Locale for more information
  682. * @return string
  683. */
  684. public function _($messageId, $locale = null)
  685. {
  686. return $this->translate($messageId, $locale);
  687. }
  688. /**
  689. * Checks if a string is translated within the source or not
  690. * returns boolean
  691. *
  692. * @param string $messageId Translation string
  693. * @param boolean $original (optional) Allow translation only for original language
  694. * when true, a translation for 'en_US' would give false when it can
  695. * be translated with 'en' only
  696. * @param string|Zend_Locale $locale (optional) Locale/Language to use, identical with locale identifier,
  697. * see Zend_Locale for more information
  698. * @return boolean
  699. */
  700. public function isTranslated($messageId, $original = false, $locale = null)
  701. {
  702. if (($original !== false) and ($original !== true)) {
  703. $locale = $original;
  704. $original = false;
  705. }
  706. if ($locale === null) {
  707. $locale = $this->_options['locale'];
  708. }
  709. if (!Zend_Locale::isLocale($locale, true, false)) {
  710. if (!Zend_Locale::isLocale($locale, false, false)) {
  711. // language does not exist, return original string
  712. return false;
  713. }
  714. $locale = new Zend_Locale($locale);
  715. }
  716. $locale = (string) $locale;
  717. if ((is_string($messageId) || is_int($messageId)) && isset($this->_translate[$locale][$messageId])) {
  718. // return original translation
  719. return true;
  720. } else if ((strlen($locale) != 2) and ($original === false)) {
  721. // faster than creating a new locale and separate the leading part
  722. $locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
  723. if ((is_string($messageId) || is_int($messageId)) && isset($this->_translate[$locale][$messageId])) {
  724. // return regionless translation (en_US -> en)
  725. return true;
  726. }
  727. }
  728. // No translation found, return original
  729. return false;
  730. }
  731. /**
  732. * Returns the set cache
  733. *
  734. * @return Zend_Cache_Core The set cache
  735. */
  736. public static function getCache()
  737. {
  738. return self::$_cache;
  739. }
  740. /**
  741. * Sets a cache for all Zend_Translate_Adapters
  742. *
  743. * @param Zend_Cache_Core $cache Cache to store to
  744. */
  745. public static function setCache(Zend_Cache_Core $cache)
  746. {
  747. self::$_cache = $cache;
  748. }
  749. /**
  750. * Returns true when a cache is set
  751. *
  752. * @return boolean
  753. */
  754. public static function hasCache()
  755. {
  756. if (self::$_cache !== null) {
  757. return true;
  758. }
  759. return false;
  760. }
  761. /**
  762. * Removes any set cache
  763. *
  764. * @return void
  765. */
  766. public static function removeCache()
  767. {
  768. self::$_cache = null;
  769. }
  770. /**
  771. * Clears all set cache data
  772. *
  773. * @return void
  774. */
  775. public static function clearCache()
  776. {
  777. require_once 'Zend/Cache.php';
  778. self::$_cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('Zend_Translate'));
  779. }
  780. /**
  781. * Returns the adapter name
  782. *
  783. * @return string
  784. */
  785. abstract public function toString();
  786. }