PHPTAL i18n调用非对象错误的成员函数

时间:2010-02-25 13:55:13

标签: php internationalization phptal

我在我的项目中使用PHPTAL几乎所有情况都能成功实现它,除非我想使用它的i18n服务。我不断收到错误“调用非对象上的成员函数”

我曾尝试搜索网络论坛等但未找到任何解决方案,如果有人可以帮助我,我将非常感激。

1 个答案:

答案 0 :(得分:4)

令人沮丧的是,没有人回答我的问题,所以我终于找到了解决方案并回答了我自己的问题。

默认情况下,PHPTAL没有设置翻译器,以便将文本从一种语言翻译成另一种语言。所以你要自己做。下面有一些步骤可以做到这一点。 。

步骤1。创建一个新的php文件(例如 MyTranslator.php )并生成一个新类,例如 PHPTAL_MyTranslator 并将其存储在里面PHPTAL文件夹。该类将实现接口 PHPTAL_TranslationService 。这个界面有五个功能,但我们关注的功能只是 translate 。因此,只需为其余函数添加声明,并为translate函数编写代码。我在我的案例中编写和使用的课程是:

class PHPTAL_MyTranslator implements PHPTAL_TranslationService {

    /**
     * current execution context
     */
    protected $_context = null;

    /**
     * @param string (name of the language)
     * @return string (language you've just set)
     *
     * This method sets translation language.
     * Name of the language is a dir name where you keep your translation files
     */
    public function setLanguage() {
    }

    public function __construct( $context ) {
        $this->_context = $context;
    }

    /**
     * @param string (translation file name)
     * @return void
     *
     * You can separate translations in several files, and use only when needed.
     * Use this method to specify witch translation file you want to
     * use for current controller.
     */
    public function useDomain( $domain ) {
    }

    /**
     * Set an interpolation var.
     * Replace all ${key}s with values in translated strings.
     */
    public function setVar( $key, $value ) {
    }

    /**
     * Translate a text and interpolate variables.
     */
    public function translate( $key, $htmlescape=true ) {
        $value = $key;
        if( empty( $value ) ) {
            return $key;
        }
        while( preg_match( '/\${(.*?)\}/sm', $value, $m ) ) {
            list( $src, $var ) = $m;
            if( !array_key_exists( $var, $this->_context ) ) {
                $err = sprintf( 'Interpolation error, var "%s" not set', $var );
                throw new Exception( $err );
            }
            $value = str_replace( $src, $this->_context->$var, $value );
        }
        return gettext( $value );
    }

    /**
     * Not implemented yet, default encoding is used
     */
    public function setEncoding( $encoding ) {
    }
}
class PHPTAL_MyTranslator implements PHPTAL_TranslationService { /** * current execution context */ protected $_context = null; /** * @param string (name of the language) * @return string (language you've just set) * * This method sets translation language. * Name of the language is a dir name where you keep your translation files */ public function setLanguage() { } public function __construct( $context ) { $this->_context = $context; } /** * @param string (translation file name) * @return void * * You can separate translations in several files, and use only when needed. * Use this method to specify witch translation file you want to * use for current controller. */ public function useDomain( $domain ) { } /** * Set an interpolation var. * Replace all ${key}s with values in translated strings. */ public function setVar( $key, $value ) { } /** * Translate a text and interpolate variables. */ public function translate( $key, $htmlescape=true ) { $value = $key; if( empty( $value ) ) { return $key; } while( preg_match( '/\${(.*?)\}/sm', $value, $m ) ) { list( $src, $var ) = $m; if( !array_key_exists( $var, $this->_context ) ) { $err = sprintf( 'Interpolation error, var "%s" not set', $var ); throw new Exception( $err ); } $value = str_replace( $src, $this->_context->$var, $value ); } return gettext( $value ); } /** * Not implemented yet, default encoding is used */ public function setEncoding( $encoding ) { } }

第2步。现在打开 PHPTAL.php 文件并修改 PHPTAL 类的构造函数。在此功能中添加一行,如下所示。 。 。 。

之前




public function __construct($path=false) { $this->_path = $path; $this->_globalContext = new StdClass(); $this->_context = new PHPTAL_Context(); $this->_context->setGlobal($this->_globalContext); if (function_exists('sys_get_temp_dir')) { ............

public function __construct($path=false) { $this->_path = $path; $this->_globalContext = new StdClass(); $this->_context = new PHPTAL_Context(); $this->_context->setGlobal($this->_globalContext); if (function_exists('sys_get_temp_dir')) { ............

public function __construct($path=false) { $this->_path = $path; $this->_globalContext = new StdClass(); $this->_context = new PHPTAL_Context(); $this->_context->setGlobal($this->_globalContext); //Set translator here $this->setTranslator( new PHPTAL_MyTranslator( $this->_context ) ); if (function_exists('sys_get_temp_dir')) { .............

这两个简单的步骤将使您的 i18n:attributes 以及 i18n:translate 属性正常工作。

干杯...