Translate.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. if ($translate === null) {
  70. return $messageid;
  71. }
  72. $options = func_get_args();
  73. array_shift($options);
  74. $count = count($options);
  75. $locale = null;
  76. if ($count > 0) {
  77. if (Zend_Locale::isLocale($options[($count - 1)], null, false) !== false) {
  78. $locale = array_pop($options);
  79. }
  80. }
  81. if ((count($options) === 1) and (is_array($options[0]) === true)) {
  82. $options = $options[0];
  83. }
  84. $message = $translate->translate($messageid, $locale);
  85. if (count($options) === 0) {
  86. return $message;
  87. }
  88. return vsprintf($message, $options);
  89. }
  90. /**
  91. * Sets a translation Adapter for translation
  92. *
  93. * @param Zend_Translate|Zend_Translate_Adapter $translate Instance of Zend_Translate
  94. * @throws Zend_View_Exception When no or a false instance was set
  95. * @return Zend_View_Helper_Translate
  96. */
  97. public function setTranslator($translate)
  98. {
  99. if ($translate instanceof Zend_Translate_Adapter) {
  100. $this->_translator = $translate;
  101. } else if ($translate instanceof Zend_Translate) {
  102. $this->_translator = $translate->getAdapter();
  103. } else {
  104. require_once 'Zend/View/Exception.php';
  105. throw new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter');
  106. }
  107. return $this;
  108. }
  109. /**
  110. * Retrieve translation object
  111. *
  112. * If none is currently registered, attempts to pull it from the registry
  113. * using the key 'Zend_Translate'.
  114. *
  115. * @return Zend_Translate_Adapter|null
  116. */
  117. public function getTranslator()
  118. {
  119. if ($this->_translator === null) {
  120. require_once 'Zend/Registry.php';
  121. if (Zend_Registry::isRegistered('Zend_Translate') === true) {
  122. $this->setTranslator(Zend_Registry::get('Zend_Translate'));
  123. }
  124. }
  125. return $this->_translator;
  126. }
  127. /**
  128. * Set's an new locale for all further translations
  129. *
  130. * @param string|Zend_Locale $locale New locale to set
  131. * @throws Zend_View_Exception When no Zend_Translate instance was set
  132. * @return Zend_View_Helper_Translate
  133. */
  134. public function setLocale($locale = null)
  135. {
  136. $translate = $this->getTranslator();
  137. if ($translate === null) {
  138. require_once 'Zend/View/Exception.php';
  139. throw new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter');
  140. }
  141. $translate->setLocale($locale);
  142. return $this;
  143. }
  144. /**
  145. * Returns the set locale for translations
  146. *
  147. * @throws Zend_View_Exception When no Zend_Translate instance was set
  148. * @return string|Zend_Locale
  149. */
  150. public function getLocale()
  151. {
  152. $translate = $this->getTranslator();
  153. if ($translate === null) {
  154. require_once 'Zend/View/Exception.php';
  155. throw new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter');
  156. }
  157. return $translate->getLocale();
  158. }
  159. }