Bootstrapping Zend_Translate with a LangSelector Plugin

This entry is part 4 of 4 in the series Work­ing with Zend_Translate and Poedit

As an update to the method of hav­ing every­thing related to Zend_Translate and Zend_Locale in the Boot­strap, here is an alter­na­tive using an Con­troller Plu­gin that does the grunt work of val­i­dat­ing, select­ing and updat­ing the Zend_Locale, Zend_Registry & Zend_Session using Zend_Session_Namespace. And we are using poedit .po & .mo files as the source as usual.

Please com­ment as usual if you have a neater way of doing it :-)

Bootstrap.php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

pro­tected func­tion _initTranslate()
{
// Get cur­rent reg­istry
$reg­istry = Zend_Registry::getInstance();
/**
* Set appli­ca­tion wide source Locale
* This is usu­ally your source string lan­guage;
* i.e. $this->translate(‘Hi I am an Eng­lish String’);
*/
$locale = new Zend_Locale(‘en_US’);

/**
* Set up and load the trans­la­tions (all of them!)
* resources.translate.options.disableNotices = true
* resources.translate.options.logUntranslated = true
*/
$trans­late = new Zend_Translate(‘gettext’,
APPLICATION_PATH . DIRECTORY_SEPARATOR .‘lan­guages’, ‘auto’,
array(
’dis­ableNo­tices’ => true, // This is a very good idea!
’logUn­trans­lated’ => false, // Change this if you debug
)
);
/**
* Both of these reg­istry keys are mag­i­cal and makes
* ZF 1.7+ do automag­i­cal things.
*/
$registry->set(‘Zend_Locale’, $locale);
$registry->set(‘Zend_Translate’, $trans­late);
return $reg­istry;
}
}

This lit­tle plu­gin will check every request for a lang para­menter and act on it.
It does not mat­ter if you set the lang para­me­ter using a cus­tom route :lang/:controller/:action
or via a get/post ?lang= etc. one or all of them will work.

library/App/Controller/Plugin/LangSelector.php

/**
* @author Danny Froberg
* @name App_Controller_Plugin_LangSelector
* @filesource library/App/Controller/Plugin/LangSelector.php
* @tutorial Instantiate in application.ini with;
* resources.frontController.plugins.LangSelector =
* "App_Controller_Plugin_LangSelector"
* @desc Takes the lang parameneter when set either via a
* route or get/post and switches Locale, This depends
* on the main initTranslate function in Bootstrap.php
* to set the initial Zend_Translate object.
* Inspiration from ZendCasts LangSelector.
*/
class App_Controller_Plugin_LangSelector extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$registry = Zend_Registry::getInstance();
// Get our translate object from registry.
$translate = $registry->get('Zend_Translate');
$currLocale = $translate->getLocale();
// Create Session block and save the locale
$session = new Zend_Session_Namespace('session');

$lang = $request->getParam(‘lang’,”);
// Reg­is­ter all your “approved” locales below.
switch($lang) {
case “sv”:
$lan­gLo­cale = ‘sv_SE’; break;
case “fr”:
$lan­gLo­cale = ‘fr_FR’; break;
case “en”:
$lan­gLo­cale = ‘en_US’; break;
default:
/**
* Get a pre­vi­ously set locale from ses­sion or set
* the cur­rent appli­ca­tion wide locale (set in
* Bootstrap)if not.
*/
$lan­gLo­cale = isset($session->lang) ? $session->lang : $cur­rLocale;
}

$newLo­cale = new Zend_Locale();
$newLocale->setLocale($langLocale);
$registry->set(‘Zend_Locale’, $newLocale);

$translate->setLocale($langLocale);
$session->lang = $langLocale;

// Save the mod­i­fied trans­late back to reg­istry
$registry->set(‘Zend_Translate’, $trans­late);
}
}

Big thanks to Zend Cast for the inspi­ra­tion!

Series Nav­i­ga­tionHow to make POEdit detect source strings in Zend Framework

Tags: , , , , , , ,

2 Responses to "Bootstrapping Zend_Translate with a LangSelector Plugin"

  • Fuzzy says:
  • web agency says:
Leave a Comment

*

Get Adobe Flash playerPlugin by wpburn.com wordpress themes