- Bootstrapping Zend_Translate with a LangSelector Plugin
- How to make POEdit detect source strings in Zend Framework
- Configuring Poedit for Zend Framework Projects
- Bootstrap Zend_Translate
As an update to the method of having everything related to Zend_Translate and Zend_Locale in the Bootstrap, here is an alternative using an Controller Plugin that does the grunt work of validating, selecting and updating 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 comment as usual if you have a neater way of doing it 🙂
Bootstrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
protected function _initTranslate()
{
// Get current registry
$registry = Zend_Registry::getInstance();
/**
* Set application wide source Locale
* This is usually your source string language;
* i.e. $this->translate('Hi I am an English String');
*/
$locale = new Zend_Locale('en_US');
/**
* Set up and load the translations (all of them!)
* resources.translate.options.disableNotices = true
* resources.translate.options.logUntranslated = true
*/
$translate = new Zend_Translate('gettext',
APPLICATION_PATH . DIRECTORY_SEPARATOR .'languages', 'auto',
array(
'disableNotices' => true, // This is a very good idea!
'logUntranslated' => false, // Change this if you debug
)
);
/**
* Both of these registry keys are magical and makes
* ZF 1.7+ do automagical things.
*/
$registry->set('Zend_Locale', $locale);
$registry->set('Zend_Translate', $translate);
return $registry;
}
}
This little plugin will check every request for a lang paramenter and act on it.
It does not matter if you set the lang parameter using a custom route :lang/:controller/:action
or via a get/post ?lang= etc. one or all of them will work.
library/App/Controller/Plugin/LangSelector.php
* @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','');
// Register all your "approved" locales below.
switch($lang) {
case "sv":
$langLocale = 'sv_SE'; break;
case "fr":
$langLocale = 'fr_FR'; break;
case "en":
$langLocale = 'en_US'; break;
default:
/**
* Get a previously set locale from session or set
* the current application wide locale (set in
* Bootstrap)if not.
*/
$langLocale = isset($session->lang) ? $session->lang : $currLocale;
}
$newLocale = new Zend_Locale();
$newLocale->setLocale($langLocale);
$registry->set('Zend_Locale', $newLocale);
$translate->setLocale($langLocale);
$session->lang = $langLocale;
// Save the modified translate back to registry
$registry->set('Zend_Translate', $translate);
}
}
Big thanks to Zend Cast for the inspiration!
Your tutorial is really very good. It helped me a lot.
But in the application.ini must stand
autoloadernamespaces[] = App;
resources.frontController.plugins.LangSelector = “App_Controller_Plugin_LangSelector”;
then it works for me. (Dann klappt es bei mir).
Folder-Struktur:
application/
docs/
library/
library/App/Controller/Plugin/
library/Zend/
…
public/
test/
…
not work !
add this : ‘scan’ => Zend_Translate::LOCALE_FILENAME
$translate = new Zend_Translate(‘gettext’,APPLICATION_PATH.DIRECTORY_SEPARATOR.’languages’,$langLocale,
array(
‘scan’ => Zend_Translate::LOCALE_FILENAME
));
how to change url default : /lang1/controller/action -> /lang2/controller/action when i click on link: and.