You might want to have a look at the view helper that already exists Zend Framework: Documentation
I've setup everything and it works, but I have some questions left.
My languages are in a folder /languages/LANG/resources.ini. It's an ini file (I'm normally a Java developer), which is a bit easier to work with. (It doesn't matter that much either).
What I do now is this:
Controller:
Page:Code:$this->view->trans = $translate;
Problems with this:Code:<?php echo $this->trans->_('register.form.firstname') ?>
You have to repeat it in every Controller-method. You can do it in 1 method, but still it's quite tedious. And is it possible to shorten the command, to echo the text from the ini file?
Is it possible to create those translations somewhere central (base action or controller), so all pages and controllers can access it?
You might want to have a look at the view helper that already exists Zend Framework: Documentation
Brenton Alker
PHP Developer - Brisbane, Australia
blog.tekerson.com | twitter.com/tekerson | brenton.mp
Hi.
I'm managing with the same issue so
...
I set up this code
[PHP]
class Zend_View_Helper_I18n {
public function i18n($key)
{
$fc = Zend_Controller_Front::getInstance();
$lang= $fc->getRequest()->getParam('lang');
//include file
$adapter = new Zend_Translate('array', array('simple' => 'einfach'), 'de');
$translate = new Zend_View_Helper_Translate($adapter);
return $translate->translate($key);
}
}
[/PHP]
or in the bootstrap (In this case I'm stuck
How can I catch the lang and manage the include)
but imho a better way you set an only instance of Zend_Translate / Zend_View_Helper_Translate
[PHP]
protected function _initDoctype()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->doctype('XHTML1_STRICT');
$adapter = new Zend_Translate('array', array('simple' => 'einfach'), 'de');
$translate = new Zend_View_Helper_Translate($adapter);
$view->prova= "prova";
$view->i18n= $translate;
}
[/PHP]
Just looking for advice.
Bye.
[EDIT]
[PHP]
class Zend_View_Helper_I18n {
protected $adapter= null;
protected $translate= null;
public function i18n($key)
{
if($this->adapter === null && $this->translate === null){
$fc = Zend_Controller_Front::getInstance();
$lang= $fc->getRequest()->getParam('lang');
//include file
$this->adapter = new Zend_Translate('array', array('simple' => 'einfach'), 'de');
$this->translate = new Zend_View_Helper_Translate($this->adapter);
}
return $this->translate->translate($key);
}
}
[/PHP]
Far better
Imho a good and quick way to use Zend_Translate.
Last edited by whisher; 06-13-2009 at 07:06 PM.
Nvm, see post below
Last edited by Bjorn121; 07-11-2009 at 01:38 PM.
Is it necessary to register this in EVERY controller? I added this to my Index controller:
If you add it to Zend registry, one might think that that's sufficiƫnt. Apparently notCode:$translate = new Zend_Translate('ini', '../app_frontend/languages/nl/resources.ini', 'auto', array('scan' => Zend_Translate::LOCALE_FILENAME)); $translate->addTranslation('../app_frontend/languages/en/resources.ini', 'en'); $translate->addTranslation('../app_frontend/languages/fr/resources.ini', 'fr'); Zend_Registry::set('Zend_Translate', $translate);. When I access my register page (which is handled by RegisterController), the texts appear as keys, not as translations.
Is there a way of adding it to 1 central place and make sure that all pages can access the translate object? Because I think it's rather tedious to add it to every controller.
Since you're adding it to the global registry anyway, you can do this in your bootstrap.
Either in a bootstrap _initTranslate() (or similar) method for >1.8 or in your index.php for <1.8.
Brenton Alker
PHP Developer - Brisbane, Australia
blog.tekerson.com | twitter.com/tekerson | brenton.mp
First:
Initiate Zend_Translate in bootstrap and add it to the registry using the key 'Zend_Translate'.
Second:
When Zend_Translate is within the registry then there is no need to initiate the translate view helper... all done automatically.
@Wisher:
Your code is a big overhead. Use bootstrap and add to registry. No need for view helper. It fetches from registry automatically.
Also it is no good way to overwrite the existing view helper. Own classes should extend existing classes and not overwrite them. They should also not use the Zend namespace.
@Bjorn:
Look at the manual... there is no need to use scanning when you don't scan. Also there is no need to say locale is in the filename when you add the files manually and there is no locale in the filename.