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

Creating Gettext source files

Gettext source files are created by GNU's gettext library. There are several free tools available that can parse your code files and create the needed gettext source files. These have the extension *.mo and they are binary files. An open source tool for creating the files is poEdit. This tool also supports you during the translation process itself.

<?php
// We accume that we have created the mo files and translated them
$translate = new Zend_Translate(
    array(
        
'adapter' => 'gettext',
        
'content' => '/path/to/english.mo',
        
'locale'  => 'en'
    
)
);
$translate->addTranslation(
    array(
        
'content' => '/path/to/german.mo',
        
'locale' => 'de'
    
)
);

As you can see the adapters are used exactly the same way, with one small difference: change array to gettext. All other usages are exactly the same as with all other adapters. With the gettext adapter you no longer have to be aware of gettext's standard directory structure, bindtextdomain and textdomain. Just give the path and filename to the adapter.

Note

You should always use UTF-8 as source encoding. Otherwise you will have problems when using two different source encodings. E.g. one of your source files is encoded with ISO-8815-11 and another one with CP815. You can set only one encoding for your source file, so one of your languages probably will not display correctly.

UTF-8 is a portable format which supports all languages. When using UTF-8 for all languages, you will eliminate the problem of incompatible encodings.

Many gettext editors add adapter informations as empty translation string. This is the reason why empty strings are not translated when using the gettext adapter. Instead they are erased from the translation table and provided by the getAdapterInfo() method. It will return the adapter informations for all added gettext files as array using the filename as key.

<?php
// Getting the adapter informations
$translate = new Zend_Translate(
    array(
        
'adapter' => 'gettext',
        
'content' => '/path/to/english.mo',
        
'locale'  => 'en'
    
)
);
print_r($translate->getAdapterInfo());

Zend Framework