douren6874 2010-02-25 13:55
浏览 32

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

I'm using PHPTAL in my project I'm able to successfully implement it almost all the cases except when I want to use its i18n services. I constantly get errors "Call to a member function on a non-object"

I've tried searching the net forums etc. but not found any solution, will really appreciate if somebody can help me out.

  • 写回答

1条回答 默认 最新

  • du958642589 2010-03-07 03:32
    关注

    Its heartily disappointing that no one answered my question so here I'm finally with the solution and answering my own question.

    By default there is no translator set by PHPTAL in order to translate your text from one language to another. So you've to do it on your own. There are some steps give below to do this . . .

    Step 1. Create a new php file( e.g. MyTranslator.php ) and generate a new class for example PHPTAL_MyTranslator and store it inside the PHPTAL folder. This class will implement the interface PHPTAL_TranslationService. There are five functions in this interface but the function of our concern is only translate. So just add a declaration for rest of the functions and write code for the translate function. The class I've written and used in my case is :

    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 ) {
        }
    }
    

    Step 2. Now open the PHPTAL.php file and modify the constructor of PHPTAL class. Add a line to this function as shown below . . . . .

    Before

    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')) {
        ............
    

    After

    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')) {
        .............
    

    These two simple steps will make your i18n:attributes as well as i18n:translate attributes to work properly.

    Cheers...

    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致