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

Checking for translations

Normally text will be translated without any computation. But sometimes it is necessary to know if a text is translated or not, therefor the isTranslated() method can be used.

isTranslated($messageId, $original = false, $locale = null) takes the text you want to check as its first parameter, and as optional third parameter the locale for which you want to do the check. The optional second parameter declares whether translation is fixed to the declared language or a lower set of translations can be used. If you have a text which can be returned for 'en' but not for 'en_US' you will normally get the translation returned, but by setting $original to TRUE, isTranslated() will return FALSE.

Example 956. Checking if a text is translatable

<?php
$english 
= array(
    
'message1' => 'Nachricht 1',
    
'message2' => 'Nachricht 2',
    
'message3' => 'Nachricht 3');

$translate = new Zend_Translate(
    array(
        
'adapter' => 'array',
        
'content' => $english,
        
'locale' => 'de_AT'
    
)
);

if (
$translate->isTranslated('message1')) {
    print 
"'message1' can be translated";
}

if (!(
$translate->isTranslated('message1'true'de'))) {
    print 
"'message1' can not be translated to 'de'"
        
" as it's available only in 'de_AT'";
}

if (
$translate->isTranslated('message1'false'de')) {
    print 
"'message1' can be translated in 'de_AT' as it falls back to 'de'";
}

Zend Framework