From 5e913f024ef80799ac191b7c7d852056850b007e Mon Sep 17 00:00:00 2001 From: Jurian Sluiman Date: Fri, 13 Sep 2013 13:55:02 +0200 Subject: [PATCH] Enable translators in the submenu helper --- src/Common/View/Helper/SubMenu.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Common/View/Helper/SubMenu.php b/src/Common/View/Helper/SubMenu.php index ce60b15..4b153b8 100644 --- a/src/Common/View/Helper/SubMenu.php +++ b/src/Common/View/Helper/SubMenu.php @@ -41,12 +41,15 @@ namespace Soflomo\Common\View\Helper; use Zend\View\Helper\AbstractHelper; +use Zend\I18n\Translator\Translator; class SubMenu extends AbstractHelper { protected $level = 0; protected $class; protected $header; + protected $translator; + protected $textdomain; public function __invoke() { @@ -71,6 +74,13 @@ public function setHeader($header) return $this; } + public function setTranslator(Translator $translator, $textdomain = 'default') + { + $this->translator = $translator; + $this->textdomain = $textdomain; + return $this; + } + public function __toString() { $view = $this->getView(); @@ -97,6 +107,15 @@ public function __toString() $flag = $container->isVisible(); $container->setVisible(true); + // Inject translator + $enabled = $menu->isTranslatorEnabled(); + $translator = $menu->getTranslator(); + $textdomain = $menu->getTranslatorTextDomain(); + if ($this->translator) { + $menu->setTranslator($this->translator, $this->textdomain); + $menu->setTranslatorEnabled(true); + } + $html = $menu->setContainer($container) ->setUlClass('') ->setOnlyActiveBranch(false) @@ -108,12 +127,19 @@ public function __toString() $container->setVisible($flag); // Reset the container $menu->setContainer($current); + // Reset translator + $menu->setTranslatorEnabled($enabled); + $menu->setTranslator($translator, $textdomain); if (!strlen($html)) { return ''; } $label = $this->header ?: $container->getLabel(); + if ($this->translator) { + $label = $this->translator->translate($label, $this->textdomain); + } + return sprintf('%s%s', (null !== $this->class) ? ' class="' . $this->class . '"' : null, ($container->isActive())? ' class="active"' : null,