Question

I have a zend framework project with zend version 1.12. I´m using zend_navigation with a xml file and zend_translation with the gettext adapter.

This code creates the main menu:

echo '<ul class="nav1">';
foreach ($this->container as $page) {
    // check if it is active (not recursive)
    $isActive = $page->isActive(false);
    $liClass = $isActive ? ' class="active"' : '';
    echo '<li ' . $liClass . '>' . $this->menu()->htmlify($page);
    // subnavigation in second layer
    if (sizeof($page) > 0) {
        echo '<ul class="subNavHead">';
        foreach ($page as $subpage) {
            $isActive = $subpage->isActive(false);
            $liClass = $isActive ? ' class="active"' : '';
            echo '<li ' . $liClass . '>' . $this->menu()->htmlify($subpage) . '</li>';
        }
        echo '</ul>';
    }
    echo '</li>';
}
echo '</ul>';

And here is my language selector class:

class AW_Controller_Plugin_LangSelector extends Zend_Controller_Plugin_Abstract {
    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        $lang = $request->getParam('lang', '');

        if ($lang !== 'de' && $lang !== 'en' && $lang !== 'pl')
            $request->setParam('lang', 'de');

        switch ($request->getParam('lang')) {
            case 'de':
                $locale = 'de';
                break;
            case 'en':
                $locale = 'en';
                break;
            case 'pl':
                $locale = 'pl';
                break;
            default :
                $locale = 'de';
                break;
        }

        $zl = new Zend_Locale();
        $zl->setLocale($locale);
        Zend_Registry::set('Zend_Locale', $zl);
        $translate = Zend_Registry::get('Zend_Translate'); 
        $translate->setLocale($zl);
    }

}

When I change the language with a select box the text on my site change the language, but the navigation targets don´t change. The navigationlabels changes too.

When I am in default language : www.example.de/de/controller/action And then I switch the language to English -> the href attributes of my navigation are still on the old value (www.example.de/de/controller/action) but they should have www.example.de/en/controller/action

Where is my problem? Have I forget to re-render the menu?

Was it helpful?

Solution

I believe you are saying that the labels ('Home', 'Contact Us') are changing, but the links ('/', '/contact-us') aren't. The way Zend_Menu uses Zend_Translate only for the menu labels.

If your menu is created by MVC instead of URIs, you can make it take your language values by adding the line

$page->setParam('lang', Zend_Registry::get('Zend_Locale')->getLocale());

This would go in your navigation render, between "foreach ($page as $subpage) {" and "echo".

If your menu is using URIs, you can do a substitution on the page HREF at the same point.

Now, if I've misunderstood your question, and it's not translating links OR labels in the menu, then check and see if your menu is executing before your plugin's preDispatch.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top