Adapter.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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, array('Zend_Translate'));
  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, array('Zend_Translate'));
  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'])) {
  501. $options['content'] = $options['content']->getMessages($options['locale']);
  502. } else {
  503. $locales = $options['content']->getList();
  504. foreach ($locales as $locale) {
  505. $options['locale'] = $locale;
  506. $options['content'] = $options['content']->getMessages($locale);
  507. $this->_addTranslationData($options);
  508. }
  509. return $this;
  510. }
  511. }
  512. try {
  513. $options['locale'] = Zend_Locale::findLocale($options['locale']);
  514. } catch (Zend_Locale_Exception $e) {
  515. require_once 'Zend/Translate/Exception.php';
  516. throw new Zend_Translate_Exception("The given Language '{$options['locale']}' does not exist", 0, $e);
  517. }
  518. if ($options['clear'] || !isset($this->_translate[$options['locale']])) {
  519. $this->_translate[$options['locale']] = array();
  520. }
  521. $read = true;
  522. if (isset(self::$_cache)) {
  523. $id = 'Zend_Translate_' . md5(serialize($options['content'])) . '_' . $this->toString();
  524. $temp = self::$_cache->load($id);
  525. if ($temp) {
  526. $read = false;
  527. }
  528. }
  529. if ($options['reload']) {
  530. $read = true;
  531. }
  532. if ($read) {
  533. if (!empty($options['usetranslateadapter'])) {
  534. $temp = array($options['locale'] => $options['content']);
  535. } else {
  536. $temp = $this->_loadTranslationData($options['content'], $options['locale'], $options);
  537. }
  538. }
  539. if (empty($temp)) {
  540. $temp = array();
  541. }
  542. $keys = array_keys($temp);
  543. foreach($keys as $key) {
  544. if (!isset($this->_translate[$key])) {
  545. $this->_translate[$key] = array();
  546. }
  547. if (array_key_exists($key, $temp) && is_array($temp[$key])) {
  548. $this->_translate[$key] = $temp[$key] + $this->_translate[$key];
  549. }
  550. }
  551. if ($this->_automatic === true) {
  552. $find = new Zend_Locale($options['locale']);
  553. $browser = $find->getEnvironment() + $find->getBrowser();
  554. arsort($browser);
  555. foreach($browser as $language => $quality) {
  556. if (isset($this->_translate[$language])) {
  557. $this->_options['locale'] = $language;
  558. break;
  559. }
  560. }
  561. }
  562. if (($read) and (isset(self::$_cache))) {
  563. $id = 'Zend_Translate_' . md5(serialize($options['content'])) . '_' . $this->toString();
  564. self::$_cache->save($temp, $id, array('Zend_Translate'));
  565. }
  566. return $this;
  567. }
  568. /**
  569. * Translates the given string
  570. * returns the translation
  571. *
  572. * @see Zend_Locale
  573. * @param string|array $messageId Translation string, or Array for plural translations
  574. * @param string|Zend_Locale $locale (optional) Locale/Language to use, identical with
  575. * locale identifier, @see Zend_Locale for more information
  576. * @return string
  577. */
  578. public function translate($messageId, $locale = null)
  579. {
  580. if ($locale === null) {
  581. $locale = $this->_options['locale'];
  582. }
  583. $plural = null;
  584. if (is_array($messageId)) {
  585. if (count($messageId) > 2) {
  586. $number = array_pop($messageId);
  587. if (!is_numeric($number)) {
  588. $plocale = $number;
  589. $number = array_pop($messageId);
  590. } else {
  591. $plocale = 'en';
  592. }
  593. $plural = $messageId;
  594. $messageId = $messageId[0];
  595. } else {
  596. $messageId = $messageId[0];
  597. }
  598. }
  599. if (!Zend_Locale::isLocale($locale, true, false)) {
  600. if (!Zend_Locale::isLocale($locale, false, false)) {
  601. // language does not exist, return original string
  602. $this->_log($messageId, $locale);
  603. // use rerouting when enabled
  604. if (!empty($this->_options['route'])) {
  605. if (array_key_exists($locale, $this->_options['route']) &&
  606. !array_key_exists($locale, $this->_routed)) {
  607. $this->_routed[$locale] = true;
  608. return $this->translate($messageId, $this->_options['route'][$locale]);
  609. }
  610. $this->_routed = array();
  611. }
  612. if ($plural === null) {
  613. return $messageId;
  614. }
  615. $rule = Zend_Translate_Plural::getPlural($number, $plocale);
  616. if (!isset($plural[$rule])) {
  617. $rule = 0;
  618. }
  619. return $plural[$rule];
  620. }
  621. $locale = new Zend_Locale($locale);
  622. }
  623. $locale = (string) $locale;
  624. if ((is_string($messageId) || is_int($messageId)) && isset($this->_translate[$locale][$messageId])) {
  625. // return original translation
  626. if ($plural === null) {
  627. return $this->_translate[$locale][$messageId];
  628. }
  629. $rule = Zend_Translate_Plural::getPlural($number, $locale);
  630. if (isset($this->_translate[$locale][$plural[0]][$rule])) {
  631. return $this->_translate[$locale][$plural[0]][$rule];
  632. }
  633. } else if (strlen($locale) != 2) {
  634. // faster than creating a new locale and separate the leading part
  635. $locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
  636. if ((is_string($messageId) || is_int($messageId)) && isset($this->_translate[$locale][$messageId])) {
  637. // return regionless translation (en_US -> en)
  638. if ($plural === null) {
  639. return $this->_translate[$locale][$messageId];
  640. }
  641. $rule = Zend_Translate_Plural::getPlural($number, $locale);
  642. if (isset($this->_translate[$locale][$plural[0]][$rule])) {
  643. return $this->_translate[$locale][$plural[0]][$rule];
  644. }
  645. }
  646. }
  647. $this->_log($messageId, $locale);
  648. // use rerouting when enabled
  649. if (!empty($this->_options['route'])) {
  650. if (array_key_exists($locale, $this->_options['route']) &&
  651. !array_key_exists($locale, $this->_routed)) {
  652. $this->_routed[$locale] = true;
  653. return $this->translate($messageId, $this->_options['route'][$locale]);
  654. }
  655. $this->_routed = array();
  656. }
  657. if ($plural === null) {
  658. return $messageId;
  659. }
  660. $rule = Zend_Translate_Plural::getPlural($number, $plocale);
  661. if (!isset($plural[$rule])) {
  662. $rule = 0;
  663. }
  664. return $plural[$rule];
  665. }
  666. /**
  667. * Translates the given string using plural notations
  668. * Returns the translated string
  669. *
  670. * @see Zend_Locale
  671. * @param string $singular Singular translation string
  672. * @param string $plural Plural translation string
  673. * @param integer $number Number for detecting the correct plural
  674. * @param string|Zend_Locale $locale (Optional) Locale/Language to use, identical with
  675. * locale identifier, @see Zend_Locale for more information
  676. * @return string
  677. */
  678. public function plural($singular, $plural, $number, $locale = null)
  679. {
  680. return $this->translate(array($singular, $plural, $number), $locale);
  681. }
  682. /**
  683. * Logs a message when the log option is set
  684. *
  685. * @param string $message Message to log
  686. * @param String $locale Locale to log
  687. */
  688. protected function _log($message, $locale) {
  689. if ($this->_options['logUntranslated']) {
  690. $message = str_replace('%message%', $message, $this->_options['logMessage']);
  691. $message = str_replace('%locale%', $locale, $message);
  692. if ($this->_options['log']) {
  693. $this->_options['log']->notice($message);
  694. } else {
  695. trigger_error($message, E_USER_NOTICE);
  696. }
  697. }
  698. }
  699. /**
  700. * Translates the given string
  701. * returns the translation
  702. *
  703. * @param string $messageId Translation string
  704. * @param string|Zend_Locale $locale (optional) Locale/Language to use, identical with locale
  705. * identifier, @see Zend_Locale for more information
  706. * @return string
  707. */
  708. public function _($messageId, $locale = null)
  709. {
  710. return $this->translate($messageId, $locale);
  711. }
  712. /**
  713. * Checks if a string is translated within the source or not
  714. * returns boolean
  715. *
  716. * @param string $messageId Translation string
  717. * @param boolean $original (optional) Allow translation only for original language
  718. * when true, a translation for 'en_US' would give false when it can
  719. * be translated with 'en' only
  720. * @param string|Zend_Locale $locale (optional) Locale/Language to use, identical with locale identifier,
  721. * see Zend_Locale for more information
  722. * @return boolean
  723. */
  724. public function isTranslated($messageId, $original = false, $locale = null)
  725. {
  726. if (($original !== false) and ($original !== true)) {
  727. $locale = $original;
  728. $original = false;
  729. }
  730. if ($locale === null) {
  731. $locale = $this->_options['locale'];
  732. }
  733. if (!Zend_Locale::isLocale($locale, true, false)) {
  734. if (!Zend_Locale::isLocale($locale, false, false)) {
  735. // language does not exist, return original string
  736. return false;
  737. }
  738. $locale = new Zend_Locale($locale);
  739. }
  740. $locale = (string) $locale;
  741. if ((is_string($messageId) || is_int($messageId)) && isset($this->_translate[$locale][$messageId])) {
  742. // return original translation
  743. return true;
  744. } else if ((strlen($locale) != 2) and ($original === false)) {
  745. // faster than creating a new locale and separate the leading part
  746. $locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
  747. if ((is_string($messageId) || is_int($messageId)) && isset($this->_translate[$locale][$messageId])) {
  748. // return regionless translation (en_US -> en)
  749. return true;
  750. }
  751. }
  752. // No translation found, return original
  753. return false;
  754. }
  755. /**
  756. * Returns the set cache
  757. *
  758. * @return Zend_Cache_Core The set cache
  759. */
  760. public static function getCache()
  761. {
  762. return self::$_cache;
  763. }
  764. /**
  765. * Sets a cache for all Zend_Translate_Adapters
  766. *
  767. * @param Zend_Cache_Core $cache Cache to store to
  768. */
  769. public static function setCache(Zend_Cache_Core $cache)
  770. {
  771. self::$_cache = $cache;
  772. }
  773. /**
  774. * Returns true when a cache is set
  775. *
  776. * @return boolean
  777. */
  778. public static function hasCache()
  779. {
  780. if (self::$_cache !== null) {
  781. return true;
  782. }
  783. return false;
  784. }
  785. /**
  786. * Removes any set cache
  787. *
  788. * @return void
  789. */
  790. public static function removeCache()
  791. {
  792. self::$_cache = null;
  793. }
  794. /**
  795. * Clears all set cache data
  796. *
  797. * @return void
  798. */
  799. public static function clearCache()
  800. {
  801. require_once 'Zend/Cache.php';
  802. self::$_cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('Zend_Translate'));
  803. }
  804. /**
  805. * Returns the adapter name
  806. *
  807. * @return string
  808. */
  809. abstract public function toString();
  810. }