Proper handling of untranslated messages
- Posted by thomasw at 12:04:30 // //
- Announcements, I18N, Translate
In past the handling of untranslated messages within Zend_Translate was somewhat unhandy.
You had to check if the message can be translated or not, and then you had to notify yourself somehow. This leaded mainly to self written view helpers or an own class as middleware which did this job for you.
So I began thinking and found a really nice solution for you guys which was integrated yesterday.
Think of a site with a huge amount of pages. For simplification you have created the translation sources yourself. You choosed cvs for example. Now your site grows from day to day. Several people are working on it. Of course, depending on the size of the site and how pedantic your helpers are, you would have forgotten to add some messages to the translation files.
For the visitors of your site it seems like you’ve overlooked things and the site seems unfinished and unprofessional because languages are mixed.
There is now a solution to this problem:
Logging
You can define that Zend_Translate should write messages which can not be translated into a log. This does even work with your existing code. You just have to add 3 lines. Let’s get into practice:
First, create a log instance:
$writer = new Zend_Log_Writer_Stream('/path/mylog.log');
$log = new Zend_Log($writer);
Then attach it to Zend_Translate:
$t = new Zend_Translate('csv', $path);
$t->setOptions(
array('log' => $log,
'logUntranslated' => true));
Additionally we activated logging for messages with the option logUntranslated.
That’s it. From now on, when a message has to be translated and there is no translation found, you will have a log entry which reads Untranslated message: My requested message. Now all you have to do is to check your log regulary and look if there are any untranslated messages.
Better than before, but with this feature you can do even more.
Now you have to add this new translations manually to a translation source file. But Zend_Log could do this for you when you want to invest some lines of code.
All you have to do is to write your own extension of a Zend_Log_Writer. Your Log_Writer instance has to handle the source format you want to use. Strip the leading “Untranslated message: ” from the thrown notice and then write the new message to the file.
As short example:
My_Csv_Log_Writer extends Zend_Log_Writer_Stream
{
protected function <em>write($event)
{
$event = substr($event, 22);
return parent::</em>write($event . ";\n");
}
}
This just strips out the leading untranslated stuff and add a separator and a new line to the file.
Of course, for a real working implementation this is not enough.
You would have to create a log writer for the source format you want to use. But it should show you how simple a new log writer can be.
As always this new feature is available with the next release which is ZF 1.8 or when you use trunk.
I hope you love this new feature. ;-)
Greetings
Thomas Weidner
I18N Team Leader, Zend Framework
Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework
