Zend_Translate with Dynamic Parameters

Pascal Opitz posted a nice little snippet; Just a quick snippet to have dynamic parameters in the underscore function, without having to write sprintf every time.

<?php
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 something like the following:

$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 little snippet solves one of the major gripes I have with the default _() function.

via Zend_Translate with dynamic parameters.

One Reply to “Zend_Translate with Dynamic Parameters”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.