Translate.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_Application
  17. * @subpackage Resource
  18. * @copyright Copyright (c) 2005-2009 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. * Resource for setting translation options
  24. *
  25. * @uses Zend_Application_Resource_ResourceAbstract
  26. * @category Zend
  27. * @package Zend_Application
  28. * @subpackage Resource
  29. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Application_Resource_Translate extends Zend_Application_Resource_ResourceAbstract
  33. {
  34. const DEFAULT_REGISTRY_KEY = 'Zend_Translate';
  35. /**
  36. * @var Zend_Translate
  37. */
  38. protected $_translate;
  39. /**
  40. * Defined by Zend_Application_Resource_Resource
  41. *
  42. * @return Zend_Translate
  43. */
  44. public function init()
  45. {
  46. return $this->getTranslate();
  47. }
  48. /**
  49. * Retrieve translate object
  50. *
  51. * @return Zend_Translate
  52. */
  53. public function getTranslate()
  54. {
  55. if (null === $this->_translate) {
  56. $options = $this->getOptions();
  57. if (!isset($options['data'])) {
  58. throw new Zend_Application_Resource_Exception('No translation source data provided.');
  59. }
  60. $adapter = isset($options['adapter']) ? $options['adapter'] : Zend_Translate::AN_ARRAY;
  61. $locale = isset($options['locale']) ? $options['locale'] : null;
  62. $translateOptions = isset($options['options']) ? $options['options'] : array();
  63. $this->_translate = new Zend_Translate(
  64. $adapter, $options['data'], $locale, $translateOptions
  65. );
  66. $key = (isset($options['registry_key']) && !is_numeric($options['registry_key']))
  67. ? $options['registry_key']
  68. : self::DEFAULT_REGISTRY_KEY;
  69. Zend_Registry::set($key, $this->_translate);
  70. }
  71. return $this->_translate;
  72. }
  73. }