Search code examples
phpzend-frameworklocalizationzend-translate

Zend Translate /locale/set/en was not found on this server


I'm new to zend framework and i'm finding the obvious problem you meet when you use it for the first time. Now i'm setting un the translate function, and, in order to cange the language of the website, i've placed two flags, one for the language of the site and the other with the english flag. It should work this way, i've built up a specific controller:

 <?php

 class LocaleController extends Zend_Controller_Action
 {

     public function init()
     {
         // init code       
     }

     public function indexAction()
     {
    // action body
     }

     public function setAction()
     {
    // if supported locale, add to session
    if (Zend_Validate::is($this->getRequest()->getParam('locale'), 'InArray',
            array('haystack' => array('en', 'it'))))
    {
        $session = new Zend_Session_Namespace('ttb.l10n');
        $session->locale = $this->getRequest()->getParam('locale');
    }
    // redirect to requesting URL
    $url = $this->getRequest()->getServer('HTTP_REFERER');
    $this->_redirect($url);
     }


 }

In the locale controller,I've written the "set" action which takes the "en" and "it" parameters. (I've set the Bootstrap file to make the translation, i'll paste it below. But the page is not found. Should i write a view for this controller? Can't i simply load the index page with the wanted language? how do i achieve that?

    protected function _initLocale()
{
 $session = new Zend_Session_Namespace('ttb.l10n');
 if ($session->locale) 
 {
    $locale = new Zend_Locale($session->locale);
 }
 if ($locale === null) 
 {
    try 
    {
        $locale = new Zend_Locale('browser');
    } 
    catch (Zend_Locale_Exception $e) 
    {
      $locale = new Zend_Locale('en');
    }
 }
 $registry = Zend_Registry::getInstance();
 $registry->set('Zend_Locale', $locale);
}

protected function _initTranslate()
{
    $translate = new Zend_Translate('array',
            APPLICATION_PATH . '/../languages/',
            null,
            array('scan' => Zend_Translate::LOCALE_FILENAME,
                    'disableNotices' => 1));
    $registry = Zend_Registry::getInstance();
    $registry->set('Zend_Translate', $translate);
}

Then in the indexController.php file i have:

    public function init()
{
    /* Initialize action controller here */
    $registry = Zend_Registry::getInstance();
    $this->view->locale = $registry->get('Zend_Locale');
}

Then in the layout.phtml i have the links:

        <a href="/locale/set/it"><img src="<?php echo $this->baseUrl(); ?>/immagini/flag/italia.png" title="Italiano"></a>      
        <a href="/locale/set/en"><img src="<?php echo $this->baseUrl(); ?>/immagini/flag/inghilterra.png" title="English"></a>

I'm using zend framework 1.12...


Solution

  • I just had to change the links to the following:

            <a href="<?php echo $this->baseUrl(); ?>/locale/set/it"><img src="<?php echo $this->baseUrl(); ?>/immagini/flag/italia.png" title="Italiano"></a>      
            <a href="<?php echo $this->baseUrl(); ?>/locale/set/en"><img src="<?php echo $this->baseUrl(); ?>/immagini/flag/inghilterra.png" title="English"></a>