PhpRiot
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

Using Translation Adapters

The next step is to use the adapter within your code.

Example 963. Example of single-language PHP code

<?php
print "Example\n";
print 
"=======\n";
print 
"Here is line one\n";
print 
"Today is the " date("d.m.Y") . "\n";
print 
"\n";
print 
"Here is line two\n";

The example above shows some output with no support for translation. You probably write your code in your native language. Generally you need to translate not only the output, but also error and log messages.

The next step is to integrate Zend_Translate into your existing code. Of course it is much easier if you had already written your code with translation in mind, than changing your code afterwards.

Example 964. Example of multi-lingual PHP code

<?php
$translate 
= new Zend_Translate(
    array(
        
'adapter' => 'gettext',
        
'content' => '/my/path/source-de.mo',
        
'locale'  => 'de'
    
)
);
$translate->addTranslation(
    array(
        
'content' => '/path/to/translation/fr-source.mo',
        
'locale'  => 'fr'
    
)
);

print 
$translate->_("Example") . "\n";
print 
"=======\n";
print 
$translate->_("Here is line one") . "\n";
printf($translate->_("Today is the %1\$s") . "\n"date('d.m.Y'));
print 
"\n";

$translate->setLocale('fr');
print 
$translate->_("Here is line two") . "\n";

Now let's take a deeper look into what has been done and how to integrate Zend_Translate into your own code.

Create a new Zend_Translate object and define the base adapter:

<?php
$translate 
= new Zend_Translate
    
array(
        
'adapter' => 'gettext',
        
'content' => '/path/to/translation/source-de.mo',
        
'locale'  => 'de'
    
)
);

In this example we chose the Gettext Adapter. We place our file source-de.mo into the directory /path/to/translation. The gettext file will have German translation included, and we also added another language source for French.

The next step is to wrap all strings which are to be translated. The simplest approach is to have only simple strings or sentences like this:

<?php
print $translate->_("Example") . "\n";
print 
"=======\n";
print 
$translate->_("Here is line one") . "\n";

Some strings do not needed to be translated. The separating line is always a separating line, even in other languages.

Having data values integrated into a translation string is also supported through the use of embedded parameters.

<?php
printf
($translate->_("Today is the %1\$s") . "\n"date("d.m.Y"));

Instead of print(), use the printf() function and replace all parameters with %1\$s parts. The first is %1\$s, the second is %2\$s, and so on. This way a translation can be done without knowing the exact value. In our example, the date is always the actual day, but the string can be translated without the knowledge of the actual day.

Each string is identified in the translation storage by a message ID. You can use message IDs instead of strings in your code, like this:

<?php
print $translate->_(1) . "\n";
print 
"=======\n";
print 
$translate->_(2) . "\n";

But doing this has several disadvantages:

You can not see what your code should output just by viewing your code.

Also you will have problems if some strings are not translated. You must always keep in mind how translation works. First Zend_Translate checks whether the specified language has a translation for the given message ID or string. If no translation string has been found it refers to the next lower level language as defined within Zend_Locale. So "de_AT" becomes "de" only. If there is no translation found for "de" either, then the original message is returned. This way you always have an output, even in case the message translation does not exist in your message storage. Zend_Translate never throws an error or exception when translating strings.

Zend Framework