How to make POEdit detect source strings in Zend Framework

This entry is part 3 of 4 in the series Work­ing with Zend_Translate and Poedit

You will notice that once you have started trans­lat­ing an appli­ca­tion using poedit it’s quite a smooth process, what ham­pers the expe­ri­ence a lit­tle bit is the muti­tude of ways you can write code in Zend Frame­work, this is great in every way for devel­op­ers, but requires a bit of think­ing when you need to also trans­late all the UI strings.
So how do we make poedit detect the strings while mak­ing our code pretty?

(And guys PLEASE com­ment on this arti­cle with your own hints, tips & quirks!)

For exam­ple;
This is a sim­ple login form using Zend_Form;
Zend_Form is com­pletely Zend_Translate i18n com­pat­able, and will read in all strings from your trans­la­tion sources with­out the use of spe­cific $this->translate() calls, which makes the code ALOT pret­tier, see below;


class Form_Login extends Zend_Form {
public function init() {
$this->setAttrib('id', 'LoginForm');
$this->addElement('text', 'username', array(
'label' => $this->getView()->translate('Username'),
'description' => $this->getView()->translate('Please enter valid username'),
));
$this->addElement('password', 'password', array(
'label' => $this->getView()->translate('Password'),
'description' => $this->getView()->translate('Please enter valid password'),
));
$this->addElement('submit', 'submit', array(
'label' => $this->getView()->translate('Login'),
));
}
}

Is the same thing as; but a LOT shorter and cleaner!

class Form_Login extends Zend_Form {
public function init() {
$this->setAttrib('id', 'LoginForm');
$this->addElement('text', 'username', array(
'label' => _('Username'),
'description' => _('Please enter valid username'),
));
$this->addElement('password', 'password', array(
'label' => _('Password'),
'description' => _('Please enter valid password'),
));
$this->addElement('submit', 'submit', array(
'label' => _('Login'),
));
}
}

Both are caught by the poedit key­words scan­ner that looks for trans­late() as well as the trans­late helper shot­code _().

Some notes (And I’ll keep this sec­tion updated while I find more tips & hints and quirks!)

But! It always seem to be a But in there;
These two are NOT the same!
translate("Welcome %s, your last login was %s",$this->user['name'],$this->user['active']); ?>
user['name'],$this->user['active']); ?>
The sec­ond one explodes with; “Warn­ing: _() expects exactly 1 para­me­ter, 3 given”

That tells us that;

or even (if you have short tag mode on;

should be ok to use. except that it’s not pro­duc­ing the trans­lated out­put in an view.phtml or layout.phtml. so we are forced to use;
translate('Logout'); ?>

Series Nav­i­ga­tionBoot­strap Zend_TranslateBoot­strap­ping Zend_Translate with a LangS­e­lec­tor Plugin

Tags: , , , ,

10 Responses to "How to make POEdit detect source strings in Zend Framework"

  • Ithier says:
  • Ithier says:
  • Wojtek says:
  • Wojtek says:
  • singles says:
  • Jochen Bünnagel says:
  • Victor says:
Leave a Comment

*

Get Adobe Flash playerPlugin by wpburn.com wordpress themes