murze
murze

Reputation: 4103

Log translated strings when using Zend_Translate

With Zend Framework it's easy to log strings that haven't got a translation.

My question: how do you log the strings that do have a translation?

Thanks!!

Upvotes: 1

Views: 361

Answers (1)

webdevbyjoss
webdevbyjoss

Reputation: 524

There is no ability to log translate() calls in Zend_Translate but you can create your own helper that will proxify all calls to original translate() helper and use it for your needs.

Here is some example of helper method:

/**
 * Translates provided message Id
 * 
 * You can give multiple params or an array of params.
 * If you want to output another locale just set it as last single parameter
 * Example 1: translate('%1\$s + %2\$s', $value1, $value2, $locale);
 * Example 2: translate('%1\$s + %2\$s', array($value1, $value2), $locale);
 *
 * @param  string $messageid Id of the message to be translated
 * @return string Translated message
 */
public function t_($messageid = null)
{
    /**
     * Process the arguments
     */
    $options = func_get_args();

    array_shift($options);

    $count  = count($options);
    $locale = null;
    if ($count > 0) {
        if (Zend_Locale::isLocale($options[($count - 1)], null, false) !== false) {
            $locale = array_pop($options);
        }
    }

    if ((count($options) === 1) and (is_array($options[0]) === true)) {
        $options = $options[0];
    }

/**
 * Get Zend_Translate_Adapter
 */
    $translator = $this->translate()      // get Zend_View_Helper_Translate
               ->getTranslator(); // Get Zend_Translate_Adapter

    /**
     * Proxify the call to Zend_Translate_Adapter
     */
    $message = $translator->translate($messageid, $locale);

    /**
     * If no any options provided then just return message
     */
    if ($count === 0) {
        return $message;
    }

    /**
     * Apply options in case we have them
     */
    return vsprintf($message, $options);
}

and use it like:

echo $this->t_('message-id', $param1, $param2);

instead of

echo $this->translate('message-id', $param1, $param2);

Then you can add any custom functionality to that method to log information you need.

This solution is not very fast but allows you to do the trick.

I've created this method while trying to workaround this issue:

http://framework.zend.com/issues/browse/ZF-5547

Upvotes: 0

Related Questions