Translate.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_View
  17. * @subpackage Helper
  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. /** Zend_Locale */
  23. require_once 'Zend/Locale.php';
  24. /** Zend_View_Helper_Abstract.php */
  25. require_once 'Zend/View/Helper/Abstract.php';
  26. /**
  27. * Translation view helper
  28. *
  29. * @category Zend
  30. * @package Zend_View
  31. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_View_Helper_Translate extends Zend_View_Helper_Abstract
  35. {
  36. /**
  37. * Translation object
  38. *
  39. * @var Zend_Translate_Adapter
  40. */
  41. protected $_translator;
  42. /**
  43. * Constructor for manually handling
  44. *
  45. * @param Zend_Translate|Zend_Translate_Adapter $translate Instance of Zend_Translate
  46. */
  47. public function __construct($translate = null)
  48. {
  49. if (empty($translate) === false) {
  50. $this->setTranslator($translate);
  51. }
  52. }
  53. /**
  54. * Translate a message
  55. * You can give multiple params or an array of params.
  56. * If you want to output another locale just set it as last single parameter
  57. * Example 1: translate('%1\$s + %2\$s', $value1, $value2, $locale);
  58. * Example 2: translate('%1\$s + %2\$s', array($value1, $value2), $locale);
  59. *
  60. * @param string $messageid Id of the message to be translated
  61. * @return string Translated message
  62. */
  63. public function translate($messageid = null)
  64. {
  65. if ($messageid === null) {
  66. return $this;
  67. }
  68. $translate = $this->getTranslator();
  69. $options = func_get_args();
  70. array_shift($options);
  71. $count = count($options);
  72. $locale = null;
  73. if ($count > 0) {
  74. if (Zend_Locale::isLocale($options[($count - 1)], null, false) !== false) {
  75. $locale = array_pop($options);
  76. }
  77. }
  78. if ((count($options) === 1) and (is_array($options[0]) === true)) {
  79. $options = $options[0];
  80. }
  81. if ($translate !== null) {
  82. $messageid = $translate->translate($messageid, $locale);
  83. }
  84. if (count($options) === 0) {
  85. return $messageid;
  86. }
  87. return vsprintf($messageid, $options);
  88. }
  89. /**
  90. * Sets a translation Adapter for translation
  91. *
  92. * @param Zend_Translate|Zend_Translate_Adapter $translate Instance of Zend_Translate
  93. * @throws Zend_View_Exception When no or a false instance was set
  94. * @return Zend_View_Helper_Translate
  95. */
  96. public function setTranslator($translate)
  97. {
  98. if ($translate instanceof Zend_Translate_Adapter) {
  99. $this->_translator = $translate;
  100. } else if ($translate instanceof Zend_Translate) {
  101. $this->_translator = $translate->getAdapter();
  102. } else {
  103. require_once 'Zend/View/Exception.php';
  104. throw new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter');
  105. }
  106. return $this;
  107. }
  108. /**
  109. * Retrieve translation object
  110. *
  111. * If none is currently registered, attempts to pull it from the registry
  112. * using the key 'Zend_Translate'.
  113. *
  114. * @return Zend_Translate_Adapter|null
  115. */
  116. public function getTranslator()
  117. {
  118. if ($this->_translator === null) {
  119. require_once 'Zend/Registry.php';
  120. if (Zend_Registry::isRegistered('Zend_Translate') === true) {
  121. $this->setTranslator(Zend_Registry::get('Zend_Translate'));
  122. }
  123. }
  124. return $this->_translator;
  125. }
  126. /**
  127. * Set's an new locale for all further translations
  128. *
  129. * @param string|Zend_Locale $locale New locale to set
  130. * @throws Zend_View_Exception When no Zend_Translate instance was set
  131. * @return Zend_View_Helper_Translate
  132. */
  133. public function setLocale($locale = null)
  134. {
  135. $translate = $this->getTranslator();
  136. if ($translate === null) {
  137. require_once 'Zend/View/Exception.php';
  138. throw new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter');
  139. }
  140. $translate->setLocale($locale);
  141. return $this;
  142. }
  143. /**
  144. * Returns the set locale for translations
  145. *
  146. * @throws Zend_View_Exception When no Zend_Translate instance was set
  147. * @return string|Zend_Locale
  148. */
  149. public function getLocale()
  150. {
  151. $translate = $this->getTranslator();
  152. if ($translate === null) {
  153. require_once 'Zend/View/Exception.php';
  154. throw new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter');
  155. }
  156. return $translate->getLocale();
  157. }
  158. }