Translate.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. * @throws Zend_Application_Resource_Exception if registry key was used
  53. * already but is no instance of Zend_Translate
  54. */
  55. public function getTranslate()
  56. {
  57. if (null === $this->_translate) {
  58. $options = $this->getOptions();
  59. if (!isset($options['data'])) {
  60. throw new Zend_Application_Resource_Exception('No translation source data provided.');
  61. }
  62. $adapter = isset($options['adapter']) ? $options['adapter'] : Zend_Translate::AN_ARRAY;
  63. $locale = isset($options['locale']) ? $options['locale'] : null;
  64. $translateOptions = isset($options['options']) ? $options['options'] : array();
  65. $key = (isset($options['registry_key']) && !is_numeric($options['registry_key']))
  66. ? $options['registry_key']
  67. : self::DEFAULT_REGISTRY_KEY;
  68. if(Zend_Registry::isRegistered($key)) {
  69. $translate = Zend_Registry::get($key);
  70. if(!$translate instanceof Zend_Translate) {
  71. require_once 'Zend/Application/Resource/Exception.php';
  72. throw new Zend_Application_Resource_Exception($key
  73. . ' already registered in registry but is '
  74. . 'no instance of Zend_Translate');
  75. }
  76. $translate->addTranslation($options['data'], $locale, $options);
  77. $this->_translate = $translate;
  78. } else {
  79. $this->_translate = new Zend_Translate(
  80. $adapter, $options['data'], $locale, $translateOptions
  81. );
  82. Zend_Registry::set($key, $this->_translate);
  83. }
  84. }
  85. return $this->_translate;
  86. }
  87. }