【问题标题】:Log translated strings when using Zend_Translate使用 Zend_Translate 时记录翻译的字符串
【发布时间】:2010-09-15 07:23:55
【问题描述】:

使用 Zend 框架很容易to log strings that haven't got a translation

我的问题:你如何记录有翻译的字符串?

谢谢!!

【问题讨论】:

  • 您可能需要扩展用于翻译的类,调用parent 方法,然后记录您的成功翻译。

标签: php zend-framework zend-translate zend-log


【解决方案1】:

在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    • 2015-09-04
    • 1970-01-01
    • 2012-05-22
    • 2011-07-03
    • 2013-07-18
    • 2022-07-21
    相关资源
    最近更新 更多