2
0

Translate.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 ($translate === null) {
  50. require_once 'Zend/Registry.php';
  51. if (Zend_Registry::isRegistered('Zend_Translate')) {
  52. $translate = Zend_Registry::get('Zend_Translate');
  53. }
  54. }
  55. if ($translate !== null) {
  56. $this->setTranslator($translate);
  57. }
  58. }
  59. /**
  60. * Translate a message
  61. * You can give multiple params or an array of params.
  62. * If you want to output another locale just set it as last single parameter
  63. * Example 1: translate('%1\$s + %2\$s', $value1, $value2, $locale);
  64. * Example 2: translate('%1\$s + %2\$s', array($value1, $value2), $locale);
  65. *
  66. * @param string $messageid Id of the message to be translated
  67. * @return string Translated message
  68. */
  69. public function translate($messageid = null)
  70. {
  71. if ($messageid === null) {
  72. return $this;
  73. }
  74. $translate = $this->getTranslator();
  75. $options = func_get_args();
  76. array_shift($options);
  77. $count = count($options);
  78. $locale = null;
  79. if ($count > 0) {
  80. if (Zend_Locale::isLocale($options[($count - 1)], null, false) !== false) {
  81. $locale = array_pop($options);
  82. }
  83. }
  84. if ((count($options) === 1) and (is_array($options[0]) === true)) {
  85. $options = $options[0];
  86. }
  87. if ($translate !== null) {
  88. $messageid = $translate->translate($messageid, $locale);
  89. }
  90. if (count($options) === 0) {
  91. return $messageid;
  92. }
  93. return vsprintf($messageid, $options);
  94. }
  95. /**
  96. * Sets a translation Adapter for translation
  97. *
  98. * @param Zend_Translate|Zend_Translate_Adapter $translate Instance of Zend_Translate
  99. * @throws Zend_View_Exception When no or a false instance was set
  100. * @return Zend_View_Helper_Translate
  101. */
  102. public function setTranslator($translate)
  103. {
  104. if ($translate instanceof Zend_Translate_Adapter) {
  105. $this->_translator = $translate;
  106. } else if ($translate instanceof Zend_Translate) {
  107. $this->_translator = $translate->getAdapter();
  108. } else {
  109. require_once 'Zend/View/Exception.php';
  110. throw new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter');
  111. }
  112. return $this;
  113. }
  114. /**
  115. * Retrieve translation object
  116. *
  117. * @return Zend_Translate_Adapter|null
  118. */
  119. public function getTranslator()
  120. {
  121. return $this->_translator;
  122. }
  123. /**
  124. * Set's an new locale for all further translations
  125. *
  126. * @param string|Zend_Locale $locale New locale to set
  127. * @throws Zend_View_Exception When no Zend_Translate instance was set
  128. * @return Zend_View_Helper_Translate
  129. */
  130. public function setLocale($locale = null)
  131. {
  132. $translate = $this->getTranslator();
  133. if ($translate === null) {
  134. require_once 'Zend/View/Exception.php';
  135. throw new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter');
  136. }
  137. $translate->setLocale($locale);
  138. return $this;
  139. }
  140. /**
  141. * Returns the set locale for translations
  142. *
  143. * @throws Zend_View_Exception When no Zend_Translate instance was set
  144. * @return string|Zend_Locale
  145. */
  146. public function getLocale()
  147. {
  148. $translate = $this->getTranslator();
  149. if ($translate === null) {
  150. require_once 'Zend/View/Exception.php';
  151. throw new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter');
  152. }
  153. return $translate->getLocale();
  154. }
  155. }