Issues

ZF-3173: Integrate Zend_Translate with Zend_View_Helper_HeadTitle?

Description

It seemed to make sense to me that the HeadTitle helper should be doing translation when it's cast to string, and it was pretty trivial to implement. Thoughts?


Index: HeadTitle.php
===================================================================
--- HeadTitle.php   (revision 9303)
+++ HeadTitle.php   (working copy)
@@ -19,6 +19,9 @@
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  */
 
+/** Zend_Registry */
+require_once 'Zend/Registry.php';
+
 /** Zend_View_Helper_Placeholder_Container_Standalone */
 require_once 'Zend/View/Helper/Placeholder/Container/Standalone.php';
 
@@ -65,18 +68,28 @@
     /**
      * Turn helper into string
      * 
-     * @param  string|null $indent 
+     * @param  string|null $indent
+     * @param  string|null $locale
      * @return string
      */
-    public function toString($indent = null)
+    public function toString($indent = null, $locale = null)
     {
         $indent = (null !== $indent)
                 ? $this->getWhitespace($indent)
                 : $this->getIndent();
 
         $items = array();
-        foreach ($this as $item) {
-            $items[] = $this->_escape($item);
+
+        if(Zend_Registry::isRegistered('Zend_Translate')) {
+            $translator = Zend_Registry::get('Zend_Translate');
+
+            foreach ($this as $item) {
+                $items[] = $this->_escape($translator->translate($item, $locale));
+            }
+        } else {
+            foreach ($this as $item) {
+                $items[] = $this->_escape($item);
+            }
         }
 
         $separator = $this->_escape($this->getSeparator());

Comments

I can see a benefit to this, definitely. However, I can also see this leading to unexpected results when people are unaware that it translates.

I think a better solution would be to have an accessor via which you can enable or disable translation:


$this->headTitle()->enableTranslation();

// optionally, even set the specific translation object:
$this->headTitle()->setTranslator(...);

I'm going to schedule this for the next minor release.

Here's a patch that should be doing things more properly. Sorry about the lazy patch before, I am a lazy lazy man. :) One last thing I am wondering... would it be possible (or wise) to try pulling the translator from a registered Zend_View_Helper_Translate object in addition to the Zend_Registry key?

(Replacement patch) Or, we could try not requiring Zend/Registry.php twice. Sigh. Sloppy copy & paste on my part.

No this would not be wise, because the official registry key to use for the translation adapter within all other components is "Zend_Translate"... ;-) See the Zend_View_Helper_Translate helper for details.

Assigning to Jon W.

This was done with r12537

Changing issues in preparation for the 1.7.0 release.