PhpRiot
Follow phpriot on Twitter
Sponsored Link
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store
Free iPad/iPhone App
Available on the App Store

  • PHP manual
  • Zend Framework manual
  • Smarty manual
  • PHP articles
  • PHP training

How to log not found translations

When you have a bigger site or you are creating the translation files manually, you often have the problem that some messages are not translated. But there is an easy solution for you when you are using Zend_Translate.

You have to follow two or three simple steps. First, you have to create an instance of Zend_Log. Then you have to attach this instance to Zend_Translate. See the following example:

Example 957. Log translations

<?php
$translate 
= new Zend_Translate(
    array(
        
'adapter' => 'gettext',
        
'content' => $path,
        
'locale' => 'de'
    
)
);

// Create a log instance
$writer = new Zend_Log_Writer_Stream('/path/to/file.log');
$log    = new Zend_Log($writer);

// Attach it to the translation instance
$translate->setOptions(
    array(
        
'log'             => $log,
        
'logUntranslated' => true
    
)
);

$translate->translate('unknown string');

Now you will have a new notice in the log: Untranslated message within 'de': unknown string.

Note

You should note that any translation which can not be found will be logged. This means all translations when a user requests a language which is not supported. Also every request for a message which can not be translated will be logged. Be aware, that 100 people requesting the same translation, will result 100 logged notices.

This feature can not only be used to log messages but also to attach this untranslated messages into an empty translation file. To do so you will have to write your own log writer which writes the format you want to have and strips the prepending "Untranslated message".

You can also set the 'logMessage' option when you want to have your own log message. Use the '%message%' token for placing the messageId within your log message, and the '%locale%' token for the requested locale. See the following example for a self defined log message:

Example 958. Self defined log messages

<?php
$translate 
= new Zend_Translate(
    array(
        
'adapter' => 'gettext',
        
'content' => $path,
        
'locale'  => 'de'
    
)
);

// Create a log instance
$writer = new Zend_Log_Writer_Stream('/path/to/file.log');
$log    = new Zend_Log($writer);

// Attach it to the translation instance
$translate->setOptions(
    array(
        
'log'             => $log,
        
'logMessage'      => "Missing '%message%' within locale '%locale%'",
        
'logUntranslated' => true
    
)
);

$translate->translate('unknown string');

Additionally you are also able to change the priority which is used to write the message into the log. Per default the priority Zend_Log::NOTICE is used. It equals with 5. When you want to change the priority you can use any of Zend_Log's priorities. See the following example:

Example 959. Self defined log priority

<?php
// Create a log instance
$writer = new Zend_Log_Writer_Stream('/path/to/file.log');
$log    = new Zend_Log($writer);

$translate = new Zend_Translate(
    array(
        
'adapter' => 'gettext',
        
'content' => $path,
        
'locale'  => 'de',
        
'log'             => $log,
        
'logMessage'      => "Missing '%message%' within locale '%locale%'",
        
'logPriority'     => Zend_Log::ALERT,
        
'logUntranslated' => true
    
)
);

$translate->translate('unknown string');

Zend Framework