Latest: Genstatic, my first sip of coffee

Content with Style

Web Technique

Zend_Translate with dynamic parameters

by Pascal Opitz on March 13 2009, 11:46

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');

Comments

  • I had no idea you had a horse. Where does it live, in the fridge? More on subject: any reason you did the func_num_args() and not a simple count()?

    by Matthias Willerich on March 13 2009, 20:01 - #

  • Nope, no particular reason.

    by Pascal Opitz on March 18 2009, 15:36 - #

  • How funny,

    I really need to implement this system-wide in the you-know-what application... almost two years later...

    by Matthias Willerich on February 21 2011, 13:23 - #


Comments for this article are closed.