Skip to content

Commit

Permalink
Merge pull request #1 from Soflomo/feature/i18n-submenu
Browse files Browse the repository at this point in the history
Enable translators in the submenu helper
  • Loading branch information
Jurian Sluiman committed Sep 13, 2013
2 parents 3baaf27 + 5e913f0 commit fdbb147
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Common/View/Helper/SubMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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();
Expand All @@ -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)
Expand All @@ -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('<ul%s><li%s><a href="%s">%s</a>%s</li></ul>',
(null !== $this->class) ? ' class="' . $this->class . '"' : null,
($container->isActive())? ' class="active"' : null,
Expand Down

0 comments on commit fdbb147

Please sign in to comment.