In case you are wondering how to quickly translate text using Zend_Translate and variable numbers of placeholders here is a quick solution that uses built-in PHP funtionality: vsprintf to the rescue!
First setup Zend_Translate to your needs, e.g. in Bootstrap:
protected function _initTranslations() {
$translate = new Zend_Translate(array(
'adapter' => 'gettext',
'content' => APPLICATION_PATH . DIRECTORY_SEPARATOR . 'languages',
'scan' => Zend_Translate::LOCALE_DIRECTORY,
'tag' => 'YourProject_Translation',
'disableNotices' => true));
Zend_Registry::set('Zend_Translate', $translate);
Zend_Translate::setCache(Zend_Registry::get('Zend_Cache'));
Zend_Form::setDefaultTranslator($translate);
}
This configuration uses a gettext to parse translation files. Then in order to translate text with placeholders simply use vsprintf after the actual translation has been done by Zend_Translate to substitute any placeholders with their actual values:
echo vsprintf(Zend_Registry::get('Zend_Translate')
->translate('Your message with 2 placeholders: The %s and the %s.'),
array('first', 'second'));
Which will produce “Your message with 2 placeholders: The first and the second.”
Leave a Reply