Implementing Zend_Locale and Zend_Translate with modules

273 views Asked by At

I've searched the internet for a good solution to integrating Zend_Locale and Zend_Translate into a modular structure. Here's the endpath I want:

http://url/:lang/:module/:controller/:action
http://url/:lang/:controller/:action
http://url/:module/:controller/:action <= should resort to default locale

I've seen lots of tutorials utilizing routers, but none seem to work for me. Can someone please just solve this for me.

Thanks!

1

There are 1 answers

0
bedeabza On

The first issue here is that when using routes you need to have very specific constraints in order to match rules with that many variables.

I suggest instead of accepting both //url/:lang/:module/:controller/:action and //url/:module/:controller/:action settle to use only the first structure. This way it will be easier to isolate the first rule from this one //url/:lang/:controller/:action, having only 2 rules each of them with different "words" (URL parts).

$withModule = new Zend_Controller_Router_Route(
    ':lang/:module/:controller/:action',
    array()
);

$withoutModule = new Zend_Controller_Router_Route(
    ':lang/:controller/:action',
    array(
        'module' => 'default'
    )
);

$router->addRoute('withModule', $withModule);
$router->addRoute('withoutModule', $withoutModule);

In the first route you don't specify any defaults so it doesn't match URLs intended for the second, while in the second route you default the module (because ZF needs that info).

As for the third rule, I suggest using a base ancestor controller, something like MyLib_Controller and in its init() method verify if a language param is received like in the example below:

if(!$this->_getParam('lang')) {
    //this should cover the 3rd use case
    $this->_redirect('/en/' + $this->view->url(), array('exit' => true, 'prependBase' => false));
} else {
    //setup Zend_Translate
}

Another possibility is restricting the :lang variable to only 2 letter words, but that would generate problems and I prefer to avoid it.