Hi, I have successfully translated my form element's labels and decorators by
implementing Zend_Translate (using the gettext adapter). Everything works
fine, but I was wondering if someone could tell me if there is a better way
to do this.
Here is the code snippet in my bootstrap.php that instantiates and registers
Zend_Translate object:
$translate = new Zend_Translate('gettext',
ROOT_PATH.'\languages\english.mo', 'en');
$translate->addTranslation(ROOT_PATH.'\languages\spanish.mo', 'es');
$translate->addTranslation(ROOT_PATH.'\languages\french.mo', 'fr');
$translate->setLocale('en');
Zend_Registry::set('Zend_Translate', $translate);
Here is the code snippet in my custom Form:
$this->addElement('text', 'email', array(
'label' => $this->getView()->translate('Email'),
'description' => $this->getView()->translate('Please enter valid
email'),
'required' => true,
'filters' => array('StringTrim'),
'validators' => array(
array('validator' => 'CustomEmailAddress',
'breakChainOnFailure' => true),
array('validator' => 'EmailTaken', 'breakChainOnFailure' =>
false),
)
));
Notice the call to $this->getView()->translate(). Is this the only way to
translate the label and description from my custom form?
'label' => $this->translate('Email'), gives me an error message: 'Method
translate does not exist'
'label' => $this->_view->translate('Email'), gives me a 'Fatal error: Call
to a member function translate() on a non-object ...'
For those of you using Poedit, I have correctly set the base path and added
paths to all my files requiring translation. I also entered 'translate' as
the keyword to recognize translatable strings in source file. When I open
the catalog and update it (synchronize it with the source), Poedit detects
'Email' and 'Please enter valid email' as strings to be validated regardless
of whether I use $this->getView()->translate(), $this->translate(), or
$this->_view->translate(). However, only by using
$this->getView()->translate() does the label and decorator get translated
instead of throwing errors.
I guess the reason why I am confused is that Section 21.8.1 (Initializing
I18n in Forms) of the Zend Framework Reference Guide states: "To turn on
I18n features in Zend_Form, you will need to instantiate a Zend_Translate
object with an appropriate adapter, and attach it to Zend_Form and/or
Zend_Validate." ... [The easiest way is to] "add it to the registry. All
I18n aware components of Zend Framework will autodiscover a translate object
that is in the registry under the 'Zend_Translate' key and use it to perform
translation and/or localization ... This will be picked up by Zend_Form,
Zend_Validate, and Zend_View_Helper_Translate."
Doesn't this mean that when I instantiated a Zend_Translate object and added
it to the registry under the 'Zend_Translate' key by doing the following in
my bootstrap
Zend_Registry::set('Zend_Translate', $translate);
that in my custom Form, all I had to do was this
'label' => 'Email',
'description => 'Please enter valid email',
instead of this
'label' => $this->getView()->translate('Email'),
'description' => $this->getView()->translate('Please enter valid email'),
and Poedit would detect 'Email' and 'Please enter valid email' as strings
that need to be translated?
Any help, suggestion, feedback would be greatly appreciated and help me
sleep a little better. Thanks!
--
View this message in context:
http://www.nabble.com/How-do-you-translate-form-labels-and-descriptions-using-Zend_Translate-and-Zend_Form--tp22764739p22764739.html
Sent from the Zend Framework mailing list archive at Nabble.com.