Adapter.php 31 KB

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