Bootstrap Zend_Translate

A recurring problem for site developers is implementing a solid way to create and maintain multilingual sites, this article series is my feeble attempt to guide you through how to quickly implement the Zend_Translate in an Zend Framework 1.9.x site.

The procedures and best practices for this is unfortunately like training a dog, everyone has a different way of doing it and an opinion, so the methods and code I show here are taken out of applications that are running in production so if you have a better way of doing it please feel free to comment!.

I usually use poedit a gettext editor which is available for most platforms to create my translation files, and after some initial configuration of the catalog paths so it can see your source files please see Part 1 of this article series.

The bootstrap below looks for the language specific gettext .mo files in /application/languages/ for example /application/languages/sv_SE.mo

Bootstrap.php

protected function _initTranslate() {
// We use the Swedish locale as an example
$locale = new Zend_Locale('sv_SE');
Zend_Registry::set('Zend_Locale', $locale);

// Create Session block and save the locale
$session = new Zend_Session_Namespace('session');
$langLocale = isset($session->lang) ? $session->lang : $locale;

// Set up and load the translations (all of them!)
$translate = new Zend_Translate('gettext', APPLICATION_PATH . DIRECTORY_SEPARATOR .'languages', $langLocale,
array('disableNotices' => true));

//$translate->setLocale($langLocale); // Use this if you only want to load the translation matching current locale, experiment.

// Save it for later
$registry = Zend_Registry::getInstance();
$registry->set('Zend_Translate', $translate);
}

Now when you use statements like

translate('Contact Admin'); ?>
in your layout.phtml or view.phtml files it will be picked up by poedit and you will be presented with a string “Contact Admin” to translate, in my case i’ll just enter “Kontakta Administratören”.

There is some debate on what to put in the translate strings as identifiers, I personaly prefer the actual term to translate in a base language, in this case English instead of some convoluted “IDS0001” type strings.

Poedit will keep track of changes, i.e if I would change the “Contact Admin” to “Contact us” it will tell you on synchronization that “Contact Admin” disappeared and a new translation is required for “Contact us”. It’s quite easy to send those strings to your translators.

Thats it for today.

Series Navigation<< Configuring Poedit for Zend Framework ProjectsHow to make POEdit detect source strings in Zend Framework >>

One Reply to “Bootstrap Zend_Translate”

  1. hi,

    very nice article. but I’am trying your steps with the csv-adapter but it doesn’t work.
    in my view I see no text.
    I develope under xampp on a windows machine. my csv-files are dos utf-8.
    I shortening the function like this:

    $translate = new Zend_Translate(
    array(
    ‘adapter’ => ‘csv’,
    ‘content’ => APPLICATION_PATH . ‘\languages\de.csv’,
    ‘locale’ => ‘de’,
    ‘delimiter’ => ‘;’,
    ‘disableNotices’ => false,
    ‘scan’ => Zend_Translate::LOCALE_DIRECTORY
    )
    );

    //$translate->setLocale($langLocale); // Use this if you only want to load the translation matching current locale, experiment.

    // Save it for later
    $registry = Zend_Registry::getInstance();
    $registry->set(‘Zend_Translate’, $translate);

    It would be nice if you have an idea.

    best regards
    max

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.