1

Zend Frameworkを使用すると、翻訳されていない文字列を簡単にログに記録できます。

私の質問: 翻訳された文字列をどのようにログに記録しますか?

ありがとう!!

4

1 に答える 1

0

Zend_Translate には translate() 呼び出しをログに記録する機能はありませんが、元の translate() ヘルパーへのすべての呼び出しをプロキシし、必要に応じて使用する独自のヘルパーを作成できます。

ヘルパー メソッドの例を次に示します。

/**
 * 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);
}

次のように使用します。

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

それ以外の

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

次に、そのメソッドにカスタム機能を追加して、必要な情報をログに記録できます。

このソリューションはそれほど高速ではありませんが、トリックを実行できます。

この問題を回避しようとして、このメソッドを作成しました。

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

于 2011-04-13T15:47:34.903 に答える