Zend_Translate with Dynamic Parameters

Pas­cal Opitz posted a nice lit­tle snip­pet; Just a quick snip­pet to have dynamic para­me­ters in the under­score func­tion, with­out hav­ing to write sprintf every time.


class Translate extends Zend_Translate {
public function _() {
$args = func_get_args();
$num = func_num_args();

$adapter = $this->getAdapter();

$args[0] = $adapter->_($args[0]);

if($num <= 1) {
return $args[0];
}
return call_user_func_array(‘sprintf’, $args);
}
}

Usage would be some­thing like the fol­low­ing:

$t = new Translate('array', $array_translation, $lang);
echo $t->_('My name is %s', 'Pascal');
echo $t->_('I have a %s and a %s', 'Cat', 'Horse');

Note: The above lit­tle snip­pet solves one of the major gripes I have with the default _() function.

via Zend_Translate with dynamic para­me­ters.

Tags: ,

Be the first to comment on "Zend_Translate with Dynamic Parameters"

Leave a Comment

*

Get Adobe Flash player