/library/View/Helper/Translate.php

https://github.com/kervin/kyzstudio · PHP · 180 lines · 80 code · 19 blank · 81 comment · 15 complexity · 7fe912819597b092025458d998bf0444 MD5 · raw file

  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Translate.php 20140 2010-01-08 05:21:04Z thomas $
  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-2010 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. $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|Zend_View_Helper_Translate 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. $e = new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter');
  105. $e->setView($this->view);
  106. throw $e;
  107. }
  108. return $this;
  109. }
  110. /**
  111. * Retrieve translation object
  112. *
  113. * @return Zend_Translate_Adapter|null
  114. */
  115. public function getTranslator()
  116. {
  117. if ($this->_translator === null) {
  118. #require_once 'Zend/Registry.php';
  119. if (Zend_Registry::isRegistered('Zend_Translate')) {
  120. $this->setTranslator(Zend_Registry::get('Zend_Translate'));
  121. }
  122. }
  123. return $this->_translator;
  124. }
  125. /**
  126. * Set's an new locale for all further translations
  127. *
  128. * @param string|Zend_Locale $locale New locale to set
  129. * @throws Zend_View_Exception When no Zend_Translate instance was set
  130. * @return Zend_View_Helper_Translate
  131. */
  132. public function setLocale($locale = null)
  133. {
  134. $translate = $this->getTranslator();
  135. if ($translate === null) {
  136. #require_once 'Zend/View/Exception.php';
  137. $e = new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter');
  138. $e->setView($this->view);
  139. throw $e;
  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. $e = new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter');
  156. $e->setView($this->view);
  157. throw $e;
  158. }
  159. return $translate->getLocale();
  160. }
  161. }