开发者

PHPTAL i18n Call to a member function on a non-object Error

I'm using PHPTAL in my project I'm able to successfu开发者_StackOverflow中文版lly 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.


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜