Locale.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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-2015 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_Application_Resource_ResourceAbstract
  24. */
  25. require_once 'Zend/Application/Resource/ResourceAbstract.php';
  26. /**
  27. * Resource for initializing the locale
  28. *
  29. * @uses Zend_Application_Resource_Base
  30. * @category Zend
  31. * @package Zend_Application
  32. * @subpackage Resource
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Application_Resource_Locale
  37. extends Zend_Application_Resource_ResourceAbstract
  38. {
  39. const DEFAULT_REGISTRY_KEY = 'Zend_Locale';
  40. /**
  41. * @var Zend_Locale
  42. */
  43. protected $_locale;
  44. /**
  45. * Defined by Zend_Application_Resource_Resource
  46. *
  47. * @return Zend_Locale
  48. */
  49. public function init()
  50. {
  51. return $this->getLocale();
  52. }
  53. /**
  54. * Retrieve locale object
  55. *
  56. * @return Zend_Locale
  57. */
  58. public function getLocale()
  59. {
  60. if (null === $this->_locale) {
  61. $options = $this->getOptions();
  62. if (!isset($options['default'])) {
  63. $this->_locale = new Zend_Locale();
  64. } elseif (!isset($options['force'])
  65. || (bool)$options['force'] == false
  66. ) {
  67. // Don't force any locale, just go for auto detection
  68. Zend_Locale::setDefault($options['default']);
  69. $this->_locale = new Zend_Locale();
  70. } else {
  71. $this->_locale = new Zend_Locale($options['default']);
  72. }
  73. $key = (isset($options['registry_key']) && !is_numeric($options['registry_key']))
  74. ? $options['registry_key']
  75. : self::DEFAULT_REGISTRY_KEY;
  76. Zend_Registry::set($key, $this->_locale);
  77. }
  78. return $this->_locale;
  79. }
  80. /**
  81. * Set the cache
  82. *
  83. * @param string|Zend_Cache_Core $cache
  84. * @return Zend_Application_Resource_Locale
  85. */
  86. public function setCache($cache)
  87. {
  88. if (is_string($cache)) {
  89. $bootstrap = $this->getBootstrap();
  90. if ($bootstrap instanceof Zend_Application_Bootstrap_ResourceBootstrapper
  91. && $bootstrap->hasPluginResource('CacheManager')
  92. ) {
  93. $cacheManager = $bootstrap->bootstrap('CacheManager')
  94. ->getResource('CacheManager');
  95. if (null !== $cacheManager && $cacheManager->hasCache($cache)) {
  96. $cache = $cacheManager->getCache($cache);
  97. }
  98. }
  99. }
  100. if ($cache instanceof Zend_Cache_Core) {
  101. Zend_Locale::setCache($cache);
  102. }
  103. return $this;
  104. }
  105. }