Prev | Current Page 346 | Next

Rob Allen, Nick Lo, and Steven Brown

"Zend Framework in Action"

com>
Zend Framework in Action (Ch01) Manning Publications Co. 43
we are using php files stored in the application/configuration/translations directory. The code the load the
action helper is therefore just:
$languageHelper = new Places_Controller_Action_Helper_Language($languages,
ROOT_DIR . '/application/configuration/translations');
Zend_Controller_Action_HelperBroker::addHelper($languageHelper);
The $languages array has already been loaded in listing 14.7 and we take advantage of the ROOT_DIR
define to absolutely specify the translations directory. Now that we have loaded the action helper with the
required data, we can write the init() function that loads the language strings. This is shown in listing 14.10.
Listing 14.10: Places_Controller_Action_Helper_Language::init()
public function init()
{
$lang = $this->getRequest()->getParam('lang');
if(!in_array($lang, array_keys($this->_languages))) { 1
$lang = 'en'; 1
} 1
$localeString = $this->_languages[$lang]; A
// setup translate object
$file = $this->_dir . '/'. $lang . '.php'; 2
if(file_exists($file)) 2
{ 2
include $file; 2
} else { 2
include $this->_dir . '/en.php'; 2
} 2
if(!isset($translationStrings)) {
throw new Exception('Missing $translationStrings');
}
$translate = new Zend_Translate('array', 3
$translationStrings, $lang); 3
$this->_actionController->_localeString = $localeString; C
$this->_actionController->_translate = $translate; C
$viewRenderer = Zend_Controller_Action_HelperBroker D
::getStaticHelper('viewRenderer'); D
$viewRenderer->view->localeString = $localeString; D
$viewRenderer->view->translate = $translate; D
};
1 Ensure that language chosen is allowed
A Collect the locales string from the config
2 Load translation file or English one if file missing
3 Create Zend_Translate object
C Assign to action controller
D Assign to view
The code in init() consists of as much error checking as actual code which is not uncommon.


Pages:
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
botox Kraków parasole nawierzchnie sportowe bwin Wczasy nad morzem