Skip to content


Testing missing translations in Zend Framework

Zend_Translate is a wonderful tool to translate your page. While your backends may vary, you have to ensure you call it everywhere in your code. Unfortunately, there is no easy way to spot any untranslated strings by default. But fear not, you can easy make one yourself.

Translate uses an adapter to actually do the heavy lifting. Translate itself is just a gateway to the adapter classes, so the magic happens there. Now my solution was to just create a new adapter and have it give back a string that’s fixed every single time. So any request to the translate method would return the same string. With this you can just look on your page, see where it doesn’t display your special string and there you have your missing spots. You could set it to be an empty string, but if you use it for links, then you wont see the link anymore.

Well used, you will only see the data strings, like user names, the configured replacement string and of course any string you missed.

I use the App prefix for such very common bits of code that I can share easy between all my applications and of course with others, but there is nothing preventing you from changing this of course.

First create a file called Testing.php in the folder library/App/Translate/Adapter filled with the content below.

<?php

/** Zend_Translate_Adapter */
require_once 'Zend/Translate/Adapter.php';

/**
 * @category   App
 * @package    App_Translate_Adapter_Testing
 * @copyright  Copyright (c) 2009 Christian Riesen <chris.riesen@gmail.com>
 * @license    Public Domain
 */
class App_Translate_Adapter_Testing extends Zend_Translate_Adapter
{
    private $_data = array();
    private $_teststring = 'XtestX';

    /**
     * Generates the adapter
     *
     * @param  array               $data     Translation data
     * @param  string|Zend_Locale  $locale   OPTIONAL Locale/Language to set, identical with locale identifier,
     *                                       see Zend_Locale for more information
     * @param  array               $options  OPTIONAL Options to set
     */
    public function __construct($data, $locale = null, array $options = array())
    {
        $this->_loadTranslationData($data, $locale, $options);
    }

    /**
     * Load translation data
     *
     * @param  string|array  $data
     * @param  string        $locale  Locale/Language to add data for, identical with locale identifier,
     *                                see Zend_Locale for more information
     * @param  array         $options OPTIONAL Options to use
     * @return array
     */
    protected function _loadTranslationData($data, $locale, array $options = array())
    {
        $this->_data = array();
        if (!is_null($data)) {
        	$this->_teststring = $data;
        }
        return $this->_data;
    }

    /**
     * Translates the given string
     * returns the translation
     *
     * @param  string|array       $messageId Translation string, or Array for plural translations
     * @param  string|Zend_Locale $locale    (optional) Locale/Language to use, identical with
     *                                       locale identifier, @see Zend_Locale for more information
     * @return string
     */
    public function translate($messageId, $locale = null)
    {
        return $this->_teststring;
    }

    /**
     * returns the adapters name
     *
     * @return string
     */
    public function toString()
    {
        return "Testing";
    }
}

So now it’s ready for use. To use it you have a line that starts something like this.

$translate = new Zend_Translate('array', ...

Now this specific one would be for the array adapter. Just comment this line and add this line here.

$translate = new Zend_Translate('App_Translate_Adapter_Testing', 'XtestingX');

All the rest can stay the same as you have used it before.

Load your application and all the translated strings will have changed to XtestingX and whatever is left over points you in the right direction. I checked and could not find anything this class would break, in case you use other functions with translate or the adapter directly. Of course some classes will not return something, like a list of all translations, but they should not break, should you attempt to call them.

Of course once you tested, you have to reverse this, or you could use a configuration option or a switch to enable it. Personally, I prefer to comment it and uncomment when really needed and testing is done. Test on a none live server, or your page will become useless in an instant (or at the least very confusing).

Feel free to use it, anyway you wish. I specified it as Public Domain which I’m trying out as a license now for a while and see how feedback and experiences are with it. Yes I know that public domain is not a license per se, as it’s a free for all thing, but maybe that’s a “really good thing” ™.

Posted in Development, PHP, Zend Framework. Tagged with , .

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

You must be logged in to post a comment.