using Zend Framework Language component
by Gabi SolomonToday i will continue the series of articles on the zend framework and i am going to talk about how i come to use the language component from zend.
The way i decided to go with it is by having a controller plugin that starts the language component and also detects the language.
But first thing first. We need to make the url to allow us to change the language. And to do that i added a new rule in the bootstrap file:
[php]
$route = new Zend_Controller_Router_Route(
‘:language/:controller/:action/*’,
array(
‘language’ => ‘en’,
‘module’ => ‘default’,
‘controller’ => ‘index’,
‘action’ => ‘index’
)
);
$router->addRoute(‘lang_default’, $route);
[/php]
This rule will produce urls like:
http://www.domain.com/en/controller/action
http://www.domain.com/es/controller/action
I decided to go with this type of URL instead of caching or storing the language in the session to not have google consider our pages as duplicate content. I have read that it better to go with subdomains (ex: http://en.domain.com/controller/action ) that with directories but for now i will do it this way.
Now that we are still in the bootstrapfile, we will also add our plugin:
[php]
Zend_Controller_Front::getInstance()->registerPlugin(new GSD_Controller_Plugin_Language());
[/php]
ANd now for the final touch the plugin:
[php]
/**
* Front Controller Plugin
*
* @uses Zend_Controller_Plugin_Abstract
* @category GSD
* @package GSD_Controller
* @subpackage Plugins
*/
class GSD_Controller_Plugin_Language extends Zend_Controller_Plugin_Abstract
{
public function routeShutdown(Zend_Controller_Request_Abstract $request)
{
$locale = new Zend_Locale();
$options = array('scan' => Zend_Translate::LOCALE_FILENAME);
$translate = new Zend_Translate(‘gettext’, Zend_Registry::get(‘siteRootDir’) . ‘/application/languages/’, ‘auto’, $options);
$requestParams = $this->getRequest()->getParams();
$language = (isset($requestParams['language'])) ? $requestParams['language'] : false;
if ($language == false) {
$language = ($translate->isAvailable($locale->getLanguage())) ? $locale->getLanguage() : ‘en’;
}
if (!$translate->isAvailable($language)) {
throw new Zend_Controller_Action_Exception(‘This page dont exist’,404);
} else {
$locale->setLocale($language);
$translate->setLocale($locale);
Zend_Form::setDefaultTranslator($translate);
setcookie(‘lang’, $locale->getLanguage(), null, ‘/’);
Zend_Registry::set(‘Zend_Locale’, $locale);
Zend_Registry::set(‘Zend_Translate’, $translate);
}
}
}
[/php]
A few explanations on the code.
I am using gettext adapter with auto mode … witch will basically crawl the language directory to detect what languages can you support. I also use it conjunction with Zend Locale and at the end i store the in the Registry.
Storing Zend translate in the registry will enable you to use it in the view in a very easy way using a built in helper.
Using translate in you controllers
[php]
$this->view->title = $this->view->translate(‘default-register-index-title’);
[/php]
Using translate in you views
[php]
translate(‘default-register-index-title’); ?>
[/php]
As a note i am not using the full text for translate i am using keys that have a naming conventions like “module-controller-action-message”. I find it much more easier then putting the whole string and also much more easier to maintain. Imagine if you misspelled a string, or want to modify it ? you will then have to modify it every in your code.
That is basically it.
Cheers
Update:
I have also published this plugin on phpclasses, you can find it here.


Pingback: links for 2008-12-23 « sySolution
Pingback: Gabi Solomon’s Blog: Using Zend Framework Language Component : WebNetiques
Pingback: Gabi Solomon’s Blog: Using Zend Framework Language Component : Dragonfly Networks
Pingback: Routes to support languages - Page 2 - Zend Framework Forum