From f3ed2ebbc99f635322bd3bf0cc0320a711f089ee Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Fri, 6 Feb 2015 10:33:12 -0500
Subject: [PATCH] Revised translate view helper to use trait. - Expanded trait
 functionality to match view helper.

---
 .../I18n/Translator/TranslatorAwareTrait.php  | 13 +++++--
 .../src/VuFind/View/Helper/Root/Translate.php | 36 +++----------------
 2 files changed, 15 insertions(+), 34 deletions(-)

diff --git a/module/VuFind/src/VuFind/I18n/Translator/TranslatorAwareTrait.php b/module/VuFind/src/VuFind/I18n/Translator/TranslatorAwareTrait.php
index ec5cb822ac2..33c13cf6e24 100644
--- a/module/VuFind/src/VuFind/I18n/Translator/TranslatorAwareTrait.php
+++ b/module/VuFind/src/VuFind/I18n/Translator/TranslatorAwareTrait.php
@@ -89,16 +89,23 @@ trait TranslatorAwareTrait
     /**
      * Translate a string
      *
-     * @param string $str    String to translate
-     * @param array  $tokens Tokens to inject into the translated string
+     * @param string $str     String to translate
+     * @param array  $tokens  Tokens to inject into the translated string
+     * @param string $default Default value to use if no translation is found (null
+     * for no default).
      *
      * @return string
      */
-    public function translate($str, $tokens = array())
+    public function translate($str, $tokens = array(), $default = null)
     {
         $msg = null === $this->translator
             ? $str : $this->translator->translate($str);
 
+        // Did the translation fail to change anything?  If so, use default:
+        if (null !== $default && $msg == $str) {
+            $msg = $default;
+        }
+
         // Do we need to perform substitutions?
         if (!empty($tokens)) {
             $in = $out = array();
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Translate.php b/module/VuFind/src/VuFind/View/Helper/Root/Translate.php
index fc67e39da7d..6df6ff82ef5 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/Translate.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/Translate.php
@@ -26,8 +26,6 @@
  * @link     http://vufind.org/wiki/vufind2:developer_manual Wiki
  */
 namespace VuFind\View\Helper\Root;
-use Zend\I18n\Exception\RuntimeException,
-    Zend\I18n\View\Helper\AbstractTranslatorHelper;
 
 /**
  * Translate view helper
@@ -38,8 +36,11 @@ use Zend\I18n\Exception\RuntimeException,
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://vufind.org/wiki/vufind2:developer_manual Wiki
  */
-class Translate extends AbstractTranslatorHelper
+class Translate extends \Zend\View\Helper\AbstractHelper
+    implements \VuFind\I18n\Translator\TranslatorAwareInterface
 {
+    use \VuFind\I18n\Translator\TranslatorAwareTrait;
+
     /**
      * Translate a string
      *
@@ -52,33 +53,6 @@ class Translate extends AbstractTranslatorHelper
      */
     public function __invoke($str, $tokens = array(), $default = null)
     {
-        try {
-            $translator = $this->getTranslator();
-            if (!is_object($translator)) {
-                throw new RuntimeException();
-            }
-            $msg = $translator->translate($str);
-        } catch (RuntimeException $e) {
-            // If we get called before the translator is set up, it will throw an
-            // exception, but we should still try to display some text!
-            $msg = $str;
-        }
-
-        // Did the translation fail to change anything?  If so, use default:
-        if (!is_null($default) && $msg == $str) {
-            $msg = $default;
-        }
-
-        // Do we need to perform substitutions?
-        if (!empty($tokens)) {
-            $in = $out = array();
-            foreach ($tokens as $key => $value) {
-                $in[] = $key;
-                $out[] = $value;
-            }
-            $msg = str_replace($in, $out, $msg);
-        }
-
-        return $msg;
+        return $this->translate($str, $tokens, $default);
     }
 }
\ No newline at end of file
-- 
GitLab