Integrating ZendTranslate with Your Project
There are many ways to integrate Zend_Translate with your project, but I prefer to create application resources for any kind of generalized configuration like this. This enables you to reuse the resource in any ZF project you work on.
To get started, create a new folder in the application folder named lang. Then create two files in this folder: source-en.csv and source-es.csv.
■ Note It is not technically necessary to create a language file for your default language. If the translate helper does not locate a translation, it simply renders the text you pass it. I prefer to create one nonetheless because this file serves as a template for people who want to translate the application into their language of choice.
The next step is to update your application.ini configuration file, adding the adapter you want to use and the paths to your language files, as shown in Listing 11-15.
Listing 11-15. Adding the Translate Settings to application/configs/application.ini resources.translate.adapter = csv resources.translate.default.locale = "en_US"
resources.translate.default.file = APPLICATION_PATH "/lang/source-en.csv" resources.translate.translation.es = APPLICATION_PATH "/lang/source-es.csv"
Now create a new file in library/CMS/Application/Resource named Translate.php. Create a new class named CMS_Application_Resource_Translate that extends Zend_Application_Resource_ResourceAbstract.
The translate application resource needs to create an instance of Zend_Translate, adding each of the translation files with their appropriate locales. Then it registers the Zend_Translate instance in Zend_Registry so the rest of the application can access it, as shown in Listing 11-16.
Listing 11-16. The Translation Application Resource in library/CMS/Application/Resource/Translate.php
<?php class CMS_Application_Resource_Translate extends Zend_Application_Resource_ResourceAbstract {
public function init ()
$options = $this->getOptions(); $adapter = $options['adapter']; $defaultTranslation = $options['default']['file']; $defaultLocale = $options['default']['locale'];
$translate = new Zend_Translate($adapter, $defaultTranslation, $defaultLocale); foreach ($options['translation'] as $locale => $translation) { $translate->addTranslation($translation, $locale);
Zend_Registry::set('Zend_Translate', $translate); return $translate;
Once this resource is configured, you are ready to start translating your interface. Translating the front end is a very straightforward task; Zend_View has a translate() helper, which automatically fetches the Zend_Translate instance from the registry. You simply pass the helper the string you want translated.
In this example, I will just translate the headline on the search box; you can follow this lead and translate the rest of the application.
Update application/layouts/scripts/layout.phtml, replacing the search box headline with the code in Listing 11-17.
Listing 11-17. Translating the Search Box Headline in application/layouts/scripts/layout.phtml
<div id='searchForm'>
<h2><?php echo $this->translate('Search Site');?></h2> <?php
$searchForm = new Form_SearchForm(); $searchForm->setAction('/search'); echo $searchForm->render();
Next you need to add Search Site to the language files, as shown in Listings 11-18 and 11-19.
Listing 11-18. The Translated Search Headline in application/lang/source-en.csv "Search Site"; "Search Site"
Listing 11-19. The Translated Search Headline in application/lang/source-es.csv
"Search Site"; "Busca Sitio"
Now your CMS will render this text in the proper language, which Zend_Translate will determine based on the headers that the browser passes it.
Post a comment