Zend Navigation setting language parameter for route doesnt reflect in app

563 views Asked by At

I have a zend xml config like so:

<design>
    <navigation>
        <frontend>
            <company>
                <label>Company</label>                  
                <route>sitepage</route>                                 
                <pages>
                    <about>
                        <label>About us</label>
                        <route>sitepage</route>
                        <params>
                            <page>about-us</page>
                            <language>en</language>
                        </params>                                                       
                    </about>

Here is my sitepage route:

resources.router.routes.sitepage.type = Zend_Controller_Router_Route
resources.router.routes.sitepage.route = ":language/page/:page"
resources.router.routes.sitepage.defaults.module ="core"
resources.router.routes.sitepage.defaults.controller = "page"
resources.router.routes.sitepage.defaults.action = "view"
resources.router.routes.sitepage.defaults.page = "home"
resources.router.routes.sitepage.defaults.language = "en"

As you can see, what I do is set the page param within the <params> xml node. I tried adding the <language> parameter thinking it would automatically update to the application language, but it doesnt seem to work that way. My navigation menu just outputs, for example, http://localhost/en/page/about-us when It should be http://localhost/it/page/about-us (given that my application is registered as using the it language). How can I get my navigation to recognize the application language (it) ?

1

There are 1 answers

0
golbarg On

I worked this out a different way...

Here is an example for a main registration page:

routes.register_index.route = @lang/@register
routes.register_index.defaults.controller = register
routes.register_index.defaults.action = index

My navigation.xml is empty but you could use just a label and route.

You may have noticed @ instead of : in my routes. This is designed mostly for i18n sites.

In my language folder, I have the regular en.php, etc. AND url-en.php, etc.

Here is what it looks like:

<?php
return array(
    'lang' => 'en',
    'register' => 'register',
    'about' => 'about-us',
);

With this, the same route will be used for /en/register, /fr/inscription, etc. (one for each language file)

In my Bootstrap, I include these with the Rewrite initiation:

protected function _initRewrite()
    {
        $translator = new Zend_Translate('array', APPLICATION_PATH . '/language/url-fr.php', 'fr');
        $translator->addTranslation(APPLICATION_PATH . '/language/url-en.php', 'en');

        // Set the current locale for the translator
        $locale = Zend_Registry::get('Zend_Locale');
        $translator->setLocale($locale);

        // Set it as default translator for routes
        Zend_Controller_Router_Route::setDefaultTranslator($translator);

        $front_controller = Zend_Controller_Front::getInstance();
        $router = $front_controller->getRouter();

        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', APPLICATION_ENV);
        $router->addConfig($config, 'routes');

        $router->addDefaultRoutes();
    }

This basically tells your application to translate @lang by its value from url-lang.php according to the current locale. Locale has to be set before this point in your bootstrap. You can either retrieve it from session, cookie, URL, anything.

Once this is done, you can call your route anywhere and it'll be in the current locale, using data from the special language files. Make sure your nav file has the same names as your routes and you should be fine.